Skip to content

Commit

Permalink
Handle completing quoted macros like ?'FOO BAR' (#1549)
Browse files Browse the repository at this point in the history
  • Loading branch information
plux authored Sep 26, 2024
1 parent eeec8ef commit bfafd78
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 6 deletions.
2 changes: 1 addition & 1 deletion apps/els_lsp/priv/code_navigation/src/code_navigation.erl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

-define(MACRO_A, macro_a).
-define(MACRO_A(X), erlang:display(X)).

-define('MACRO A', macro_a).
function_a() ->
function_b(),
#record_a{}.
Expand Down
26 changes: 22 additions & 4 deletions apps/els_lsp/src/els_completion_provider.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1610,9 +1610,27 @@ features() ->

-spec macro_label(atom() | {atom(), non_neg_integer()}) -> binary().
macro_label({Name, Arity}) ->
els_utils:to_binary(io_lib:format("~ts/~p", [Name, Arity]));
els_utils:to_binary(
io_lib:format(
"~ts/~p",
[macro_to_label(Name), Arity]
)
);
macro_label(Name) ->
atom_to_binary(Name, utf8).
macro_to_label(Name).

-spec macro_to_label(atom()) -> binary().
macro_to_label(Name) ->
%% Trick to ensure we can handle macros like ?'FOO BAR'.
Bin = atom_to_binary(Name, utf8),
LowerBin = string:lowercase(Bin),
LowerAtom = binary_to_atom(LowerBin, utf8),
case atom_to_label(LowerAtom) == LowerBin of
true ->
Bin;
false ->
atom_to_label(Name)
end.

-spec format_function(atom(), els_arg:args(), boolean(), els_poi:poi_kind()) -> binary().
format_function(Name, Args, SnippetSupport, Kind) ->
Expand All @@ -1624,10 +1642,10 @@ format_function(Name, Args, SnippetSupport, Kind) ->
boolean()
) -> binary().
format_macro({Name0, _Arity}, Args, SnippetSupport) ->
Name = atom_to_binary(Name0, utf8),
Name = macro_to_label(Name0),
format_args(Name, Args, SnippetSupport, define);
format_macro(Name, none, _SnippetSupport) ->
atom_to_binary(Name, utf8).
macro_to_label(Name).

-spec format_args(
binary(),
Expand Down
7 changes: 7 additions & 0 deletions apps/els_lsp/test/els_completion_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -945,6 +945,13 @@ macros(Config) ->
insertTextFormat => ?INSERT_TEXT_FORMAT_SNIPPET,
data => #{}
},
#{
kind => ?COMPLETION_ITEM_KIND_CONSTANT,
label => <<"'MACRO A'">>,
insertText => <<"'MACRO A'">>,
insertTextFormat => ?INSERT_TEXT_FORMAT_SNIPPET,
data => #{}
},
#{
kind => ?COMPLETION_ITEM_KIND_CONSTANT,
label => <<"MACRO_A/1">>,
Expand Down
3 changes: 2 additions & 1 deletion apps/els_lsp/test/els_document_symbol_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,8 @@ macros() ->
{<<"macro_A">>, {44, 8}, {44, 15}},
{<<"MACRO_B">>, {117, 8}, {117, 15}},
{<<"MACRO_A">>, {17, 8}, {17, 15}},
{<<"MACRO_A/1">>, {18, 8}, {18, 15}}
{<<"MACRO_A/1">>, {18, 8}, {18, 15}},
{<<"MACRO A">>, {19, 8}, {19, 17}}
].

records() ->
Expand Down

0 comments on commit bfafd78

Please sign in to comment.