Skip to content

Commit

Permalink
Merge pull request #955 from plux/limit-export-completion-to-unexported
Browse files Browse the repository at this point in the history
Limit export completions to unexported definitions
  • Loading branch information
robertoaloi authored Mar 26, 2021
2 parents acd3b83 + 490e970 commit 4b82a5c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 83 deletions.
25 changes: 18 additions & 7 deletions apps/els_lsp/src/els_completion_provider.erl
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,19 @@ find_completions( Prefix
[{atom, _, Name} | _] ->
NameBinary = atom_to_binary(Name, utf8),
{ExportFormat, POIKind} = completion_context(Document, Line, Column),
keywords()
++ bifs(POIKind, ExportFormat)
++ atoms(Document, NameBinary)
++ all_record_fields(Document, NameBinary)
++ modules(NameBinary)
++ definitions(Document, POIKind, ExportFormat)
++ els_snippets_server:snippets();
case ExportFormat of
true ->
%% Only complete unexported definitions when in export
unexported_definitions(Document, POIKind);
false ->
keywords()
++ bifs(POIKind, ExportFormat)
++ atoms(Document, NameBinary)
++ all_record_fields(Document, NameBinary)
++ modules(NameBinary)
++ definitions(Document, POIKind, ExportFormat)
++ els_snippets_server:snippets()
end;
_ ->
[]
end;
Expand Down Expand Up @@ -222,6 +228,11 @@ item_kind_module(Module) ->
%%==============================================================================
%% Functions, Types, Macros and Records
%%==============================================================================
-spec unexported_definitions(els_dt_document:item(), poi_kind()) -> [map()].
unexported_definitions(Document, POIKind) ->
AllDefs = definitions(Document, POIKind, true, false),
ExportedDefs = definitions(Document, POIKind, true, true),
AllDefs -- ExportedDefs.

-spec definitions(els_dt_document:item(), poi_kind()) -> [map()].
definitions(Document, POIKind) ->
Expand Down
98 changes: 22 additions & 76 deletions apps/els_lsp/test/els_completion_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -299,64 +299,28 @@ functions_arity(Config) ->
functions_export_list(Config) ->
TriggerKind = ?COMPLETION_TRIGGER_KIND_INVOKED,
Uri = ?config(code_navigation_extra_uri, Config),
ExpectedCompletion = [ #{ label => <<"do/1">>
, kind => ?COMPLETION_ITEM_KIND_FUNCTION
, insertTextFormat => ?INSERT_TEXT_FORMAT_PLAIN_TEXT
, data =>
#{ module => <<"code_navigation_extra">>
, function => <<"do">>
, arity => 1
}
}
, #{ label => <<"do_2/0">>
, kind => ?COMPLETION_ITEM_KIND_FUNCTION
, insertTextFormat => ?INSERT_TEXT_FORMAT_PLAIN_TEXT
, data =>
#{ module => <<"code_navigation_extra">>
, function => <<"do_2">>
, arity => 0
}
}
, #{ insertTextFormat => ?INSERT_TEXT_FORMAT_PLAIN_TEXT
, kind => ?COMPLETION_ITEM_KIND_FUNCTION
, label => <<"do_3/2">>
, data =>
#{ module => <<"code_navigation_extra">>
, function => <<"do_3">>
, arity => 2
}
}
, #{ insertTextFormat => ?INSERT_TEXT_FORMAT_PLAIN_TEXT
, kind => ?COMPLETION_ITEM_KIND_FUNCTION
, label => <<"do_4/2">>
, data =>
#{ module => <<"code_navigation_extra">>
, function => <<"do_4">>
, arity => 2
}
}
, #{ label => <<"'DO_LOUDER'/0">>
, kind => ?COMPLETION_ITEM_KIND_FUNCTION
, insertTextFormat => ?INSERT_TEXT_FORMAT_PLAIN_TEXT
, data =>
#{ module => <<"code_navigation_extra">>
, function => <<"DO_LOUDER">>
, arity => 0
}
}
],

DefaultCompletion = els_completion_provider:keywords()
++ els_completion_provider:bifs(function, true)
++ els_snippets_server:snippets(),

Expected = [ #{ insertTextFormat => ?INSERT_TEXT_FORMAT_PLAIN_TEXT
, kind => ?COMPLETION_ITEM_KIND_FUNCTION
, label => <<"do_3/2">>
, data =>
#{ module => <<"code_navigation_extra">>
, function => <<"do_3">>
, arity => 2
}
}
, #{ insertTextFormat => ?INSERT_TEXT_FORMAT_PLAIN_TEXT
, kind => ?COMPLETION_ITEM_KIND_FUNCTION
, label => <<"do_4/2">>
, data =>
#{ module => <<"code_navigation_extra">>
, function => <<"do_4">>
, arity => 2
}
}
],
#{result := Completion} =
els_client:completion(Uri, 3, 13, TriggerKind, <<"">>),
?assertEqual(
lists:sort(ExpectedCompletion),
filter_completion(Completion, DefaultCompletion)),

ok.
?assertEqual(Expected, Completion).

-spec handle_empty_lines(config()) -> ok.
handle_empty_lines(Config) ->
Expand Down Expand Up @@ -555,29 +519,11 @@ types_export_list(Config) ->
, label => <<"user_type_a/0">>
, data => #{}
}
, #{ insertTextFormat => ?INSERT_TEXT_FORMAT_PLAIN_TEXT
, kind => ?COMPLETION_ITEM_KIND_TYPE_PARAM
, label => <<"type_a/0">>
, data => #{}
}
, #{ insertTextFormat => ?INSERT_TEXT_FORMAT_PLAIN_TEXT
, kind => ?COMPLETION_ITEM_KIND_TYPE_PARAM
, label => <<"opaque_type_a/0">>
, data => #{}
}
],

DefaultCompletion = els_completion_provider:keywords()
++ els_completion_provider:bifs(type_definition, true)
++ els_snippets_server:snippets(),

ct:comment("Types in an export_type section is provided with arity"),
#{result := Completion1} =
#{result := Completion} =
els_client:completion(Uri, 5, 19, TriggerKind, <<"">>),
?assertEqual(
lists:sort(Expected),
filter_completion(Completion1, DefaultCompletion)),

?assertEqual(Expected, Completion),
ok.

-spec variables(config()) -> ok.
Expand Down

0 comments on commit 4b82a5c

Please sign in to comment.