From 490e970515fc6f4570e35a269dbe3d88b788f31a Mon Sep 17 00:00:00 2001 From: Hakan Nilsson Date: Tue, 23 Mar 2021 00:15:01 +0100 Subject: [PATCH] Limit export completions to unexported definitions --- apps/els_lsp/src/els_completion_provider.erl | 25 +++-- apps/els_lsp/test/els_completion_SUITE.erl | 98 +++++--------------- 2 files changed, 40 insertions(+), 83 deletions(-) diff --git a/apps/els_lsp/src/els_completion_provider.erl b/apps/els_lsp/src/els_completion_provider.erl index b864b5543..f42fa703b 100644 --- a/apps/els_lsp/src/els_completion_provider.erl +++ b/apps/els_lsp/src/els_completion_provider.erl @@ -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; @@ -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) -> diff --git a/apps/els_lsp/test/els_completion_SUITE.erl b/apps/els_lsp/test/els_completion_SUITE.erl index 584d89b72..dd41a8ef9 100644 --- a/apps/els_lsp/test/els_completion_SUITE.erl +++ b/apps/els_lsp/test/els_completion_SUITE.erl @@ -298,64 +298,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) -> @@ -554,29 +518,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.