-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add code action for the ParenthesesOnZeroArityDefs check (#76)
* feat: Add code action for the ParenthesesOnZeroArityDefs check --------- Co-authored-by: Mitchell Hanberg <[email protected]>
- Loading branch information
Showing
3 changed files
with
194 additions
and
23 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
56 changes: 56 additions & 0 deletions
56
lib/credo_language_server/code_action/parentheses_on_zero_arity_defs.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,56 @@ | ||
defmodule CredoLanguageServer.CodeAction.ParenthesesOnZeroArityDefs do | ||
@moduledoc false | ||
|
||
alias GenLSP.Structures.{ | ||
CodeAction, | ||
Diagnostic, | ||
Position, | ||
Range, | ||
TextEdit, | ||
WorkspaceEdit | ||
} | ||
|
||
require Logger | ||
|
||
defp opts do | ||
Schematic.map(%{ | ||
# TODO: schematic needs a way to define a struct | ||
diagnostic: Schematic.any(), | ||
uri: Schematic.str(), | ||
text: Schematic.list(Schematic.str()) | ||
}) | ||
end | ||
|
||
def new(opts) do | ||
{:ok, | ||
%{ | ||
diagnostic: %Diagnostic{range: %{start: start}} = diagnostic, | ||
uri: uri, | ||
text: text | ||
}} = Schematic.unify(opts(), Map.new(opts)) | ||
|
||
function_definition = Enum.at(text, start.line) | ||
new_text = String.replace(function_definition, "()", "", global: false) | ||
|
||
%CodeAction{ | ||
title: "Remove parentheses", | ||
diagnostics: [diagnostic], | ||
edit: %WorkspaceEdit{ | ||
changes: %{ | ||
uri => [ | ||
%TextEdit{ | ||
new_text: new_text, | ||
range: %Range{ | ||
start: start, | ||
end: %Position{ | ||
start | ||
| character: String.length(function_definition) | ||
} | ||
} | ||
} | ||
] | ||
} | ||
} | ||
} | ||
end | ||
end |
104 changes: 104 additions & 0 deletions
104
test/credo_language_server/code_action/parentheses_on_zero_arity_defs_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,104 @@ | ||
defmodule CredoLanguageServer.CodeAction.ParenthesesOnZeroArityDefsTest do | ||
use ExUnit.Case, async: true | ||
|
||
alias CredoLanguageServer.CodeAction.ParenthesesOnZeroArityDefs | ||
|
||
alias GenLSP.Structures.{ | ||
CodeAction, | ||
CodeDescription, | ||
Diagnostic, | ||
Position, | ||
Range, | ||
TextEdit, | ||
WorkspaceEdit | ||
} | ||
|
||
describe "new" do | ||
test "provides a code action that removes unnecessary parentheses" do | ||
diagnostic = %Diagnostic{ | ||
data: %{ | ||
"check" => | ||
"Elixir.Credo.Check.Readability.ParenthesesOnZeroArityDefs", | ||
"file" => "foo.ex" | ||
}, | ||
related_information: nil, | ||
tags: nil, | ||
message: | ||
"Do not use parentheses when defining a function which has no arguments.", | ||
source: "credo", | ||
code_description: %CodeDescription{ | ||
href: | ||
"https://hexdocs.pm/credo/Credo.Check.Readability.ParenthesesOnZeroArityDefs.html" | ||
}, | ||
code: "Credo.Check.Readability.ParenthesesOnZeroArityDefs", | ||
severity: 3, | ||
range: %Range{ | ||
start: %Position{character: 0, line: 1}, | ||
end: %Position{character: 1, line: 1} | ||
} | ||
} | ||
|
||
text = [ | ||
"defmodule Test do", | ||
" def foo() do", | ||
" :bar", | ||
" end", | ||
"end", | ||
"" | ||
] | ||
|
||
assert %CodeAction{ | ||
title: "Remove parentheses", | ||
diagnostics: [^diagnostic], | ||
edit: %WorkspaceEdit{ | ||
changes: %{ | ||
"uri" => [ | ||
%TextEdit{ | ||
new_text: " def foo do", | ||
range: %Range{ | ||
start: %Position{character: 0, line: 1}, | ||
end: %Position{character: 14, line: 1} | ||
} | ||
} | ||
] | ||
} | ||
} | ||
} = | ||
ParenthesesOnZeroArityDefs.new(%{ | ||
diagnostic: diagnostic, | ||
text: text, | ||
uri: "uri" | ||
}) | ||
|
||
text = [ | ||
"defmodule Test do", | ||
" def foo(), do: bar()", | ||
"end", | ||
"" | ||
] | ||
|
||
assert %CodeAction{ | ||
title: "Remove parentheses", | ||
diagnostics: [^diagnostic], | ||
edit: %WorkspaceEdit{ | ||
changes: %{ | ||
"uri" => [ | ||
%TextEdit{ | ||
new_text: " def foo, do: bar()", | ||
range: %Range{ | ||
start: %Position{character: 0, line: 1}, | ||
end: %Position{character: 22, line: 1} | ||
} | ||
} | ||
] | ||
} | ||
} | ||
} = | ||
ParenthesesOnZeroArityDefs.new(%{ | ||
diagnostic: diagnostic, | ||
text: text, | ||
uri: "uri" | ||
}) | ||
end | ||
end | ||
end |