-
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: unused variable code action (#349)
- Loading branch information
Showing
11 changed files
with
272 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
defmodule NextLS.CodeActionable do | ||
@moduledoc false | ||
# A diagnostic can produce 1 or more code actions hence we return a list | ||
|
||
alias GenLSP.Structures.CodeAction | ||
alias GenLSP.Structures.Diagnostic | ||
|
||
defmodule Data do | ||
@moduledoc false | ||
defstruct [:diagnostic, :uri, :document] | ||
|
||
@type t :: %__MODULE__{ | ||
diagnostic: Diagnostic.t(), | ||
uri: String.t(), | ||
document: String.t() | ||
} | ||
end | ||
|
||
@callback from(diagnostic :: Data.t()) :: [CodeAction.t()] | ||
|
||
# TODO: Add support for third party extensions | ||
def from("elixir", diagnostic_data) do | ||
NextLS.ElixirExtension.CodeAction.from(diagnostic_data) | ||
end | ||
|
||
def from("credo", diagnostic_data) do | ||
NextLS.CredoExtension.CodeAction.from(diagnostic_data) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
defmodule NextLS.CredoExtension.CodeAction do | ||
@moduledoc false | ||
|
||
@behaviour NextLS.CodeActionable | ||
|
||
alias NextLS.CodeActionable.Data | ||
|
||
@impl true | ||
def from(%Data{} = _data), do: [] | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
defmodule NextLS.ElixirExtension.CodeAction do | ||
@moduledoc false | ||
|
||
@behaviour NextLS.CodeActionable | ||
|
||
alias NextLS.CodeActionable.Data | ||
alias NextLS.ElixirExtension.CodeAction.UnusedVariable | ||
|
||
@impl true | ||
def from(%Data{} = data) do | ||
case data.diagnostic.data do | ||
%{"type" => "unused_variable"} -> | ||
UnusedVariable.new(data.diagnostic, data.document, data.uri) | ||
|
||
_ -> | ||
[] | ||
end | ||
end | ||
end |
33 changes: 33 additions & 0 deletions
33
lib/next_ls/extensions/elixir_extension/code_action/unused_variable.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
defmodule NextLS.ElixirExtension.CodeAction.UnusedVariable do | ||
@moduledoc false | ||
|
||
alias GenLSP.Structures.CodeAction | ||
alias GenLSP.Structures.Diagnostic | ||
alias GenLSP.Structures.Range | ||
alias GenLSP.Structures.TextEdit | ||
alias GenLSP.Structures.WorkspaceEdit | ||
|
||
def new(diagnostic, _text, uri) do | ||
%Diagnostic{range: %{start: start}} = diagnostic | ||
|
||
[ | ||
%CodeAction{ | ||
title: "Underscore unused variable", | ||
diagnostics: [diagnostic], | ||
edit: %WorkspaceEdit{ | ||
changes: %{ | ||
uri => [ | ||
%TextEdit{ | ||
new_text: "_", | ||
range: %Range{ | ||
start: start, | ||
end: start | ||
} | ||
} | ||
] | ||
} | ||
} | ||
} | ||
] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
test/next_ls/extensions/elixir_extension/code_action/unused_variable_test.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
defmodule NextLS.ElixirExtension.UnusedVariableTest do | ||
use ExUnit.Case, async: true | ||
|
||
alias GenLSP.Structures.CodeAction | ||
alias GenLSP.Structures.Position | ||
alias GenLSP.Structures.Range | ||
alias GenLSP.Structures.TextEdit | ||
alias GenLSP.Structures.WorkspaceEdit | ||
alias NextLS.ElixirExtension.CodeAction.UnusedVariable | ||
|
||
test "adds an underscore to unused variables" do | ||
text = """ | ||
defmodule Test.Unused do | ||
def hello() do | ||
foo = 3 | ||
:world | ||
end | ||
end | ||
""" | ||
|
||
start = %Position{character: 4, line: 3} | ||
|
||
diagnostic = %GenLSP.Structures.Diagnostic{ | ||
data: %{"namespace" => "elixir", "type" => "unused_variable"}, | ||
message: "variable \"foo\" is unused (if the variable is not meant to be used, prefix it with an underscore)", | ||
source: "Elixir", | ||
range: %GenLSP.Structures.Range{ | ||
start: start, | ||
end: %{start | character: 999} | ||
} | ||
} | ||
|
||
uri = "file:///home/owner/my_project/hello.ex" | ||
|
||
assert [code_action] = UnusedVariable.new(diagnostic, text, uri) | ||
assert is_struct(code_action, CodeAction) | ||
assert [diagnostic] == code_action.diagnostics | ||
|
||
# We insert a single underscore character at the start position of the unused variable | ||
# Hence the start and end positions are matching the original start position in the diagnostic | ||
assert %WorkspaceEdit{ | ||
changes: %{ | ||
^uri => [ | ||
%TextEdit{ | ||
new_text: "_", | ||
range: %Range{start: ^start, end: ^start} | ||
} | ||
] | ||
} | ||
} = code_action.edit | ||
end | ||
end |
77 changes: 77 additions & 0 deletions
77
test/next_ls/extensions/elixir_extension/code_action_test.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
defmodule NextLS.Extensions.ElixirExtension.CodeActionTest do | ||
use ExUnit.Case, async: true | ||
|
||
import GenLSP.Test | ||
import NextLS.Support.Utils | ||
|
||
@moduletag :tmp_dir | ||
@moduletag root_paths: ["my_proj"] | ||
|
||
setup %{tmp_dir: tmp_dir} do | ||
File.mkdir_p!(Path.join(tmp_dir, "my_proj/lib")) | ||
File.write!(Path.join(tmp_dir, "my_proj/mix.exs"), mix_exs()) | ||
|
||
cwd = Path.join(tmp_dir, "my_proj") | ||
|
||
foo_path = Path.join(cwd, "lib/foo.ex") | ||
|
||
foo = """ | ||
defmodule MyProj.Foo do | ||
def hello() do | ||
foo = :bar | ||
:world | ||
end | ||
end | ||
""" | ||
|
||
File.write!(foo_path, foo) | ||
|
||
[foo: foo, foo_path: foo_path] | ||
end | ||
|
||
setup :with_lsp | ||
|
||
setup context do | ||
assert :ok == notify(context.client, %{method: "initialized", jsonrpc: "2.0", params: %{}}) | ||
assert_is_ready(context, "my_proj") | ||
assert_compiled(context, "my_proj") | ||
assert_notification "$/progress", %{"value" => %{"kind" => "end", "message" => "Finished indexing!"}} | ||
|
||
did_open(context.client, context.foo_path, context.foo) | ||
context | ||
end | ||
|
||
test "sends back a list of code actions", %{client: client, foo_path: foo} do | ||
foo_uri = uri(foo) | ||
id = 1 | ||
|
||
request client, %{ | ||
method: "textDocument/codeAction", | ||
id: id, | ||
jsonrpc: "2.0", | ||
params: %{ | ||
context: %{ | ||
"diagnostics" => [ | ||
%{ | ||
"data" => %{"namespace" => "elixir", "type" => "unused_variable"}, | ||
"message" => | ||
"variable \"foo\" is unused (if the variable is not meant to be used, prefix it with an underscore)", | ||
"range" => %{"end" => %{"character" => 999, "line" => 2}, "start" => %{"character" => 4, "line" => 2}}, | ||
"severity" => 2, | ||
"source" => "Elixir" | ||
} | ||
] | ||
}, | ||
range: %{start: %{line: 2, character: 4}, end: %{line: 2, character: 999}}, | ||
textDocument: %{uri: foo_uri} | ||
} | ||
} | ||
|
||
assert_receive %{ | ||
"jsonrpc" => "2.0", | ||
"id" => 1, | ||
"result" => [%{"edit" => %{"changes" => %{^foo_uri => [%{"newText" => "_"}]}}}] | ||
}, | ||
500 | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters