Skip to content

Commit

Permalink
Add action for suggesting undefined function and modules
Browse files Browse the repository at this point in the history
  • Loading branch information
plux committed Oct 8, 2024
1 parent 04a32dd commit 0af7076
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
3 changes: 3 additions & 0 deletions apps/els_lsp/src/els_code_action_provider.erl
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ make_code_actions(
fun els_code_actions:fix_module_name/4},
{"Unused macro: (.*)", fun els_code_actions:remove_macro/4},
{"function (.*) undefined", fun els_code_actions:create_function/4},
{"function (.*) undefined", fun els_code_actions:suggest_function/4},
{"Cannot find definition for function (.*)", fun els_code_actions:suggest_function/4},
{"Cannot find module (.*)", fun els_code_actions:suggest_module/4},
{"Unused file: (.*)", fun els_code_actions:remove_unused/4},
{"Atom typo\\? Did you mean: (.*)", fun els_code_actions:fix_atom_typo/4},
{"undefined callback function (.*) \\\(behaviour '(.*)'\\\)",
Expand Down
48 changes: 48 additions & 0 deletions apps/els_lsp/src/els_code_actions.erl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
suggest_macro/4,
suggest_record/4,
suggest_record_field/4,
suggest_function/4,
suggest_module/4,
bump_variables/2
]).

Expand Down Expand Up @@ -345,6 +347,52 @@ suggest_record_field(Uri, Range, _Data, [Field, Record]) ->
Distance > 0.8
].

-spec suggest_function(uri(), range(), binary(), [binary()]) -> [map()].
suggest_function(Uri, Range, _Data, [FunBin]) ->
[ModNameBin, _ArityBin] = string:split(FunBin, <<"/">>),
{{ok, Document}, NameBin} =
case string:split(ModNameBin, <<":">>) of
[ModBin, NameBin0] ->
Mod = binary_to_atom(ModBin, utf8),
{ok, ModUri} = els_utils:find_module(Mod),
{els_utils:lookup_document(ModUri), NameBin0};
[NameBin0] ->
{els_utils:lookup_document(Uri), NameBin0}
end,
POIs = els_dt_document:pois(Document, [function]),
Funs = [atom_to_binary(F) || #{id := {F, _A}} <- POIs],
Distances =
[{els_utils:jaro_distance(F, NameBin), F} || F <- Funs, F =/= NameBin],
[
make_edit_action(
Uri,
<<"Did you mean ", F/binary, "?">>,
?CODE_ACTION_KIND_QUICKFIX,
F,
Range
)
|| {Distance, F} <- lists:reverse(lists:usort(Distances)),
Distance > 0.8
].

-spec suggest_module(uri(), range(), binary(), [binary()]) -> [map()].
suggest_module(Uri, Range, _Data, [NameBin]) ->
{ok, Items} = els_dt_document_index:find_by_kind(module),
Mods = [atom_to_binary(M) || #{id := M} <- Items],
Distances =
[{els_utils:jaro_distance(M, NameBin), M} || M <- Mods, M =/= NameBin],
[
make_edit_action(
Uri,
<<"Did you mean ", M/binary, "?">>,
?CODE_ACTION_KIND_QUICKFIX,
M,
Range
)
|| {Distance, M} <- lists:reverse(lists:usort(Distances)),
Distance > 0.8
].

-spec fix_module_name(uri(), range(), binary(), [binary()]) -> [map()].
fix_module_name(Uri, Range0, _Data, [ModName, FileName]) ->
{ok, Document} = els_utils:lookup_document(Uri),
Expand Down

0 comments on commit 0af7076

Please sign in to comment.