Skip to content
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: add code action for the ParenthesesOnZeroArityDefs check #76

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading