Skip to content

Commit

Permalink
Merge pull request erlang-ls#1036 from gomoripeti/macro_in_application
Browse files Browse the repository at this point in the history
Fix creating macro POIs in function applications
  • Loading branch information
robertoaloi authored Jun 11, 2021
2 parents 7e7202a + adbcbc9 commit cc02ca9
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

-define(USED_MACRO, used_macro).
-define(UNUSED_MACRO, unused_macro).
-define(MOD, module). %% MOD was incorrectly reported as unused (#1021)

main() ->
?MOD:foo(),
?USED_MACRO.
8 changes: 7 additions & 1 deletion apps/els_lsp/src/els_parser.erl
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,13 @@ fold2(_, S, []) ->

-spec subtrees(tree(), atom()) -> [[tree()]].
subtrees(Tree, application) ->
[erl_syntax:application_arguments(Tree)];
[ case application_mfa(Tree) of
undefined ->
[erl_syntax:application_operator(Tree)];
_ ->
[]
end
, erl_syntax:application_arguments(Tree)];
subtrees(Tree, function) ->
[erl_syntax:function_clauses(Tree)];
subtrees(_Tree, implicit_fun) ->
Expand Down
33 changes: 33 additions & 0 deletions apps/els_lsp/test/els_parser_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
, wild_attrbibute_macro/1
, type_name_macro/1
, spec_name_macro/1
, macro_in_application/1
, var_in_application/1
, unicode_clause_pattern/1
, latin1_source_code/1
]).
Expand Down Expand Up @@ -239,6 +241,37 @@ spec_name_macro(_Config) ->
%%?assertMatch([_], parse_find_pois(Text2, type_application, {t, 0})),
ok.

macro_in_application(_Config) ->
Text1 = "f() -> ?M:f(42).",
?assertMatch([#{id := 'M'}], parse_find_pois(Text1, macro)),

Text2 = "f() -> ?M(mod):f(42).",
?assertMatch([#{id := {'M', 1}}], parse_find_pois(Text2, macro)),

%% This is not an application, only a module qualifier before macro M/1
Text3 = "f() -> mod:?M(42).",
?assertMatch([#{id := {'M', 1}}], parse_find_pois(Text3, macro)),

%% Application with macro M/0 as function name
Text4 = "f() -> mod:?M()(42).",
?assertMatch([#{id := {'M', 0}}], parse_find_pois(Text4, macro)),

%% Known limitation of the current implementation,
%% ?MODULE is handled specially, converted to a local call
Text5 = "f() -> ?MODULE:foo().",
?assertMatch([], parse_find_pois(Text5, macro)),
?assertMatch([#{id := {foo, 0}}], parse_find_pois(Text5, application)),

ok.

var_in_application(_Config) ->
Text1 = "f() -> Mod:f(42).",
?assertMatch([#{id := 'Mod'}], parse_find_pois(Text1, variable)),

Text2 = "f() -> mod:Fun(42).",
?assertMatch([#{id := 'Fun'}], parse_find_pois(Text2, variable)),
ok.

-spec unicode_clause_pattern(config()) -> ok.
unicode_clause_pattern(_Config) ->
%% From OTP compiler's bs_utf_SUITE.erl
Expand Down
1 change: 1 addition & 0 deletions elvis.config
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
, els_definition_SUITE
, els_diagnostics_SUITE
, els_document_highlight_SUITE
, els_parser_SUITE
, els_references_SUITE
, els_methods
]}}
Expand Down

0 comments on commit cc02ca9

Please sign in to comment.