From fd899803bf1297cf575b93bd59d1b189694114af Mon Sep 17 00:00:00 2001 From: Steve Cohen Date: Thu, 1 Dec 2022 22:04:52 -0800 Subject: [PATCH] The code action needs to fix up the line numbers Code mods deal with snippets of code that need to have their line numbers fixed up by the code actions. --- .../code_action/replace_with_underscore.ex | 11 +++++++++++ .../code_action/replace_with_underscore_test.exs | 14 ++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/apps/language_server/lib/language_server/experimental/provider/code_action/replace_with_underscore.ex b/apps/language_server/lib/language_server/experimental/provider/code_action/replace_with_underscore.ex index 5ce3a8e89..688619f53 100644 --- a/apps/language_server/lib/language_server/experimental/provider/code_action/replace_with_underscore.ex +++ b/apps/language_server/lib/language_server/experimental/provider/code_action/replace_with_underscore.ex @@ -2,11 +2,13 @@ defmodule ElixirLS.LanguageServer.Experimental.Provider.CodeAction.ReplaceWithUn @moduledoc """ A code action that prefixes unused variables with an underscore """ + alias ElixirLS.LanguageServer.Experimental.CodeMod alias ElixirLS.LanguageServer.Experimental.CodeMod.Ast alias ElixirLS.LanguageServer.Experimental.Protocol.Requests.CodeAction alias ElixirLS.LanguageServer.Experimental.Protocol.Types.CodeAction, as: CodeActionResult alias ElixirLS.LanguageServer.Experimental.Protocol.Types.Diagnostic + alias ElixirLS.LanguageServer.Experimental.Protocol.Types.TextEdit alias ElixirLS.LanguageServer.Experimental.Protocol.Types.WorkspaceEdit alias ElixirLS.LanguageServer.Experimental.SourceFile @@ -37,6 +39,8 @@ defmodule ElixirLS.LanguageServer.Experimental.Provider.CodeAction.ReplaceWithUn :error [_ | _] -> + text_edits = Enum.map(text_edits, &update_line(&1, one_based_line)) + reply = CodeActionResult.new( title: "Rename to _#{variable_name}", @@ -49,6 +53,13 @@ defmodule ElixirLS.LanguageServer.Experimental.Provider.CodeAction.ReplaceWithUn end end + defp update_line(%TextEdit{} = text_edit, line_number) do + text_edit = + text_edit + |> put_in([:range, :start, :line], line_number - 1) + |> put_in([:range, :end, :line], line_number - 1) + end + defp extract_variable_and_line(%Diagnostic{} = diagnostic) do with {:ok, variable_name} <- extract_variable_name(diagnostic.message), {:ok, line} <- extract_line(diagnostic) do diff --git a/apps/language_server/test/experimental/provider/code_action/replace_with_underscore_test.exs b/apps/language_server/test/experimental/provider/code_action/replace_with_underscore_test.exs index 71feb57bb..baa1195f0 100644 --- a/apps/language_server/test/experimental/provider/code_action/replace_with_underscore_test.exs +++ b/apps/language_server/test/experimental/provider/code_action/replace_with_underscore_test.exs @@ -131,4 +131,18 @@ defmodule ElixirLS.LanguageServer.Experimental.Provider.CodeAction.ReplaceWithUn assert [%CodeActionReply{edit: %{changes: %{^file_uri => [edit]}}}] = apply(code_action) assert edit.new_text == "_" end + + test "works with multiple lines" do + {file_uri, code_action} = ~S[ + defmodule MyModule do + def my_func(a) do + end + end + ] |> code_action("/project/file.ex", 1, "a") + + assert [%CodeActionReply{edit: %{changes: %{^file_uri => [edit]}}}] = apply(code_action) + assert edit.new_text == "_" + assert edit.range.start.line == 1 + assert edit.range.end.line == 1 + end end