Skip to content

Commit fbb587a

Browse files
committed
Handle completing quoted macros like ?'FOO BAR'
1 parent e1d04e2 commit fbb587a

File tree

4 files changed

+32
-6
lines changed

4 files changed

+32
-6
lines changed

apps/els_lsp/priv/code_navigation/src/code_navigation.erl

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
-define(MACRO_A, macro_a).
1919
-define(MACRO_A(X), erlang:display(X)).
20-
20+
-define('MACRO A', macro_a).
2121
function_a() ->
2222
function_b(),
2323
#record_a{}.

apps/els_lsp/src/els_completion_provider.erl

+22-4
Original file line numberDiff line numberDiff line change
@@ -1610,9 +1610,27 @@ features() ->
16101610

16111611
-spec macro_label(atom() | {atom(), non_neg_integer()}) -> binary().
16121612
macro_label({Name, Arity}) ->
1613-
els_utils:to_binary(io_lib:format("~ts/~p", [Name, Arity]));
1613+
els_utils:to_binary(
1614+
io_lib:format(
1615+
"~ts/~p",
1616+
[macro_to_label(Name), Arity]
1617+
)
1618+
);
16141619
macro_label(Name) ->
1615-
atom_to_binary(Name, utf8).
1620+
macro_to_label(Name).
1621+
1622+
-spec macro_to_label(atom()) -> binary().
1623+
macro_to_label(Name) ->
1624+
%% Trick to ensure we can handle macros like ?'FOO BAR'.
1625+
Bin = atom_to_binary(Name, utf8),
1626+
LowerBin = string:lowercase(Bin),
1627+
LowerAtom = binary_to_atom(LowerBin, utf8),
1628+
case atom_to_label(LowerAtom) == LowerBin of
1629+
true ->
1630+
Bin;
1631+
false ->
1632+
atom_to_label(Name)
1633+
end.
16161634

16171635
-spec format_function(atom(), els_arg:args(), boolean(), els_poi:poi_kind()) -> binary().
16181636
format_function(Name, Args, SnippetSupport, Kind) ->
@@ -1624,10 +1642,10 @@ format_function(Name, Args, SnippetSupport, Kind) ->
16241642
boolean()
16251643
) -> binary().
16261644
format_macro({Name0, _Arity}, Args, SnippetSupport) ->
1627-
Name = atom_to_binary(Name0, utf8),
1645+
Name = macro_to_label(Name0),
16281646
format_args(Name, Args, SnippetSupport, define);
16291647
format_macro(Name, none, _SnippetSupport) ->
1630-
atom_to_binary(Name, utf8).
1648+
macro_to_label(Name).
16311649

16321650
-spec format_args(
16331651
binary(),

apps/els_lsp/test/els_completion_SUITE.erl

+7
Original file line numberDiff line numberDiff line change
@@ -945,6 +945,13 @@ macros(Config) ->
945945
insertTextFormat => ?INSERT_TEXT_FORMAT_SNIPPET,
946946
data => #{}
947947
},
948+
#{
949+
kind => ?COMPLETION_ITEM_KIND_CONSTANT,
950+
label => <<"'MACRO A'">>,
951+
insertText => <<"'MACRO A'">>,
952+
insertTextFormat => ?INSERT_TEXT_FORMAT_SNIPPET,
953+
data => #{}
954+
},
948955
#{
949956
kind => ?COMPLETION_ITEM_KIND_CONSTANT,
950957
label => <<"MACRO_A/1">>,

apps/els_lsp/test/els_document_symbol_SUITE.erl

+2-1
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,8 @@ macros() ->
182182
{<<"macro_A">>, {44, 8}, {44, 15}},
183183
{<<"MACRO_B">>, {117, 8}, {117, 15}},
184184
{<<"MACRO_A">>, {17, 8}, {17, 15}},
185-
{<<"MACRO_A/1">>, {18, 8}, {18, 15}}
185+
{<<"MACRO_A/1">>, {18, 8}, {18, 15}},
186+
{<<"MACRO A">>, {19, 8}, {19, 17}}
186187
].
187188

188189
records() ->

0 commit comments

Comments
 (0)