From 8486de473a219dcdb72408702f8d56d0227c7366 Mon Sep 17 00:00:00 2001 From: Lukasz Samson Date: Sun, 21 Apr 2024 20:31:58 +0200 Subject: [PATCH] adopt elixir_sense changes --- apps/elixir_ls_utils/lib/completion_engine.ex | 3 +- apps/elixir_ls_utils/test/complete_test.exs | 32 +++---------------- .../providers/completion/reducers/params.ex | 4 +-- .../providers/completion/reducers/struct.ex | 4 +-- .../providers/definition/locator.ex | 4 +-- .../providers/execute_command/expand_macro.ex | 14 +++++--- .../language_server/providers/hover/docs.ex | 3 +- .../providers/implementation/locator.ex | 4 +-- .../language_server/providers/plugins/util.ex | 4 +-- .../providers/references/locator.ex | 7 ++-- .../providers/signature_help/signature.ex | 4 +-- .../providers/plugins/phoenix/scope_test.exs | 2 -- .../providers/references/locator_test.exs | 22 ++++++------- dep_versions.exs | 2 +- mix.lock | 2 +- 15 files changed, 45 insertions(+), 66 deletions(-) diff --git a/apps/elixir_ls_utils/lib/completion_engine.ex b/apps/elixir_ls_utils/lib/completion_engine.ex index ef5ed9dd9..f742b59e3 100644 --- a/apps/elixir_ls_utils/lib/completion_engine.ex +++ b/apps/elixir_ls_utils/lib/completion_engine.ex @@ -484,8 +484,7 @@ defmodule ElixirLS.Utils.CompletionEngine do match_module_funs(env.module, hint, exact?, false, :all, env, metadata, cursor_position) imported_locals = - env.imports - |> Introspection.expand_imports(metadata.mods_funs_to_positions) + {env.functions, env.macros} |> Introspection.combine_imports() |> Enum.flat_map(fn {scope_import, imported} -> match_module_funs( diff --git a/apps/elixir_ls_utils/test/complete_test.exs b/apps/elixir_ls_utils/test/complete_test.exs index b6c5cbf24..ed82b5317 100644 --- a/apps/elixir_ls_utils/test/complete_test.exs +++ b/apps/elixir_ls_utils/test/complete_test.exs @@ -34,9 +34,7 @@ defmodule ElixirLS.Utils.CompletionEngineTest do def expand( expr, - env \\ %Env{ - imports: [{Kernel, []}] - }, + env \\ %Env{functions: :elixir_env.new().functions, macros: :elixir_env.new().macros}, metadata \\ %Metadata{}, opts \\ [] ) do @@ -1237,34 +1235,26 @@ defmodule ElixirLS.Utils.CompletionEngineTest do metadata = %Metadata{ mods_funs_to_positions: %{ {MyModule, nil, nil} => %ModFunInfo{type: :defmodule}, - {MyModule, :my_fun_priv, nil} => %ModFunInfo{type: :defp}, {MyModule, :my_fun_priv, 2} => %ModFunInfo{ type: :defp, params: [[{:some, [], nil}, {:other, [], nil}]] }, - {MyModule, :my_fun_pub, nil} => %ModFunInfo{type: :def}, {MyModule, :my_fun_pub, 1} => %ModFunInfo{type: :def, params: [[{:some, [], nil}]]}, - {MyModule, :my_macro_priv, nil} => %ModFunInfo{type: :defmacrop}, {MyModule, :my_macro_priv, 1} => %ModFunInfo{ type: :defmacrop, params: [[{:some, [], nil}]] }, - {MyModule, :my_macro_pub, nil} => %ModFunInfo{type: :defmacro}, {MyModule, :my_macro_pub, 1} => %ModFunInfo{type: :defmacro, params: [[{:some, [], nil}]]}, - {MyModule, :my_guard_priv, nil} => %ModFunInfo{type: :defguardp}, {MyModule, :my_guard_priv, 1} => %ModFunInfo{ type: :defguardp, params: [[{:some, [], nil}]] }, - {MyModule, :my_guard_pub, nil} => %ModFunInfo{type: :defguard}, {MyModule, :my_guard_pub, 1} => %ModFunInfo{type: :defguard, params: [[{:some, [], nil}]]}, - {MyModule, :my_delegated, nil} => %ModFunInfo{type: :defdelegate}, {MyModule, :my_delegated, 1} => %ModFunInfo{ type: :defdelegate, params: [[{:some, [], nil}]] }, {OtherModule, nil, nil} => %ModFunInfo{}, - {OtherModule, :my_fun_pub_other, nil} => %ModFunInfo{type: :def}, {OtherModule, :my_fun_pub_other, 1} => %ModFunInfo{ type: :def, params: [[{:some, [], nil}]] @@ -1318,18 +1308,16 @@ defmodule ElixirLS.Utils.CompletionEngineTest do test "complete remote funs from imported module" do env = %Env{ module: MyModule, - imports: [{OtherModule, []}, {Kernel, []}] + functions: [{OtherModule, [{:my_fun_other_pub, 1}]}] } metadata = %Metadata{ mods_funs_to_positions: %{ {OtherModule, nil, nil} => %ModFunInfo{type: :defmodule}, - {OtherModule, :my_fun_other_pub, nil} => %ModFunInfo{type: :def}, {OtherModule, :my_fun_other_pub, 1} => %ModFunInfo{ type: :def, params: [[{:some, [], nil}]] }, - {OtherModule, :my_fun_other_priv, nil} => %ModFunInfo{type: :defp}, {OtherModule, :my_fun_other_priv, 1} => %ModFunInfo{ type: :defp, params: [[{:some, [], nil}]] @@ -1345,13 +1333,12 @@ defmodule ElixirLS.Utils.CompletionEngineTest do test "complete remote funs from imported module - needed import" do env = %Env{ module: MyModule, - imports: [{OtherModule, [only: [{:my_fun_other_pub, 1}]]}, {Kernel, []}] + functions: [{OtherModule, [{:my_fun_other_pub, 1}]}] } metadata = %Metadata{ mods_funs_to_positions: %{ {OtherModule, nil, nil} => %ModFunInfo{type: :defmodule}, - {OtherModule, :my_fun_other_pub, nil} => %ModFunInfo{type: :def}, {OtherModule, :my_fun_other_pub, 1} => %ModFunInfo{ type: :def, params: [[{:some, [], nil}]] @@ -1360,7 +1347,6 @@ defmodule ElixirLS.Utils.CompletionEngineTest do type: :def, params: [[{:some, [], nil}]] }, - {OtherModule, :my_fun_other_priv, nil} => %ModFunInfo{type: :defp}, {OtherModule, :my_fun_other_priv, 1} => %ModFunInfo{ type: :defp, params: [[{:some, [], nil}]] @@ -1386,12 +1372,10 @@ defmodule ElixirLS.Utils.CompletionEngineTest do metadata = %Metadata{ mods_funs_to_positions: %{ {Some.OtherModule, nil, nil} => %ModFunInfo{type: :defmodule}, - {Some.OtherModule, :my_fun_other_pub, nil} => %ModFunInfo{type: :def}, {Some.OtherModule, :my_fun_other_pub, 1} => %ModFunInfo{ type: :def, params: [[{:some, [], nil}]] }, - {Some.OtherModule, :my_fun_other_priv, nil} => %ModFunInfo{type: :defp}, {Some.OtherModule, :my_fun_other_priv, 1} => %ModFunInfo{ type: :defp, params: [[{:some, [], nil}]] @@ -1413,12 +1397,10 @@ defmodule ElixirLS.Utils.CompletionEngineTest do metadata = %Metadata{ mods_funs_to_positions: %{ {Some.OtherModule, nil, nil} => %ModFunInfo{type: :defmodule}, - {Some.OtherModule, :my_fun_other_pub, nil} => %ModFunInfo{type: :def}, {Some.OtherModule, :my_fun_other_pub, 1} => %ModFunInfo{ type: :def, params: [[{:some, [], nil}]] }, - {Some.OtherModule, :my_fun_other_priv, nil} => %ModFunInfo{type: :defp}, {Some.OtherModule, :my_fun_other_priv, 1} => %ModFunInfo{ type: :defp, params: [[{:some, [], nil}]] @@ -1465,12 +1447,10 @@ defmodule ElixirLS.Utils.CompletionEngineTest do metadata = %Metadata{ mods_funs_to_positions: %{ {Some.OtherModule, nil, nil} => %ModFunInfo{type: :defmodule}, - {Some.OtherModule, :my_fun_other_pub, nil} => %ModFunInfo{type: :def}, {Some.OtherModule, :my_fun_other_pub, 1} => %ModFunInfo{ type: :def, params: [[{:some, [], nil}]] }, - {Some.OtherModule, :my_fun_other_priv, nil} => %ModFunInfo{type: :defp}, {Some.OtherModule, :my_fun_other_priv, 1} => %ModFunInfo{ type: :defp, params: [[{:some, [], nil}]] @@ -1529,8 +1509,7 @@ defmodule ElixirLS.Utils.CompletionEngineTest do metadata = %Metadata{ mods_funs_to_positions: %{ {MyKeyword, nil, nil} => %ModFunInfo{type: :defmodule}, - {MyKeyword, :values1, 0} => %ModFunInfo{type: :def, params: [[]]}, - {MyKeyword, :values1, nil} => %ModFunInfo{type: :def} + {MyKeyword, :values1, 0} => %ModFunInfo{type: :def, params: [[]]} } } @@ -1826,10 +1805,8 @@ defmodule ElixirLS.Utils.CompletionEngineTest do metadata = %Metadata{ mods_funs_to_positions: %{ {MyModule, nil, nil} => %ModFunInfo{type: :defmodule}, - {MyModule, :module_info, nil} => %ModFunInfo{type: :def}, {MyModule, :module_info, 0} => %ModFunInfo{type: :def, params: [[]]}, {MyModule, :module_info, 1} => %ModFunInfo{type: :def, params: [[{:atom, [], nil}]]}, - {MyModule, :__info__, nil} => %ModFunInfo{type: :def}, {MyModule, :__info__, 1} => %ModFunInfo{type: :def, params: [[{:atom, [], nil}]]} } } @@ -2167,7 +2144,6 @@ defmodule ElixirLS.Utils.CompletionEngineTest do metadata = %Metadata{ mods_funs_to_positions: %{ {MyModule, nil, nil} => %ElixirSense.Core.State.ModFunInfo{}, - {MyModule, :info, nil} => macro_info, {MyModule, :info, 1} => macro_info } } diff --git a/apps/language_server/lib/language_server/providers/completion/reducers/params.ex b/apps/language_server/lib/language_server/providers/completion/reducers/params.ex index 530a5e1c7..0d0eacc38 100644 --- a/apps/language_server/lib/language_server/providers/completion/reducers/params.ex +++ b/apps/language_server/lib/language_server/providers/completion/reducers/params.ex @@ -31,7 +31,6 @@ defmodule ElixirLS.LanguageServer.Providers.Completion.Reducers.Params do prefix = cursor_context.text_before %State.Env{ - imports: imports, requires: requires, aliases: aliases, module: module, @@ -51,7 +50,8 @@ defmodule ElixirLS.LanguageServer.Providers.Completion.Reducers.Params do {mod, fun, true, :mod_fun} <- Introspection.actual_mod_fun( {mod, fun}, - imports, + env.functions, + env.macros, requires, if(elixir_prefix, do: [], else: aliases), module, diff --git a/apps/language_server/lib/language_server/providers/completion/reducers/struct.ex b/apps/language_server/lib/language_server/providers/completion/reducers/struct.ex index e5733b820..0f1d952af 100644 --- a/apps/language_server/lib/language_server/providers/completion/reducers/struct.ex +++ b/apps/language_server/lib/language_server/providers/completion/reducers/struct.ex @@ -55,7 +55,6 @@ defmodule ElixirLS.LanguageServer.Providers.Completion.Reducers.Struct do module: module, vars: vars, attributes: attributes, - imports: imports, aliases: aliases } = env @@ -70,7 +69,8 @@ defmodule ElixirLS.LanguageServer.Providers.Completion.Reducers.Struct do attributes: attributes, variables: vars, structs: structs, - imports: imports, + functions: env.functions, + macros: env.macros, current_module: module, specs: specs, types: metadata_types, diff --git a/apps/language_server/lib/language_server/providers/definition/locator.ex b/apps/language_server/lib/language_server/providers/definition/locator.ex index dbd57eb56..a092f5c5b 100644 --- a/apps/language_server/lib/language_server/providers/definition/locator.ex +++ b/apps/language_server/lib/language_server/providers/definition/locator.ex @@ -202,7 +202,6 @@ defmodule ElixirLS.LanguageServer.Providers.Definition.Locator do ) do %State.Env{ module: current_module, - imports: imports, requires: requires, aliases: aliases, scope: scope @@ -212,7 +211,8 @@ defmodule ElixirLS.LanguageServer.Providers.Definition.Locator do case {m, function} |> Introspection.actual_mod_fun( - imports, + env.functions, + env.macros, requires, aliases, current_module, diff --git a/apps/language_server/lib/language_server/providers/execute_command/expand_macro.ex b/apps/language_server/lib/language_server/providers/execute_command/expand_macro.ex index 646c9a422..88fd67d3a 100644 --- a/apps/language_server/lib/language_server/providers/execute_command/expand_macro.ex +++ b/apps/language_server/lib/language_server/providers/execute_command/expand_macro.ex @@ -59,12 +59,16 @@ defmodule ElixirLS.LanguageServer.Providers.ExecuteCommand.ExpandMacro do do_expand_full(code, env) end - def do_expand_full(code, %State.Env{requires: requires, imports: imports, module: module}) do + def do_expand_full(code, %State.Env{} = env) do + # TODO function and other env = - %Macro.Env{macros: __ENV__.macros} - |> Ast.set_module_for_env(module) - |> Ast.add_requires_to_env(requires) - |> Ast.add_imports_to_env(imports) + %Macro.Env{ + macros: env.macros, + functions: env.functions, + module: env.module, + requires: env.requires, + aliases: env.aliases + } try do {:ok, expr} = code |> Code.string_to_quoted() diff --git a/apps/language_server/lib/language_server/providers/hover/docs.ex b/apps/language_server/lib/language_server/providers/hover/docs.ex index a5dbfe9a7..c29c54b21 100644 --- a/apps/language_server/lib/language_server/providers/hover/docs.ex +++ b/apps/language_server/lib/language_server/providers/hover/docs.ex @@ -181,7 +181,8 @@ defmodule ElixirLS.LanguageServer.Providers.Hover.Docs do {Binding.expand(binding_env, mod), fun} |> expand(env.aliases) |> Introspection.actual_mod_fun( - env.imports, + env.functions, + env.macros, env.requires, env.aliases, env.module, diff --git a/apps/language_server/lib/language_server/providers/implementation/locator.ex b/apps/language_server/lib/language_server/providers/implementation/locator.ex index 7b0085257..fa802e5f3 100644 --- a/apps/language_server/lib/language_server/providers/implementation/locator.ex +++ b/apps/language_server/lib/language_server/providers/implementation/locator.ex @@ -253,7 +253,6 @@ defmodule ElixirLS.LanguageServer.Providers.Implementation.Locator do ) do %State.Env{ module: current_module, - imports: imports, requires: requires, aliases: aliases, scope: scope @@ -261,7 +260,8 @@ defmodule ElixirLS.LanguageServer.Providers.Implementation.Locator do case {module, function} |> Introspection.actual_mod_fun( - imports, + env.functions, + env.macros, requires, aliases, current_module, diff --git a/apps/language_server/lib/language_server/providers/plugins/util.ex b/apps/language_server/lib/language_server/providers/plugins/util.ex index 06ddf0ac8..979747b8d 100644 --- a/apps/language_server/lib/language_server/providers/plugins/util.ex +++ b/apps/language_server/lib/language_server/providers/plugins/util.ex @@ -40,7 +40,6 @@ defmodule ElixirLS.LanguageServer.Plugins.Util do def actual_mod_fun({mod, fun}, elixir_prefix, env, buffer_metadata) do %State.Env{ - imports: imports, requires: requires, aliases: aliases, module: module, @@ -51,7 +50,8 @@ defmodule ElixirLS.LanguageServer.Plugins.Util do Introspection.actual_mod_fun( {mod, fun}, - imports, + env.functions, + env.macros, requires, if(elixir_prefix, do: [], else: aliases), module, diff --git a/apps/language_server/lib/language_server/providers/references/locator.ex b/apps/language_server/lib/language_server/providers/references/locator.ex index fe64c04b3..5262ca306 100644 --- a/apps/language_server/lib/language_server/providers/references/locator.ex +++ b/apps/language_server/lib/language_server/providers/references/locator.ex @@ -95,7 +95,6 @@ defmodule ElixirLS.LanguageServer.Providers.References.Locator do def find( context, %State.Env{ - imports: imports, requires: requires, aliases: aliases, module: module, @@ -119,7 +118,8 @@ defmodule ElixirLS.LanguageServer.Providers.References.Locator do {mod, function} |> expand(binding_env, module, aliases) |> Introspection.actual_mod_fun( - imports, + env.functions, + env.macros, requires, aliases, module, @@ -149,7 +149,8 @@ defmodule ElixirLS.LanguageServer.Providers.References.Locator do |> wrap_atom |> expand(binding_env, module, aliases) |> Introspection.actual_mod_fun( - env.imports, + env.functions, + env.macros, env.requires, env.aliases, env.module, diff --git a/apps/language_server/lib/language_server/providers/signature_help/signature.ex b/apps/language_server/lib/language_server/providers/signature_help/signature.ex index b6c80a367..c94234bab 100644 --- a/apps/language_server/lib/language_server/providers/signature_help/signature.ex +++ b/apps/language_server/lib/language_server/providers/signature_help/signature.ex @@ -36,7 +36,6 @@ defmodule ElixirLS.LanguageServer.Providers.SignatureHelp.Signature do signature_info | :none def find(prefix, cursor_position, env, metadata) do %State.Env{ - imports: imports, requires: requires, aliases: aliases, module: module, @@ -50,7 +49,8 @@ defmodule ElixirLS.LanguageServer.Providers.SignatureHelp.Signature do {mod, fun, true, kind} <- Introspection.actual_mod_fun( {m, f}, - imports, + env.functions, + env.macros, requires, if(elixir_prefix, do: [], else: aliases), module, diff --git a/apps/language_server/test/providers/plugins/phoenix/scope_test.exs b/apps/language_server/test/providers/plugins/phoenix/scope_test.exs index e78a58a5d..1b9bb2d81 100644 --- a/apps/language_server/test/providers/plugins/phoenix/scope_test.exs +++ b/apps/language_server/test/providers/plugins/phoenix/scope_test.exs @@ -66,7 +66,6 @@ defmodule ElixirLS.LanguageServer.Plugins.Phoenix.ScopeTest do } ], current_module: ExampleWeb.Router, - imports: [{Kernel, []}, {Phoenix.Router, []}], specs: %{}, types: %{}, mods_funs: %{} @@ -98,7 +97,6 @@ defmodule ElixirLS.LanguageServer.Plugins.Phoenix.ScopeTest do ], attributes: [], current_module: ExampleWeb.Router, - imports: [{Kernel, []}, {Phoenix.Router, []}], specs: %{}, types: %{}, mods_funs: %{} diff --git a/apps/language_server/test/providers/references/locator_test.exs b/apps/language_server/test/providers/references/locator_test.exs index d2d04bf72..a6fa457f1 100644 --- a/apps/language_server/test/providers/references/locator_test.exs +++ b/apps/language_server/test/providers/references/locator_test.exs @@ -1657,6 +1657,13 @@ defmodule ElixirLS.LanguageServer.Providers.References.LocatorTest do test "find references of public metadata functions from definition", %{trace: trace} do buffer = """ + defmodule MyCalleeModule.Some do + def public_fun do + # ^ + :ok + end + end + defmodule MyModule do def calls_public do MyCalleeModule.Some.public_fun() @@ -1672,21 +1679,14 @@ defmodule ElixirLS.LanguageServer.Providers.References.LocatorTest do public_fun() end end - - defmodule MyCalleeModule.Some do - def public_fun do - # ^ - :ok - end - end """ - references = Locator.references(buffer, 18, 15, trace) + references = Locator.references(buffer, 2, 15, trace) assert references == [ - %{uri: nil, range: %{start: %{line: 3, column: 25}, end: %{line: 3, column: 35}}}, - %{uri: nil, range: %{start: %{line: 8, column: 10}, end: %{line: 8, column: 20}}}, - %{uri: nil, range: %{start: %{line: 13, column: 5}, end: %{line: 13, column: 15}}} + %{uri: nil, range: %{start: %{line: 10, column: 25}, end: %{line: 10, column: 35}}}, + %{uri: nil, range: %{start: %{line: 15, column: 10}, end: %{line: 15, column: 20}}}, + %{uri: nil, range: %{start: %{line: 20, column: 5}, end: %{line: 20, column: 15}}} ] end diff --git a/dep_versions.exs b/dep_versions.exs index dde9862d8..0ff3a4106 100644 --- a/dep_versions.exs +++ b/dep_versions.exs @@ -1,5 +1,5 @@ [ - elixir_sense: "33c9113324a6ed716612499afcfef2c3049730d6", + elixir_sense: "ec46edb5475d61769839b1945546c5520d1a53f3", dialyxir_vendored: "d50dcd7101c6ebd37b57b7ee4a7888d8cb634782", jason_v: "c81537e2a5e1acacb915cf339fe400357e3c2aaa", erl2ex_vendored: "073ac6b9a44282e718b6050c7b27cedf9217a12a", diff --git a/mix.lock b/mix.lock index 36f252fe8..b81fccaff 100644 --- a/mix.lock +++ b/mix.lock @@ -2,7 +2,7 @@ "benchee": {:hex, :benchee, "1.1.0", "f3a43817209a92a1fade36ef36b86e1052627fd8934a8b937ac9ab3a76c43062", [:mix], [{:deep_merge, "~> 1.0", [hex: :deep_merge, repo: "hexpm", optional: false]}, {:statistex, "~> 1.0", [hex: :statistex, repo: "hexpm", optional: false]}], "hexpm", "7da57d545003165a012b587077f6ba90b89210fd88074ce3c60ce239eb5e6d93"}, "deep_merge": {:hex, :deep_merge, "1.0.0", "b4aa1a0d1acac393bdf38b2291af38cb1d4a52806cf7a4906f718e1feb5ee961", [:mix], [], "hexpm", "ce708e5f094b9cd4e8f2be4f00d2f4250c4095be93f8cd6d018c753894885430"}, "dialyxir_vendored": {:git, "https://github.com/elixir-lsp/dialyxir.git", "d50dcd7101c6ebd37b57b7ee4a7888d8cb634782", [ref: "d50dcd7101c6ebd37b57b7ee4a7888d8cb634782"]}, - "elixir_sense": {:git, "https://github.com/elixir-lsp/elixir_sense.git", "33c9113324a6ed716612499afcfef2c3049730d6", [ref: "33c9113324a6ed716612499afcfef2c3049730d6"]}, + "elixir_sense": {:git, "https://github.com/elixir-lsp/elixir_sense.git", "ec46edb5475d61769839b1945546c5520d1a53f3", [ref: "ec46edb5475d61769839b1945546c5520d1a53f3"]}, "erl2ex_vendored": {:git, "https://github.com/elixir-lsp/erl2ex.git", "073ac6b9a44282e718b6050c7b27cedf9217a12a", [ref: "073ac6b9a44282e718b6050c7b27cedf9217a12a"]}, "erlex_vendored": {:git, "https://github.com/elixir-lsp/erlex.git", "82db0e82ee4896491bc26dec99f5d795f03ab9f4", [ref: "82db0e82ee4896491bc26dec99f5d795f03ab9f4"]}, "jason_v": {:git, "https://github.com/elixir-lsp/jason.git", "c81537e2a5e1acacb915cf339fe400357e3c2aaa", [ref: "c81537e2a5e1acacb915cf339fe400357e3c2aaa"]},