Skip to content

Commit c0096fd

Browse files
authored
Fix bug where completion would erroneously think it's inside a record (#1554)
1 parent 3342c36 commit c0096fd

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

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

+8
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,11 @@ function_a(#record_a{field_a = a, field_b = b}) ->
88
%% #record_a{
99
field_y = y},
1010
{}.
11+
12+
-spec function_b(#record_b{}) -> #record_a{}.
13+
function_b(R) ->
14+
function_a(R).
15+
16+
-define(A, #record_a{}).
17+
function_c(R) ->
18+
function_a(R).

apps/els_lsp/src/els_completion_provider.erl

+4-3
Original file line numberDiff line numberDiff line change
@@ -628,15 +628,16 @@ complete_record_field(
628628
-spec complete_record_field(map(), pos(), binary()) -> items().
629629
complete_record_field(#{text := Text0} = Document, Pos, Suffix) ->
630630
Prefix0 = els_text:range(Text0, {1, 1}, Pos),
631-
POIs = els_dt_document:pois(Document, [function]),
632-
%% Look for record start between current pos and end of last function
631+
POIs = els_dt_document:pois(Document, [function, spec, define, callback, record]),
632+
%% Look for record start between current position and end of last
633+
%% relevant top level expression
633634
Prefix =
634635
case els_scope:pois_before(POIs, #{from => Pos, to => Pos}) of
635636
[#{range := #{to := {Line, _}}} | _] ->
636637
{_, Prefix1} = els_text:split_at_line(Prefix0, Line),
637638
Prefix1;
638639
_ ->
639-
%% No function before, consider all the text
640+
%% Found no POI before, consider all the text
640641
Prefix0
641642
end,
642643
case parse_record(els_text:strip_comments(Prefix), Suffix) of

apps/els_lsp/test/els_completion_SUITE.erl

+18
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
records/1,
3434
record_fields/1,
3535
record_fields_inside_record/1,
36+
record_fields_no_completion/1,
3637
types/1,
3738
types_export_list/1,
3839
types_context/1,
@@ -1195,6 +1196,23 @@ record_fields_inside_record(Config) ->
11951196
?assertEqual(lists:sort(Expected2), lists:sort(Completion2)),
11961197
ok.
11971198

1199+
-spec record_fields_no_completion(config()) -> ok.
1200+
record_fields_no_completion(Config) ->
1201+
%% These should NOT trigger record fields completion
1202+
%% Instead expected behaviour is to trigger regular atom completion
1203+
Uri = ?config(completion_records_uri, Config),
1204+
TriggerKindInvoked = ?COMPLETION_TRIGGER_KIND_INVOKED,
1205+
#{result := Result} =
1206+
els_client:completion(Uri, 14, 14, TriggerKindInvoked, <<>>),
1207+
1208+
#{result := Result} =
1209+
els_client:completion(Uri, 18, 14, TriggerKindInvoked, <<>>),
1210+
1211+
Labels = [Label || #{label := Label} <- Result],
1212+
?assert(lists:member(<<"function_a/1">>, Labels)),
1213+
?assert(lists:member(<<"function_b/1">>, Labels)),
1214+
ok.
1215+
11981216
-spec types(config()) -> ok.
11991217
types(Config) ->
12001218
TriggerKind = ?COMPLETION_TRIGGER_KIND_INVOKED,

0 commit comments

Comments
 (0)