Skip to content

Commit

Permalink
Replace record_access with record_expr+record_field pois
Browse files Browse the repository at this point in the history
  • Loading branch information
gomoripeti committed Jan 13, 2021
1 parent 6bc28b9 commit 05e2ccf
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 52 deletions.
1 change: 0 additions & 1 deletion include/erlang_ls.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,6 @@
| parse_transform
| record
| record_def_field
| record_access
| record_expr
| record_field
| spec
Expand Down
3 changes: 0 additions & 3 deletions src/els_code_navigation.erl
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@ goto_definition( _Uri
end;
goto_definition(Uri, #{ kind := macro, id := Define }) ->
find(Uri, define, Define);
goto_definition(Uri, #{ kind := record_access
, id := Record}) ->
find(Uri, record, Record);
goto_definition(Uri, #{ kind := record_expr, id := Record }) ->
find(Uri, record, Record);
goto_definition(Uri, #{ kind := record_field, id := {Record, Field} }) ->
Expand Down
3 changes: 1 addition & 2 deletions src/els_dt_references.erl
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,7 @@ kind_to_category(Kind) when Kind =:= macro;
Kind =:= define ->
macro;
kind_to_category(Kind) when Kind =:= record_expr;
Kind =:= record;
Kind =:= record_access ->
Kind =:= record ->
record;
kind_to_category(Kind) when Kind =:= behaviour ->
behaviour.
2 changes: 0 additions & 2 deletions src/els_indexing.erl
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ index_references(#{uri := Uri} = Document, 'deep') ->
, behaviour
, implicit_fun
, macro
, record_access
, record_expr
, type_application
]),
Expand Down Expand Up @@ -139,7 +138,6 @@ register_reference(Uri, #{id := {F, A}} = POI) ->
register_reference(Uri, #{kind := Kind, id := Id, range := Range})
when %% Record
Kind =:= record_expr;
Kind =:= record_access;
%% Macro
Kind =:= macro;
%% Function
Expand Down
49 changes: 30 additions & 19 deletions src/els_parser.erl
Original file line number Diff line number Diff line change
Expand Up @@ -384,11 +384,16 @@ record_access(Tree) ->
case erl_syntax:type(RecordNode) of
atom ->
Record = erl_syntax:atom_value(RecordNode),
Field = case erl_syntax:type(FieldNode) of
atom -> erl_syntax:atom_value(FieldNode);
_ -> 'UNKNOWN_FIELD'
end,
[poi(erl_syntax:get_pos(Tree), record_access, Record, Field)];
FieldPoi =
case erl_syntax:type(FieldNode) of
atom ->
Field = erl_syntax:atom_value(FieldNode),
[poi(erl_syntax:get_pos(FieldNode), record_field, {Record, Field})];
_ ->
[]
end,
[ poi(erl_syntax:get_pos(Tree), record_expr, Record)
| FieldPoi ];
_ ->
[]
end.
Expand Down Expand Up @@ -544,9 +549,9 @@ subtrees(Tree, macro) ->
Args -> [Args]
end;
subtrees(Tree, record_access) ->
[ [ erl_syntax:record_access_argument(Tree)
, erl_syntax:record_access_field(Tree)
]
NameNode = erl_syntax:record_access_field(Tree),
[ [erl_syntax:record_access_argument(Tree)]
, skip_record_field_atom(NameNode)
];
subtrees(Tree, record_expr) ->
Fields = erl_syntax:record_expr_fields(Tree),
Expand All @@ -556,18 +561,13 @@ subtrees(Tree, record_expr) ->
end;
subtrees(Tree, record_field) ->
NameNode = erl_syntax:record_field_name(Tree),
[case erl_syntax:type(NameNode) of
atom ->
[];
_ ->
[NameNode]
end,
case erl_syntax:record_field_value(Tree) of
none ->
[];
V ->
[ skip_record_field_atom(NameNode)
, case erl_syntax:record_field_value(Tree) of
none ->
[];
V ->
[V]
end];
end];
subtrees(Tree, attribute) ->
case erl_syntax:attribute_arguments(Tree) of
none -> [];
Expand All @@ -576,6 +576,17 @@ subtrees(Tree, attribute) ->
subtrees(Tree, _) ->
erl_syntax:subtrees(Tree).

%% Skip visiting atoms of record field names as they are already represented as
%% `record_field' pois
-spec skip_record_field_atom(tree()) -> [tree()].
skip_record_field_atom(NameNode) ->
case erl_syntax:type(NameNode) of
atom ->
[];
_ ->
[NameNode]
end.

-spec pretty_print(tree()) -> binary().
pretty_print(Tree) ->
try
Expand Down
4 changes: 0 additions & 4 deletions src/els_range.erl
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,6 @@ range({Line, Column}, parse_transform, PT, _Data) ->
From = {Line, Column},
To = plus(From, atom_to_list(PT)),
#{ from => From, to => To };
range(Pos, record_access, Record, Field) ->
From = plus(Pos, "#"),
#{ from => From
, to => plus(From, atom_to_list(Record) ++ "." ++ atom_to_list(Field)) };
range(Pos, record_expr, Record, _Data) ->
From = plus(Pos, "#"),
#{ from => From, to => plus(From, atom_to_list(Record)) };
Expand Down
3 changes: 1 addition & 2 deletions src/els_references_provider.erl
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ find_references(Uri, #{ kind := Kind
find_references_for_id(Kind, Key);
find_references(_Uri, #{kind := Kind, id := Name})
when Kind =:= record_expr;
Kind =:= record;
Kind =:= record_access ->
Kind =:= record ->
find_references_for_id(Kind, Name);
find_references(_Uri, #{kind := Kind, id := Name})
when Kind =:= macro;
Expand Down
37 changes: 22 additions & 15 deletions test/els_document_highlight_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -150,19 +150,15 @@ atom(Config) ->
record(Config) ->
Uri = ?config(code_navigation_uri, Config),
#{result := Locations} = els_client:document_highlight(Uri, 23, 4),
ExpectedLocations = [ #{range => #{from => {23, 4}, to => {23, 12}}}
, #{range => #{from => {33, 8}, to => {33, 16}}}
],
ExpectedLocations = record_uses(),
assert_locations(ExpectedLocations, Locations),
ok.

-spec record_access(config()) -> ok.
record_access(Config) ->
Uri = ?config(code_navigation_uri, Config),
#{result := Locations} = els_client:document_highlight(Uri, 34, 10),
ExpectedLocations = [ #{range => #{from => {34, 10}, to => {34, 26}}}
, #{range => #{from => {34, 35}, to => {34, 51}}}
],
ExpectedLocations = record_uses(),
assert_locations(ExpectedLocations, Locations),
ok.

Expand All @@ -172,9 +168,8 @@ record_field(Config) ->
#{result := Locations} = els_client:document_highlight(Uri, 16, 23),
ExpectedLocations = [ #{range => #{from => {33, 18}, to => {33, 25}}}
, #{range => #{from => {16, 20}, to => {16, 27}}}
%% TODO record access not highlighted
%%, #{range => #{from => {34, 19}, to => {34, 26}}}
%%, #{range => #{from => {34, 44}, to => {34, 51}}}
, #{range => #{from => {34, 19}, to => {34, 26}}}
, #{range => #{from => {34, 44}, to => {34, 51}}}
],
assert_locations(ExpectedLocations, Locations),
ok.
Expand Down Expand Up @@ -308,25 +303,37 @@ callback(Config) ->
assert_locations(null, null) ->
ok;
assert_locations(ExpectedLocations, Locations) ->
ct:log("ExpectedLocations: ~p~nLocations: ~p~n",
[ExpectedLocations, lists:sort(Locations)]),
ExpectedProtoLocs = lists:sort(protocol_ranges(ExpectedLocations)),
SortedLocations = lists:sort(Locations),
ct:log("ExpectedLocations:~n ~p~nLocations:~n ~p~n",
[ExpectedProtoLocs, SortedLocations]),
?assertEqual(length(ExpectedLocations), length(Locations)),
Pairs = lists:zip(lists:sort(Locations), ExpectedLocations),
Pairs = lists:zip(SortedLocations, ExpectedProtoLocs),
[ begin
#{range := Range} = Location,
#{range := ExpectedRange} = Expected,
?assertEqual( els_protocol:range(ExpectedRange)
, Range
)
?assertEqual(ExpectedRange, Range)
end
|| {Location, Expected} <- Pairs
],
ok.

protocol_ranges(Locations) ->
[ L#{range => els_protocol:range(R)}
|| L = #{range := R} <- Locations ].

-spec expected_definitions() -> [map()].
expected_definitions() ->
[ #{range => #{from => {25, 1}, to => {25, 11}}}
, #{range => #{from => {22, 3}, to => {22, 13}}}
, #{range => #{from => {51, 7}, to => {51, 23}}}
, #{range => #{from => {5, 25}, to => {5, 37}}}
].

-spec record_uses() -> [map()].
record_uses() ->
[ #{range => #{from => {23, 4}, to => {23, 12}}}
, #{range => #{from => {33, 8}, to => {33, 16}}}
, #{range => #{from => {34, 10}, to => {34, 18}}}
, #{range => #{from => {34, 35}, to => {34, 43}}}
].
8 changes: 4 additions & 4 deletions test/els_references_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -201,17 +201,17 @@ record(Config) ->
, range => #{from => {33, 8}, to => {33, 16}}
}
, #{ uri => Uri
, range => #{from => {34, 10}, to => {34, 26}}
, range => #{from => {34, 10}, to => {34, 18}}
}
, #{ uri => Uri
, range => #{from => {34, 35}, to => {34, 51}}
, range => #{from => {34, 35}, to => {34, 43}}
}
],

ct:comment("Find references record_a from a usage"),
#{result := Locations} = els_client:references(Uri, 23, 4),
ct:comment("Find references record_a from a field usage"),
#{result := Locations} = els_client:references(Uri, 34, 22),
ct:comment("Find references record_a from an access"),
#{result := Locations} = els_client:references(Uri, 34, 15),
ct:comment("Find references record_a from beginning of definition"),
#{result := Locations} = els_client:references(Uri, 16, 9),
ct:comment("Find references record_a from end of definition"),
Expand Down

0 comments on commit 05e2ccf

Please sign in to comment.