-
-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: unused variable code action #349
Merged
mhanberg
merged 10 commits into
elixir-tools:main
from
NJichev:feat-code-action-unused-var
Feb 21, 2024
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
93b75b0
feat: unused variable code action
NJichev 0bf4219
Update to use dynamic dispatch based on namespace
NJichev 26070ca
Add a working version of the code action
NJichev a755efd
Refactor code, add integration tests
NJichev c84a9aa
Refactor aliasing
NJichev 8f4eef8
Fix tests
NJichev 1c3033d
Fix formatting
NJichev 74b9b4f
Fix dialyzer error
NJichev 09314d1
Fix formatting again
NJichev 5c49e0e
review changes
mhanberg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,7 +44,8 @@ defmodule NextLS.ElixirExtension do | |
severity: severity(d.severity), | ||
message: IO.iodata_to_binary(d.message), | ||
source: d.compiler_name, | ||
range: range(d.position, Map.get(d, :span)) | ||
range: range(d.position, Map.get(d, :span)), | ||
data: metadata(d) | ||
}) | ||
end | ||
|
||
|
@@ -111,4 +112,17 @@ defmodule NextLS.ElixirExtension do | |
end | ||
|
||
def clamp(line), do: max(line, 0) | ||
|
||
@unused_variable ~r/variable\s\"[^\"]+\"\sis\sunused/ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if elixir core would consider tagging diagnostics with IDs, like EX1 means "unused variable" 🤔 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That would be dope |
||
defp metadata(diagnostic) do | ||
base = %{"namespace" => "elixir"} | ||
|
||
cond do | ||
is_binary(diagnostic.message) and diagnostic.message =~ @unused_variable -> | ||
Map.put(base, "type", "unused_variable") | ||
|
||
true -> | ||
base | ||
end | ||
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I can remove this in favour of just using schematic