diff --git a/lib/next_ls/extensions/elixir_extension/code_action/require.ex b/lib/next_ls/extensions/elixir_extension/code_action/require.ex index 8ae34351..ea638847 100644 --- a/lib/next_ls/extensions/elixir_extension/code_action/require.ex +++ b/lib/next_ls/extensions/elixir_extension/code_action/require.ex @@ -16,19 +16,18 @@ defmodule NextLS.ElixirExtension.CodeAction.Require do with {:ok, require_module} <- get_edit(diagnostic.message), {:ok, ast} <- parse_ast(text), {:ok, defm} <- nearest_defmodule(ast, range), - {:ok, module_name} <- get_module_name(defm), indentation <- get_indent(text, defm), nearest <- find_nearest_node_for_require(defm), range <- get_edit_range(nearest) do [ %CodeAction{ - title: "Add missing require in #{module_name}", + title: "Add missing require for #{require_module}", diagnostics: [diagnostic], edit: %WorkspaceEdit{ changes: %{ uri => [ %TextEdit{ - new_text: indentation <> require_module, + new_text: indentation <> "require #{require_module}\n", range: range } ] @@ -72,7 +71,7 @@ defmodule NextLS.ElixirExtension.CodeAction.Require do @module_name ~r/require\s+([^\s]+)\s+before/ defp get_edit(message) do case Regex.run(@module_name, message) do - [_, module] -> {:ok, "require #{module}\n"} + [_, module] -> {:ok, module} _ -> {:error, "unable to find require"} end end @@ -119,15 +118,4 @@ defmodule NextLS.ElixirExtension.CodeAction.Require do context end end - - defp get_module_name({:defmodule, _, [{:__aliases__, _, alias} | _rest]}) do - name = - alias - |> Module.concat() - |> Macro.to_string() - - {:ok, name} - end - - defp get_module_name(_), do: {:error, "expected defmodule ast"} end diff --git a/test/next_ls/extensions/elixir_extension/code_action/require_test.exs b/test/next_ls/extensions/elixir_extension/code_action/require_test.exs index cdc9861f..39e5942b 100644 --- a/test/next_ls/extensions/elixir_extension/code_action/require_test.exs +++ b/test/next_ls/extensions/elixir_extension/code_action/require_test.exs @@ -38,7 +38,7 @@ defmodule NextLS.ElixirExtension.RequireTest do assert [code_action] = Require.new(diagnostic, text, uri) assert is_struct(code_action, CodeAction) assert [diagnostic] == code_action.diagnostics - assert code_action.title =~ "Test.Require" + assert code_action.title == "Add missing require for Logger" assert %WorkspaceEdit{ changes: %{ @@ -83,7 +83,7 @@ defmodule NextLS.ElixirExtension.RequireTest do assert [code_action] = Require.new(diagnostic, text, uri) assert is_struct(code_action, CodeAction) assert [diagnostic] == code_action.diagnostics - assert code_action.title =~ "Test.Require" + assert code_action.title == "Add missing require for Logger" assert %WorkspaceEdit{ changes: %{ @@ -130,7 +130,7 @@ defmodule NextLS.ElixirExtension.RequireTest do assert [code_action] = Require.new(diagnostic, text, uri) assert is_struct(code_action, CodeAction) assert [diagnostic] == code_action.diagnostics - assert code_action.title =~ "Test.Require" + assert code_action.title == "Add missing require for Logger" assert %WorkspaceEdit{ changes: %{ @@ -186,7 +186,7 @@ defmodule NextLS.ElixirExtension.RequireTest do assert [code_action] = Require.new(diagnostic, text, uri) assert is_struct(code_action, CodeAction) assert [diagnostic] == code_action.diagnostics - assert code_action.title =~ "Require" + assert code_action.title == "Add missing require for Logger" assert %WorkspaceEdit{ changes: %{