|
| 1 | +defmodule ElixirLS.LanguageServer.Plugins.Ecto do |
| 2 | + @moduledoc false |
| 3 | + |
| 4 | + alias ElixirLS.LanguageServer.Plugins.ModuleStore |
| 5 | + alias ElixirSense.Core.Source |
| 6 | + alias ElixirLS.LanguageServer.Plugins.Ecto.Query |
| 7 | + alias ElixirLS.LanguageServer.Plugins.Ecto.Schema |
| 8 | + alias ElixirLS.LanguageServer.Plugins.Ecto.Types |
| 9 | + |
| 10 | + @behaviour ElixirLS.LanguageServer.Plugin |
| 11 | + use ElixirLS.LanguageServer.Providers.Completion.GenericReducer |
| 12 | + |
| 13 | + @schema_funcs [:field, :belongs_to, :has_one, :has_many, :many_to_many] |
| 14 | + |
| 15 | + @impl true |
| 16 | + def setup(context) do |
| 17 | + ModuleStore.ensure_compiled(context, Ecto.UUID) |
| 18 | + end |
| 19 | + |
| 20 | + @impl true |
| 21 | + def suggestions(hint, {Ecto.Migration, :add, 1, _info}, _chain, opts) do |
| 22 | + builtin_types = Types.find_builtin_types(hint, opts.cursor_context) |
| 23 | + builtin_types = Enum.reject(builtin_types, &String.starts_with?(&1.label, "{")) |
| 24 | + |
| 25 | + {:override, builtin_types} |
| 26 | + end |
| 27 | + |
| 28 | + def suggestions(hint, {Ecto.Schema, :field, 1, _info}, _chain, opts) do |
| 29 | + builtin_types = Types.find_builtin_types(hint, opts.cursor_context) |
| 30 | + custom_types = Types.find_custom_types(hint, opts.module_store) |
| 31 | + |
| 32 | + {:override, builtin_types ++ custom_types} |
| 33 | + end |
| 34 | + |
| 35 | + def suggestions(hint, {Ecto.Schema, func, 1, _info}, _chain, opts) |
| 36 | + when func in @schema_funcs do |
| 37 | + {:override, Schema.find_schemas(hint, opts.module_store)} |
| 38 | + end |
| 39 | + |
| 40 | + def suggestions(hint, {Ecto.Schema, func, 2, %{option: option}}, _, _) |
| 41 | + when func in @schema_funcs and option != nil do |
| 42 | + {:override, Schema.find_option_values(hint, option, func)} |
| 43 | + end |
| 44 | + |
| 45 | + def suggestions(_hint, {Ecto.Schema, func, 2, %{cursor_at_option: false}}, _, _) |
| 46 | + when func in @schema_funcs do |
| 47 | + :ignore |
| 48 | + end |
| 49 | + |
| 50 | + def suggestions(hint, {Ecto.Schema, func, 2, _info}, _, _) |
| 51 | + when func in @schema_funcs do |
| 52 | + {:override, Schema.find_options(hint, func)} |
| 53 | + end |
| 54 | + |
| 55 | + def suggestions(hint, {Ecto.Query, :from, 0, _info}, _, opts) do |
| 56 | + text_before = opts.cursor_context.text_before |
| 57 | + |
| 58 | + if after_in?(hint, text_before) do |
| 59 | + {:add, Schema.find_schemas(hint, opts.module_store)} |
| 60 | + else |
| 61 | + :ignore |
| 62 | + end |
| 63 | + end |
| 64 | + |
| 65 | + def suggestions( |
| 66 | + hint, |
| 67 | + _, |
| 68 | + [{nil, :assoc, 1, assoc_info} | rest], |
| 69 | + opts |
| 70 | + ) do |
| 71 | + text_before = opts.cursor_context.text_before |
| 72 | + env = opts.env |
| 73 | + meta = opts.buffer_metadata |
| 74 | + |
| 75 | + with %{pos: {{line, col}, _}} <- assoc_info, |
| 76 | + from_info when not is_nil(from_info) <- |
| 77 | + Enum.find_value(rest, fn |
| 78 | + {Ecto.Query, :from, 1, from_info} -> from_info |
| 79 | + _ -> nil |
| 80 | + end), |
| 81 | + assoc_code <- Source.text_after(text_before, line, col), |
| 82 | + [_, var] <- |
| 83 | + Regex.run(~r/^assoc\(\s*([_\p{Ll}\p{Lo}][\p{L}\p{N}_]*[?!]?)\s*,/u, assoc_code), |
| 84 | + %{^var => %{type: type}} <- Query.extract_bindings(text_before, from_info, env, meta), |
| 85 | + true <- function_exported?(type, :__schema__, 1) do |
| 86 | + {:override, Query.find_assoc_suggestions(type, hint)} |
| 87 | + else |
| 88 | + _ -> |
| 89 | + :ignore |
| 90 | + end |
| 91 | + end |
| 92 | + |
| 93 | + def suggestions(hint, _func_call, chain, opts) do |
| 94 | + case Enum.find(chain, &match?({Ecto.Query, :from, 1, _}, &1)) do |
| 95 | + {_, _, _, %{cursor_at_option: false} = info} -> |
| 96 | + text_before = opts.cursor_context.text_before |
| 97 | + env = opts.env |
| 98 | + buffer_metadata = opts.buffer_metadata |
| 99 | + |
| 100 | + schemas = |
| 101 | + if after_in?(hint, text_before), |
| 102 | + do: Schema.find_schemas(hint, opts.module_store), |
| 103 | + else: [] |
| 104 | + |
| 105 | + bindings = Query.extract_bindings(text_before, info, env, buffer_metadata) |
| 106 | + {:add, schemas ++ Query.bindings_suggestions(hint, bindings)} |
| 107 | + |
| 108 | + {_, _, _, _} -> |
| 109 | + {:override, Query.find_options(hint)} |
| 110 | + |
| 111 | + _ -> |
| 112 | + :ignore |
| 113 | + end |
| 114 | + end |
| 115 | + |
| 116 | + # Adds customized snippet for `Ecto.Schema.schema/2` |
| 117 | + @impl true |
| 118 | + def decorate(%{origin: "Ecto.Schema", name: "schema", arity: 2} = item) do |
| 119 | + snippet = """ |
| 120 | + schema "$1" do |
| 121 | + $0 |
| 122 | + end |
| 123 | + """ |
| 124 | + |
| 125 | + Map.put(item, :snippet, snippet) |
| 126 | + end |
| 127 | + |
| 128 | + # Fallback |
| 129 | + def decorate(item) do |
| 130 | + item |
| 131 | + end |
| 132 | + |
| 133 | + defp after_in?(hint, text_before) do |
| 134 | + Regex.match?(~r/\s+in\s+#{Regex.escape(hint)}$/u, text_before) |
| 135 | + end |
| 136 | +end |
0 commit comments