Skip to content

Commit 8d83073

Browse files
committed
fixup! feat: undefined function code action
1 parent 227d462 commit 8d83073

File tree

2 files changed

+47
-31
lines changed

2 files changed

+47
-31
lines changed

lib/next_ls/extensions/elixir_extension/code_action/undefined_function.ex

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,44 +9,25 @@ defmodule NextLS.ElixirExtension.CodeAction.UndefinedFunction do
99
alias NextLS.ASTHelpers
1010

1111
def new(diagnostic, text, uri) do
12-
%Diagnostic{range: range, data: %{"info" => info}} = diagnostic
12+
%Diagnostic{range: range, data: %{"info" => %{"name" => name, "arity" => arity}}} = diagnostic
1313

1414
with {:ok, ast} <-
1515
text
1616
|> Enum.join("\n")
17-
|> Spitfire.parse(
18-
literal_encoder:
19-
&{:ok,
20-
{
21-
:__block__,
22-
&2,
23-
[&1]
24-
}}
25-
),
26-
{:ok, {:defmodule, meta, _} = defm} <- ASTHelpers.get_surrounding_module(ast, range.start),
27-
indentation <- get_indent(text, defm) do
17+
|> Spitfire.parse(literal_encoder: &{:ok, {:__block__, &2, [&1]}}),
18+
{:ok, {:defmodule, meta, _} = defm} <- ASTHelpers.get_surrounding_module(ast, range.start) do
19+
indentation = get_indent(text, defm)
20+
2821
position = %GenLSP.Structures.Position{
2922
line: meta[:end][:line] - 1,
3023
character: 0
3124
}
3225

33-
%{
34-
"name" => name,
35-
"arity" => arity
36-
} = info
37-
3826
params = if arity == "0", do: "", else: Enum.map_join(1..String.to_integer(arity), ", ", fn i -> "param#{i}" end)
3927

40-
new_text = """
41-
42-
#{indentation}defp #{name}(#{params}) do
43-
44-
#{indentation}end
45-
"""
46-
47-
[
28+
action = fn title, new_text ->
4829
%CodeAction{
49-
title: "Create local private function #{info["name"]}/#{info["arity"]}",
30+
title: title,
5031
diagnostics: [diagnostic],
5132
edit: %WorkspaceEdit{
5233
changes: %{
@@ -62,6 +43,21 @@ defmodule NextLS.ElixirExtension.CodeAction.UndefinedFunction do
6243
}
6344
}
6445
}
46+
end
47+
48+
[
49+
action.("Create public function #{name}/#{arity}", """
50+
51+
#{indentation}def #{name}(#{params}) do
52+
53+
#{indentation}end
54+
"""),
55+
action.("Create private function #{name}/#{arity}", """
56+
57+
#{indentation}defp #{name}(#{params}) do
58+
59+
#{indentation}end
60+
""")
6561
]
6662
end
6763
end

test/next_ls/extensions/elixir_extension/code_action/undefined_function_test.exs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,30 @@ defmodule NextLS.ElixirExtension.UndefinedFunctionTest do
5656

5757
uri = "file:///home/owner/my_project/hello.ex"
5858

59-
assert [code_action] = UndefinedFunction.new(diagnostic, text, uri)
60-
assert %CodeAction{} = code_action
61-
assert [diagnostic] == code_action.diagnostics
62-
assert code_action.title == "Create local private function bar/2"
59+
assert [public, private] = UndefinedFunction.new(diagnostic, text, uri)
60+
assert [diagnostic] == public.diagnostics
61+
assert public.title == "Create public function bar/2"
62+
63+
edit_position = %Position{line: 16, character: 0}
64+
65+
assert %WorkspaceEdit{
66+
changes: %{
67+
^uri => [
68+
%TextEdit{
69+
new_text: """
70+
71+
def bar(param1, param2) do
72+
73+
end
74+
""",
75+
range: %Range{start: ^edit_position, end: ^edit_position}
76+
}
77+
]
78+
}
79+
} = public.edit
80+
81+
assert [diagnostic] == private.diagnostics
82+
assert private.title == "Create private function bar/2"
6383

6484
edit_position = %Position{line: 16, character: 0}
6585

@@ -77,7 +97,7 @@ defmodule NextLS.ElixirExtension.UndefinedFunctionTest do
7797
}
7898
]
7999
}
80-
} = code_action.edit
100+
} = private.edit
81101
end
82102

83103
test "in inner module creates new private function inside current module" do

0 commit comments

Comments
 (0)