Skip to content

Commit

Permalink
[#1391] Add support for record index in els_parser (#1392)
Browse files Browse the repository at this point in the history
* [#1391] Add support for record index in els_parser

* Fix typo in test suite
  • Loading branch information
plux authored Nov 7, 2022
1 parent b151f9d commit c6a8a5e
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 4 deletions.
38 changes: 38 additions & 0 deletions apps/els_lsp/src/els_parser.erl
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ do_points_of_interest(Tree) ->
macro(Tree);
record_access ->
record_access(Tree);
record_index_expr ->
record_index_expr(Tree);
record_expr ->
record_expr(Tree);
variable ->
Expand Down Expand Up @@ -796,6 +798,29 @@ record_access_pois(Tree, Record) ->
| FieldPoi
].

-spec record_index_expr(tree()) -> [els_poi:poi()].
record_index_expr(Tree) ->
RecordNode = erl_syntax:record_index_expr_type(Tree),
case is_record_name(RecordNode) of
{true, Record} ->
record_index_expr_pois(Tree, Record);
false ->
[]
end.

-spec record_index_expr_pois(tree(), atom()) -> [els_poi:poi()].
record_index_expr_pois(Tree, Record) ->
FieldNode = erl_syntax:record_index_expr_field(Tree),
FieldPoi =
case is_atom_node(FieldNode) of
{true, FieldName} ->
[poi(erl_syntax:get_pos(FieldNode), record_field, {Record, FieldName})];
_ ->
[]
end,
Anno = record_index_expr_location(Tree),
[poi(Anno, record_expr, Record) | FieldPoi].

-spec record_expr(tree()) -> [els_poi:poi()].
record_expr(Tree) ->
RecordNode = erl_syntax:record_expr_type(Tree),
Expand Down Expand Up @@ -1067,6 +1092,13 @@ subtrees(Tree, record_access) ->
skip_record_name_atom(NameNode),
skip_name_atom(FieldNode)
];
subtrees(Tree, record_index_expr) ->
NameNode = erl_syntax:record_index_expr_type(Tree),
FieldNode = erl_syntax:record_index_expr_field(Tree),
[
skip_record_name_atom(NameNode),
skip_name_atom(FieldNode)
];
subtrees(Tree, record_expr) ->
NameNode = erl_syntax:record_expr_type(Tree),
Fields = erl_syntax:record_expr_fields(Tree),
Expand Down Expand Up @@ -1298,6 +1330,12 @@ record_access_location(Tree) ->
Anno = erl_syntax:get_pos(erl_syntax:record_access_type(Tree)),
erl_anno:set_location(Start, Anno).

-spec record_index_expr_location(tree()) -> erl_anno:anno().
record_index_expr_location(Tree) ->
Start = get_start_location(Tree),
Anno = erl_syntax:get_pos(erl_syntax:record_index_expr_type(Tree)),
erl_anno:set_location(Start, Anno).

-spec record_expr_location(tree(), tree()) -> erl_anno:anno().
record_expr_location(Tree, RecordName) ->
%% set start location at '#'
Expand Down
17 changes: 13 additions & 4 deletions apps/els_lsp/test/els_parser_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
types_with_types/1,
record_def_with_types/1,
record_def_with_record_type/1,
record_index/1,
callback_recursive/1,
specs_recursive/1,
types_recursive/1,
Expand Down Expand Up @@ -277,7 +278,7 @@ record_def_with_types(_Config) ->

Text2 = "-record(r1, {f1 = defval :: t2()}).",
?assertMatch([_], parse_find_pois(Text2, type_application, {t2, 0})),
%% No redundanct atom POIs
%% No redundant atom POIs
?assertMatch([#{id := defval}], parse_find_pois(Text2, atom)),

Text3 = "-record(r1, {f1 :: t1(integer())}).",
Expand All @@ -290,7 +291,7 @@ record_def_with_types(_Config) ->

Text4 = "-record(r1, {f1 :: m:t1(integer())}).",
?assertMatch([_], parse_find_pois(Text4, type_application, {m, t1, 1})),
%% No redundanct atom POIs
%% No redundant atom POIs
?assertMatch([], parse_find_pois(Text4, atom)),

ok.
Expand All @@ -299,16 +300,24 @@ record_def_with_types(_Config) ->
record_def_with_record_type(_Config) ->
Text1 = "-record(r1, {f1 :: #r2{}}).",
?assertMatch([_], parse_find_pois(Text1, record_expr, r2)),
%% No redundanct atom POIs
%% No redundant atom POIs
?assertMatch([], parse_find_pois(Text1, atom)),

Text2 = "-record(r1, {f1 :: #r2{f2 :: t2()}}).",
?assertMatch([_], parse_find_pois(Text2, record_expr, r2)),
?assertMatch([_], parse_find_pois(Text2, record_field, {r2, f2})),
%% No redundanct atom POIs
%% No redundant atom POIs
?assertMatch([], parse_find_pois(Text2, atom)),
ok.

-spec record_index(config()) -> ok.
record_index(_Config) ->
Text1 = "#r1.f1.",
?assertMatch([_], parse_find_pois(Text1, record_expr, r1)),
?assertMatch([_], parse_find_pois(Text1, record_field, {r1, f1})),
%% No redundant atom POIs
?assertMatch([#{id := '*exprs*'}], parse_find_pois(Text1, atom)).

-spec callback_recursive(config()) -> ok.
callback_recursive(_Config) ->
Text = "-callback foo(#r1{f1 :: m:t1(#r2{f2 :: t2(t3())})}) -> any().",
Expand Down

0 comments on commit c6a8a5e

Please sign in to comment.