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

Improve create function code action #1487

Merged
merged 1 commit into from
Jan 19, 2024
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
1 change: 1 addition & 0 deletions apps/els_lsp/priv/code_navigation/src/code_action.erl
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ function_c() ->
function_d() ->
foobar(),
foobar(x,y,z),
foobar(Foo, #foo_bar{}, Bar = 123, #foo_bar{} = Baz),
ok.
53 changes: 50 additions & 3 deletions apps/els_lsp/src/els_code_actions.erl
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@

-spec create_function(uri(), range(), binary(), [binary()]) -> [map()].
create_function(Uri, Range0, _Data, [UndefinedFun]) ->
{ok, Document} = els_utils:lookup_document(Uri),
{ok, #{text := Text} = Document} = els_utils:lookup_document(Uri),
Range = els_range:to_poi_range(Range0),
Indent = guess_indentation(string:lexemes(Text, "\n")),
IndentStr = lists:duplicate(Indent, 32),
FunPOIs = els_dt_document:pois(Document, [function]),
%% Figure out which function the error was found in, as we want to
%% create the function right after the current function.
Expand All @@ -32,8 +34,11 @@ create_function(Uri, Range0, _Data, [UndefinedFun]) ->
[#{to := {Line, _}} | _] ->
[Name, ArityBin] = string:split(UndefinedFun, "/"),
Arity = binary_to_integer(ArityBin),
Args = string:join(lists:duplicate(Arity, "_"), ", "),
SpecAndFun = io_lib:format("~s(~s) ->\n ok.\n\n", [Name, Args]),
Args = format_args(Document, Arity, Range),
SpecAndFun = io_lib:format(
"~s(~s) ->\n~sok.\n\n",
[Name, Args, IndentStr]
),
[
make_edit_action(
Uri,
Expand Down Expand Up @@ -244,3 +249,45 @@ make_edit_action(Uri, Title, Kind, Text, Range) ->
-spec edit(uri(), binary(), range()) -> workspace_edit().
edit(Uri, Text, Range) ->
#{changes => #{Uri => [#{newText => Text, range => Range}]}}.

-spec format_args(
els_dt_document:item(),
non_neg_integer(),
els_poi:poi_range()
) -> string().
format_args(Document, Arity, Range) ->
%% Find the matching function application and extract
%% argument names from it.
AppPOIs = els_dt_document:pois(Document, [application]),
Matches = [
POI
|| #{range := R} = POI <- AppPOIs,
els_range:in(R, Range)
],
case Matches of
[#{data := #{args := Args0}} | _] ->
string:join([A || {_N, A} <- Args0], ", ");
[] ->
string:join(lists:duplicate(Arity, "_"), ", ")
end.

-spec guess_indentation([binary()]) -> pos_integer().
guess_indentation([]) ->
2;
guess_indentation([A, B | Rest]) ->
ACount = count_leading_spaces(A, 0),
BCount = count_leading_spaces(B, 0),
case {ACount, BCount} of
{0, N} when N > 0 ->
N;
{_, _} ->
guess_indentation([B | Rest])
end.

-spec count_leading_spaces(binary(), non_neg_integer()) -> non_neg_integer().
count_leading_spaces(<<>>, _Acc) ->
0;
count_leading_spaces(<<" ", Rest/binary>>, Acc) ->
count_leading_spaces(Rest, 1 + Acc);
count_leading_spaces(<<_:8, _/binary>>, Acc) ->
Acc.
45 changes: 39 additions & 6 deletions apps/els_lsp/src/els_parser.erl
Original file line number Diff line number Diff line change
Expand Up @@ -290,9 +290,12 @@ application(Tree) ->
Pos = erl_syntax:get_pos(erl_syntax:application_operator(Tree)),
case erl_internal:bif(F, A) of
%% Call to a function from the `erlang` module
true -> [poi(Pos, application, {erlang, F, A}, #{imported => true})];
true ->
[poi(Pos, application, {erlang, F, A}, #{imported => true})];
%% Local call
false -> [poi(Pos, application, {F, A})]
false ->
Args = erl_syntax:application_arguments(Tree),
[poi(Pos, application, {F, A}, #{args => args_from_subtrees(Args)})]
end;
{{ModType, M}, {FunType, F}, A} ->
ModFunTree = erl_syntax:application_operator(Tree),
Expand Down Expand Up @@ -719,14 +722,44 @@ function_args(Clause) ->
args_from_subtrees(Trees) ->
Arity = length(Trees),
[
case erl_syntax:type(T) of
%% TODO: Handle literals
variable -> {N, erl_syntax:variable_literal(T)};
_ -> {N, "Arg" ++ integer_to_list(N)}
case extract_variable(T) of
{true, Variable} ->
{N, Variable};
false ->
{N, "Arg" ++ integer_to_list(N)}
end
|| {N, T} <- lists:zip(lists:seq(1, Arity), Trees)
].

-spec extract_variable(tree()) -> {true, string()} | false.
extract_variable(T) ->
case erl_syntax:type(T) of
%% TODO: Handle literals
variable ->
{true, erl_syntax:variable_literal(T)};
match_expr ->
Body = erl_syntax:match_expr_body(T),
Pattern = erl_syntax:match_expr_pattern(T),
case {extract_variable(Pattern), extract_variable(Body)} of
{false, Result} ->
Result;
{Result, _} ->
Result
end;
record_expr ->
RecordNode = erl_syntax:record_expr_type(T),
case erl_syntax:type(RecordNode) of
atom ->
NameAtom = erl_syntax:atom_value(RecordNode),
NameBin = els_utils:camel_case(atom_to_binary(NameAtom, utf8)),
{true, unicode:characters_to_list(NameBin)};
_ ->
false
end;
_Type ->
false
end.

-spec implicit_fun(tree()) -> [els_poi:poi()].
implicit_fun(Tree) ->
FunSpec =
Expand Down
60 changes: 51 additions & 9 deletions apps/els_lsp/test/els_code_action_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
remove_unused_import/1,
create_undefined_function/1,
create_undefined_function_arity/1,
create_undefined_function_variable_names/1,
fix_callbacks/1
]).

Expand Down Expand Up @@ -313,8 +314,8 @@ remove_unused_import(Config) ->
create_undefined_function(Config) ->
Uri = ?config(code_action_uri, Config),
Range = els_protocol:range(#{
from => {23, 2},
to => {23, 8}
from => {23, 3},
to => {23, 9}
}),
Diag = #{
message => <<"function foobar/0 undefined">>,
Expand All @@ -334,8 +335,8 @@ create_undefined_function(Config) ->
#{
range =>
els_protocol:range(#{
from => {27, 1},
to => {27, 1}
from => {28, 1},
to => {28, 1}
}),
newText =>
<<"foobar() ->\n ok.\n\n">>
Expand All @@ -354,8 +355,8 @@ create_undefined_function(Config) ->
create_undefined_function_arity(Config) ->
Uri = ?config(code_action_uri, Config),
Range = els_protocol:range(#{
from => {24, 2},
to => {24, 8}
from => {24, 3},
to => {24, 9}
}),
Diag = #{
message => <<"function foobar/3 undefined">>,
Expand All @@ -375,11 +376,11 @@ create_undefined_function_arity(Config) ->
#{
range =>
els_protocol:range(#{
from => {27, 1},
to => {27, 1}
from => {28, 1},
to => {28, 1}
}),
newText =>
<<"foobar(_, _, _) ->\n ok.\n\n">>
<<"foobar(Arg1, Arg2, Arg3) ->\n ok.\n\n">>
}
]
}
Expand All @@ -391,6 +392,47 @@ create_undefined_function_arity(Config) ->
?assertEqual(Expected, Result),
ok.

-spec create_undefined_function_variable_names((config())) -> ok.
create_undefined_function_variable_names(Config) ->
Uri = ?config(code_action_uri, Config),
Range = els_protocol:range(#{
from => {25, 3},
to => {25, 9}
}),
Diag = #{
message => <<"function foobar/5 undefined">>,
range => Range,
severity => 2,
source => <<"">>
},
#{result := Result} = els_client:document_codeaction(Uri, Range, [Diag]),
Expected =
[
#{
edit => #{
changes =>
#{
binary_to_atom(Uri, utf8) =>
[
#{
range =>
els_protocol:range(#{
from => {28, 1},
to => {28, 1}
}),
newText =>
<<"foobar(Foo, FooBar, Bar, FooBar) ->\n ok.\n\n">>
}
]
}
},
kind => <<"quickfix">>,
title => <<"Create function foobar/5">>
}
],
?assertEqual(Expected, Result),
ok.

-spec fix_callbacks(config()) -> ok.
fix_callbacks(Config) ->
Uri = ?config(code_action_uri, Config),
Expand Down
Loading