Skip to content

Commit 2bda865

Browse files
authored
Add action for suggesting undefined function and modules (#1560)
1 parent 04a32dd commit 2bda865

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

apps/els_lsp/src/els_code_action_provider.erl

+3
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ make_code_actions(
5959
fun els_code_actions:fix_module_name/4},
6060
{"Unused macro: (.*)", fun els_code_actions:remove_macro/4},
6161
{"function (.*) undefined", fun els_code_actions:create_function/4},
62+
{"function (.*) undefined", fun els_code_actions:suggest_function/4},
63+
{"Cannot find definition for function (.*)", fun els_code_actions:suggest_function/4},
64+
{"Cannot find module (.*)", fun els_code_actions:suggest_module/4},
6265
{"Unused file: (.*)", fun els_code_actions:remove_unused/4},
6366
{"Atom typo\\? Did you mean: (.*)", fun els_code_actions:fix_atom_typo/4},
6467
{"undefined callback function (.*) \\\(behaviour '(.*)'\\\)",

apps/els_lsp/src/els_code_actions.erl

+48
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
suggest_macro/4,
1818
suggest_record/4,
1919
suggest_record_field/4,
20+
suggest_function/4,
21+
suggest_module/4,
2022
bump_variables/2
2123
]).
2224

@@ -345,6 +347,52 @@ suggest_record_field(Uri, Range, _Data, [Field, Record]) ->
345347
Distance > 0.8
346348
].
347349

350+
-spec suggest_function(uri(), range(), binary(), [binary()]) -> [map()].
351+
suggest_function(Uri, Range, _Data, [FunBin]) ->
352+
[ModNameBin, _ArityBin] = string:split(FunBin, <<"/">>),
353+
{{ok, Document}, NameBin} =
354+
case string:split(ModNameBin, <<":">>) of
355+
[ModBin, NameBin0] ->
356+
Mod = binary_to_atom(ModBin, utf8),
357+
{ok, ModUri} = els_utils:find_module(Mod),
358+
{els_utils:lookup_document(ModUri), NameBin0};
359+
[NameBin0] ->
360+
{els_utils:lookup_document(Uri), NameBin0}
361+
end,
362+
POIs = els_dt_document:pois(Document, [function]),
363+
Funs = [atom_to_binary(F) || #{id := {F, _A}} <- POIs],
364+
Distances =
365+
[{els_utils:jaro_distance(F, NameBin), F} || F <- Funs, F =/= NameBin],
366+
[
367+
make_edit_action(
368+
Uri,
369+
<<"Did you mean ", F/binary, "?">>,
370+
?CODE_ACTION_KIND_QUICKFIX,
371+
F,
372+
Range
373+
)
374+
|| {Distance, F} <- lists:reverse(lists:usort(Distances)),
375+
Distance > 0.8
376+
].
377+
378+
-spec suggest_module(uri(), range(), binary(), [binary()]) -> [map()].
379+
suggest_module(Uri, Range, _Data, [NameBin]) ->
380+
{ok, Items} = els_dt_document_index:find_by_kind(module),
381+
Mods = [atom_to_binary(M) || #{id := M} <- Items],
382+
Distances =
383+
[{els_utils:jaro_distance(M, NameBin), M} || M <- Mods, M =/= NameBin],
384+
[
385+
make_edit_action(
386+
Uri,
387+
<<"Did you mean ", M/binary, "?">>,
388+
?CODE_ACTION_KIND_QUICKFIX,
389+
M,
390+
Range
391+
)
392+
|| {Distance, M} <- lists:reverse(lists:usort(Distances)),
393+
Distance > 0.8
394+
].
395+
348396
-spec fix_module_name(uri(), range(), binary(), [binary()]) -> [map()].
349397
fix_module_name(Uri, Range0, _Data, [ModName, FileName]) ->
350398
{ok, Document} = els_utils:lookup_document(Uri),

0 commit comments

Comments
 (0)