Skip to content

Commit

Permalink
feat: add code action for the ParenthesesOnZeroArityDefs check (#76)
Browse files Browse the repository at this point in the history
* feat: Add code action for the ParenthesesOnZeroArityDefs check

---------

Co-authored-by: Mitchell Hanberg <[email protected]>
  • Loading branch information
sineed and mhanberg authored Jul 28, 2023
1 parent ce08431 commit f846c9d
Show file tree
Hide file tree
Showing 3 changed files with 194 additions and 23 deletions.
57 changes: 34 additions & 23 deletions lib/credo_language_server/check.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ defmodule CredoLanguageServer.Check do
Data structure for Credo Checks.
"""

alias Credo.Check.Readability.{
ModuleDoc,
ParenthesesOnZeroArityDefs
}

@doc """
Data structure that holds information related to an instance of a check found by Credo.
"""
Expand All @@ -16,39 +21,45 @@ defmodule CredoLanguageServer.Check do
defimpl CredoLanguageServer.CodeActionable do
alias CredoLanguageServer.CodeAction

def fetch(%{
check: Credo.Check.Readability.ModuleDoc = check,
diagnostic: diagnostic,
uri: uri,
document: document
}) do
def fetch(%{check: ModuleDoc} = ca) do
[
CodeAction.DisableCheck.new(
uri: uri,
diagnostic: diagnostic,
text: document,
check: Macro.to_string(check)
uri: ca.uri,
diagnostic: ca.diagnostic,
text: ca.document,
check: Macro.to_string(ca.check)
),
CodeAction.ModuleDocFalse.new(
uri: uri,
diagnostic: diagnostic,
text: document
uri: ca.uri,
diagnostic: ca.diagnostic,
text: ca.document
)
]
end

def fetch(%{check: ParenthesesOnZeroArityDefs} = ca) do
[
CodeAction.DisableCheck.new(
uri: ca.uri,
diagnostic: ca.diagnostic,
text: ca.document,
check: Macro.to_string(ca.check)
),
CodeAction.ParenthesesOnZeroArityDefs.new(
uri: ca.uri,
diagnostic: ca.diagnostic,
text: ca.document
)
]
end

def fetch(%{
check: check,
diagnostic: diagnostic,
uri: uri,
document: document
}) do
def fetch(ca) do
[
CodeAction.DisableCheck.new(
uri: uri,
diagnostic: diagnostic,
text: document,
check: Macro.to_string(check)
uri: ca.uri,
diagnostic: ca.diagnostic,
text: ca.document,
check: Macro.to_string(ca.check)
)
]
end
Expand Down
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
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

0 comments on commit f846c9d

Please sign in to comment.