Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug where completion would erroneously think it's inside a record #1554

Merged
merged 1 commit into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions apps/els_lsp/priv/code_navigation/src/completion_records.erl
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,11 @@ function_a(#record_a{field_a = a, field_b = b}) ->
%% #record_a{
field_y = y},
{}.

-spec function_b(#record_b{}) -> #record_a{}.
function_b(R) ->
function_a(R).

-define(A, #record_a{}).
function_c(R) ->
function_a(R).
7 changes: 4 additions & 3 deletions apps/els_lsp/src/els_completion_provider.erl
Original file line number Diff line number Diff line change
Expand Up @@ -628,15 +628,16 @@ complete_record_field(
-spec complete_record_field(map(), pos(), binary()) -> items().
complete_record_field(#{text := Text0} = Document, Pos, Suffix) ->
Prefix0 = els_text:range(Text0, {1, 1}, Pos),
POIs = els_dt_document:pois(Document, [function]),
%% Look for record start between current pos and end of last function
POIs = els_dt_document:pois(Document, [function, spec, define, callback, record]),
%% Look for record start between current position and end of last
%% relevant top level expression
Prefix =
case els_scope:pois_before(POIs, #{from => Pos, to => Pos}) of
[#{range := #{to := {Line, _}}} | _] ->
{_, Prefix1} = els_text:split_at_line(Prefix0, Line),
Prefix1;
_ ->
%% No function before, consider all the text
%% Found no POI before, consider all the text
Prefix0
end,
case parse_record(els_text:strip_comments(Prefix), Suffix) of
Expand Down
18 changes: 18 additions & 0 deletions apps/els_lsp/test/els_completion_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
records/1,
record_fields/1,
record_fields_inside_record/1,
record_fields_no_completion/1,
types/1,
types_export_list/1,
types_context/1,
Expand Down Expand Up @@ -1195,6 +1196,23 @@ record_fields_inside_record(Config) ->
?assertEqual(lists:sort(Expected2), lists:sort(Completion2)),
ok.

-spec record_fields_no_completion(config()) -> ok.
record_fields_no_completion(Config) ->
%% These should NOT trigger record fields completion
%% Instead expected behaviour is to trigger regular atom completion
Uri = ?config(completion_records_uri, Config),
TriggerKindInvoked = ?COMPLETION_TRIGGER_KIND_INVOKED,
#{result := Result} =
els_client:completion(Uri, 14, 14, TriggerKindInvoked, <<>>),

#{result := Result} =
els_client:completion(Uri, 18, 14, TriggerKindInvoked, <<>>),

Labels = [Label || #{label := Label} <- Result],
?assert(lists:member(<<"function_a/1">>, Labels)),
?assert(lists:member(<<"function_b/1">>, Labels)),
ok.

-spec types(config()) -> ok.
types(Config) ->
TriggerKind = ?COMPLETION_TRIGGER_KIND_INVOKED,
Expand Down
Loading