From 80714bb9739feb3ed01eff76335d48689e9d22f1 Mon Sep 17 00:00:00 2001 From: Steve Cohen Date: Tue, 22 Nov 2022 16:28:21 -0800 Subject: [PATCH] Revert "Add basic code action support (#718)" This reverts commit 9fe8ea1f453e0c2c7f7d5bc3fb6da7d4e98bee9f. --- .../lib/language_server/protocol.ex | 11 ---- .../language_server/providers/code_action.ex | 61 ------------------ .../lib/language_server/server.ex | 10 +-- apps/language_server/test/server_test.exs | 64 ------------------- 4 files changed, 2 insertions(+), 144 deletions(-) delete mode 100644 apps/language_server/lib/language_server/providers/code_action.ex diff --git a/apps/language_server/lib/language_server/protocol.ex b/apps/language_server/lib/language_server/protocol.ex index 0845bfa4b..faf5af976 100644 --- a/apps/language_server/lib/language_server/protocol.ex +++ b/apps/language_server/lib/language_server/protocol.ex @@ -200,17 +200,6 @@ defmodule ElixirLS.LanguageServer.Protocol do end end - defmacro code_action_req(id, uri, diagnostics) do - quote do - request(unquote(id), "textDocument/codeAction", %{ - "context" => %{"diagnostics" => unquote(diagnostics)}, - "textDocument" => %{ - "uri" => unquote(uri) - } - }) - end - end - # Other utilities defmacro range(start_line, start_character, end_line, end_character) do diff --git a/apps/language_server/lib/language_server/providers/code_action.ex b/apps/language_server/lib/language_server/providers/code_action.ex deleted file mode 100644 index 288b9bfd3..000000000 --- a/apps/language_server/lib/language_server/providers/code_action.ex +++ /dev/null @@ -1,61 +0,0 @@ -defmodule ElixirLS.LanguageServer.Providers.CodeAction do - use ElixirLS.LanguageServer.Protocol - - def code_actions(uri, diagnostics) do - actions = - diagnostics - |> Enum.map(fn diagnostic -> actions(uri, diagnostic) end) - |> List.flatten() - - {:ok, actions} - end - - defp actions(uri, %{"message" => message} = diagnostic) do - [ - {~r/variable "(.*)" is unused/, &prefix_with_underscore/2}, - {~r/variable "(.*)" is unused/, &remove_variable/2} - ] - |> Enum.filter(fn {r, _fun} -> String.match?(message, r) end) - |> Enum.map(fn {_r, fun} -> fun.(uri, diagnostic) end) - end - - defp prefix_with_underscore(uri, %{"range" => range}) do - %{ - "title" => "Add '_' to unused variable", - "kind" => "quickfix", - "edit" => %{ - "changes" => %{ - uri => [ - %{ - "newText" => "_", - "range" => - range( - range["start"]["line"], - range["start"]["character"], - range["start"]["line"], - range["start"]["character"] - ) - } - ] - } - } - } - end - - defp remove_variable(uri, %{"range" => range}) do - %{ - "title" => "Remove unused variable", - "kind" => "quickfix", - "edit" => %{ - "changes" => %{ - uri => [ - %{ - "newText" => "", - "range" => range - } - ] - } - } - } - end -end diff --git a/apps/language_server/lib/language_server/server.ex b/apps/language_server/lib/language_server/server.ex index 900d04e40..a41fcca50 100644 --- a/apps/language_server/lib/language_server/server.ex +++ b/apps/language_server/lib/language_server/server.ex @@ -32,8 +32,7 @@ defmodule ElixirLS.LanguageServer.Server do OnTypeFormatting, CodeLens, ExecuteCommand, - FoldingRange, - CodeAction + FoldingRange } alias ElixirLS.Utils.Launch @@ -780,10 +779,6 @@ defmodule ElixirLS.LanguageServer.Server do end end - defp handle_request(code_action_req(_id, uri, diagnostics), state = %__MODULE__{}) do - {:async, fn -> CodeAction.code_actions(uri, diagnostics) end, state} - end - defp handle_request(%{"method" => "$/" <> _}, state = %__MODULE__{}) do # "$/" requests that the server doesn't support must return method_not_found {:error, :method_not_found, nil, state} @@ -835,8 +830,7 @@ defmodule ElixirLS.LanguageServer.Server do "workspace" => %{ "workspaceFolders" => %{"supported" => false, "changeNotifications" => false} }, - "foldingRangeProvider" => true, - "codeActionProvider" => true + "foldingRangeProvider" => true } end diff --git a/apps/language_server/test/server_test.exs b/apps/language_server/test/server_test.exs index 3b9ec9342..0bf9f218b 100644 --- a/apps/language_server/test/server_test.exs +++ b/apps/language_server/test/server_test.exs @@ -1525,70 +1525,6 @@ defmodule ElixirLS.LanguageServer.ServerTest do end end - describe "textDocument/codeAction" do - test "return code actions on unused variables", %{server: server} do - uri = "file:///file.ex" - fake_initialize(server) - - Server.receive_packet(server, did_open(uri, "elixir", 1, "")) - - Server.receive_packet( - server, - code_action_req(1, uri, [ - %{ - "message" => - "variable \"foo\" is unused (if the variable is not meant to be used, prefix it with an underscore)", - "range" => %{ - "end" => %{"character" => 13, "line" => 19}, - "start" => %{"character" => 4, "line" => 19} - }, - "severity" => 1, - "source" => "Elixir" - } - ]) - ) - - resp = assert_receive(%{"id" => 1}, 5000) - - assert response(1, [ - %{ - "edit" => %{ - "changes" => %{ - "file:///file.ex" => [ - %{ - "newText" => "_", - "range" => %{ - "end" => %{"character" => 4, "line" => 19}, - "start" => %{"character" => 4, "line" => 19} - } - } - ] - } - }, - "kind" => "quickfix", - "title" => "Add '_' to unused variable" - }, - %{ - "edit" => %{ - "changes" => %{ - "file:///file.ex" => [ - %{ - "newText" => "", - "range" => %{ - "end" => %{"character" => 13, "line" => 19}, - "start" => %{"character" => 4, "line" => 19} - } - } - ] - } - }, - "kind" => "quickfix", - "title" => "Remove unused variable" - } - ]) == resp - end - end - defp with_new_server(func) do server = start_supervised!({Server, nil}) packet_capture = start_supervised!({PacketCapture, self()})