Skip to content

Commit

Permalink
fixup! feat: undefined function code action
Browse files Browse the repository at this point in the history
  • Loading branch information
mhanberg committed Apr 24, 2024
1 parent 227d462 commit 8d83073
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,44 +9,25 @@ defmodule NextLS.ElixirExtension.CodeAction.UndefinedFunction do
alias NextLS.ASTHelpers

def new(diagnostic, text, uri) do
%Diagnostic{range: range, data: %{"info" => info}} = diagnostic
%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),
indentation <- get_indent(text, defm) do
|> 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
}

%{
"name" => name,
"arity" => arity
} = info

params = if arity == "0", do: "", else: Enum.map_join(1..String.to_integer(arity), ", ", fn i -> "param#{i}" end)

new_text = """
#{indentation}defp #{name}(#{params}) do
#{indentation}end
"""

[
action = fn title, new_text ->
%CodeAction{
title: "Create local private function #{info["name"]}/#{info["arity"]}",
title: title,
diagnostics: [diagnostic],
edit: %WorkspaceEdit{
changes: %{
Expand All @@ -62,6 +43,21 @@ defmodule NextLS.ElixirExtension.CodeAction.UndefinedFunction do
}
}
}
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,30 @@ defmodule NextLS.ElixirExtension.UndefinedFunctionTest do

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 local private function bar/2"
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}

Expand All @@ -77,7 +97,7 @@ defmodule NextLS.ElixirExtension.UndefinedFunctionTest do
}
]
}
} = code_action.edit
} = private.edit
end

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

0 comments on commit 8d83073

Please sign in to comment.