Skip to content

Commit

Permalink
Improve create function code action
Browse files Browse the repository at this point in the history
The create function code action will now use the argument names used in the
function call.

It will also try to indent correctly by guessing indentation amount by analyzing
the source file.
  • Loading branch information
plux committed Jan 18, 2024
1 parent b29ef4a commit 53e6a85
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 9 deletions.
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(), pos_integer()) -> pos_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

0 comments on commit 53e6a85

Please sign in to comment.