-
-
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: undefined function code action (#441)
When you have an undefined local function, this code action allows you to create a private function stub for the function.
- Loading branch information
Showing
5 changed files
with
261 additions
and
10 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
78 changes: 78 additions & 0 deletions
78
lib/next_ls/extensions/elixir_extension/code_action/undefined_function.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,78 @@ | ||
defmodule NextLS.ElixirExtension.CodeAction.UndefinedFunction do | ||
@moduledoc false | ||
|
||
alias GenLSP.Structures.CodeAction | ||
alias GenLSP.Structures.Diagnostic | ||
alias GenLSP.Structures.Range | ||
alias GenLSP.Structures.TextEdit | ||
alias GenLSP.Structures.WorkspaceEdit | ||
alias NextLS.ASTHelpers | ||
|
||
def new(diagnostic, text, uri) do | ||
%Diagnostic{range: range, data: %{"info" => %{"name" => name, "arity" => arity}}} = diagnostic | ||
|
||
with {:ok, ast} <- | ||
text | ||
|> Enum.join("\n") | ||
|> Spitfire.parse(literal_encoder: &{:ok, {:__block__, &2, [&1]}}), | ||
{:ok, {:defmodule, meta, _} = defm} <- ASTHelpers.get_surrounding_module(ast, range.start) do | ||
indentation = get_indent(text, defm) | ||
|
||
position = %GenLSP.Structures.Position{ | ||
line: meta[:end][:line] - 1, | ||
character: 0 | ||
} | ||
|
||
params = if arity == "0", do: "", else: Enum.map_join(1..String.to_integer(arity), ", ", fn i -> "param#{i}" end) | ||
|
||
action = fn title, new_text -> | ||
%CodeAction{ | ||
title: title, | ||
diagnostics: [diagnostic], | ||
edit: %WorkspaceEdit{ | ||
changes: %{ | ||
uri => [ | ||
%TextEdit{ | ||
new_text: new_text, | ||
range: %Range{ | ||
start: position, | ||
end: position | ||
} | ||
} | ||
] | ||
} | ||
} | ||
} | ||
end | ||
|
||
[ | ||
action.("Create public function #{name}/#{arity}", """ | ||
#{indentation}def #{name}(#{params}) do | ||
#{indentation}end | ||
"""), | ||
action.("Create private function #{name}/#{arity}", """ | ||
#{indentation}defp #{name}(#{params}) do | ||
#{indentation}end | ||
""") | ||
] | ||
end | ||
end | ||
|
||
@one_indentation_level " " | ||
@indent ~r/^(\s*).*/ | ||
defp get_indent(text, {_, defm_context, _}) do | ||
line = defm_context[:line] - 1 | ||
|
||
indent = | ||
text | ||
|> Enum.at(line) | ||
|> then(&Regex.run(@indent, &1)) | ||
|> List.last() | ||
|
||
indent <> @one_indentation_level | ||
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
170 changes: 170 additions & 0 deletions
170
test/next_ls/extensions/elixir_extension/code_action/undefined_function_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,170 @@ | ||
defmodule NextLS.ElixirExtension.UndefinedFunctionTest 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.UndefinedFunction | ||
|
||
test "in outer module creates new private function inside current module" do | ||
text = | ||
String.split( | ||
""" | ||
defmodule Test.Foo do | ||
defmodule Bar do | ||
def run() do | ||
:ok | ||
end | ||
end | ||
def hello() do | ||
bar(1, 2) | ||
end | ||
defmodule Baz do | ||
def run() do | ||
:error | ||
end | ||
end | ||
end | ||
""", | ||
"\n" | ||
) | ||
|
||
start = %Position{character: 4, line: 8} | ||
|
||
diagnostic = %GenLSP.Structures.Diagnostic{ | ||
data: %{ | ||
"namespace" => "elixir", | ||
"type" => "undefined-function", | ||
"info" => %{ | ||
"name" => "bar", | ||
"arity" => "2", | ||
"module" => "Elixir.Test.Foo" | ||
} | ||
}, | ||
message: | ||
"undefined function bar/2 (expected Test.Foo to define such a function or for it to be imported, but none are available)", | ||
source: "Elixir", | ||
range: %GenLSP.Structures.Range{ | ||
start: start, | ||
end: %{start | character: 6} | ||
} | ||
} | ||
|
||
uri = "file:///home/owner/my_project/hello.ex" | ||
|
||
assert [public, private] = UndefinedFunction.new(diagnostic, text, uri) | ||
assert [diagnostic] == public.diagnostics | ||
assert public.title == "Create public function bar/2" | ||
|
||
edit_position = %Position{line: 16, character: 0} | ||
|
||
assert %WorkspaceEdit{ | ||
changes: %{ | ||
^uri => [ | ||
%TextEdit{ | ||
new_text: """ | ||
def bar(param1, param2) do | ||
end | ||
""", | ||
range: %Range{start: ^edit_position, end: ^edit_position} | ||
} | ||
] | ||
} | ||
} = public.edit | ||
|
||
assert [diagnostic] == private.diagnostics | ||
assert private.title == "Create private function bar/2" | ||
|
||
edit_position = %Position{line: 16, character: 0} | ||
|
||
assert %WorkspaceEdit{ | ||
changes: %{ | ||
^uri => [ | ||
%TextEdit{ | ||
new_text: """ | ||
defp bar(param1, param2) do | ||
end | ||
""", | ||
range: %Range{start: ^edit_position, end: ^edit_position} | ||
} | ||
] | ||
} | ||
} = private.edit | ||
end | ||
|
||
test "in inner module creates new private function inside current module" do | ||
text = | ||
String.split( | ||
""" | ||
defmodule Test.Foo do | ||
defmodule Bar do | ||
def run() do | ||
bar(1, 2) | ||
end | ||
end | ||
defmodule Baz do | ||
def run() do | ||
:error | ||
end | ||
end | ||
end | ||
""", | ||
"\n" | ||
) | ||
|
||
start = %Position{character: 6, line: 3} | ||
|
||
diagnostic = %GenLSP.Structures.Diagnostic{ | ||
data: %{ | ||
"namespace" => "elixir", | ||
"type" => "undefined-function", | ||
"info" => %{ | ||
"name" => "bar", | ||
"arity" => "2", | ||
"module" => "Elixir.Test.Foo.Bar" | ||
} | ||
}, | ||
message: | ||
"undefined function bar/2 (expected Test.Foo to define such a function or for it to be imported, but none are available)", | ||
source: "Elixir", | ||
range: %GenLSP.Structures.Range{ | ||
start: start, | ||
end: %{start | character: 9} | ||
} | ||
} | ||
|
||
uri = "file:///home/owner/my_project/hello.ex" | ||
|
||
assert [_, code_action] = UndefinedFunction.new(diagnostic, text, uri) | ||
assert %CodeAction{} = code_action | ||
assert [diagnostic] == code_action.diagnostics | ||
assert code_action.title == "Create private function bar/2" | ||
|
||
edit_position = %Position{line: 5, character: 0} | ||
|
||
assert %WorkspaceEdit{ | ||
changes: %{ | ||
^uri => [ | ||
%TextEdit{ | ||
new_text: """ | ||
defp bar(param1, param2) do | ||
end | ||
""", | ||
range: %Range{start: ^edit_position, end: ^edit_position} | ||
} | ||
] | ||
} | ||
} = code_action.edit | ||
end | ||
end |