Skip to content

Commit

Permalink
Add OTP support in "Add include_lib" action (#1561)
Browse files Browse the repository at this point in the history
Also fixes completion for OTP behaviours
  • Loading branch information
plux authored Oct 9, 2024
1 parent 2bda865 commit f8a6ee1
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 31 deletions.
3 changes: 1 addition & 2 deletions apps/els_lsp/src/els_code_actions.erl
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,8 @@ add_include_lib_record(Uri, Range, _Data, [Record]) ->
-spec add_include_file(uri(), range(), els_poi:poi_kind(), atom(), els_poi:poi_id()) -> [map()].
add_include_file(Uri, Range, Kind, Name, Id) ->
%% TODO: Add support for -include() also
%% TODO: Doesn't work for OTP headers
CandidateUris =
els_dt_document:find_candidates(Name, 'header'),
els_dt_document:find_candidates_with_otp(Name, 'header'),
Uris = [
CandidateUri
|| CandidateUri <- CandidateUris,
Expand Down
10 changes: 2 additions & 8 deletions apps/els_lsp/src/els_completion_provider.erl
Original file line number Diff line number Diff line change
Expand Up @@ -946,20 +946,14 @@ item_kind_module(Module) ->

-spec behaviour_modules(list()) -> [atom()].
behaviour_modules(Begin) ->
OtpBehaviours = [
gen_event,
gen_server,
gen_statem,
supervisor
],
Candidates = els_dt_document:find_candidates(callback),
Candidates = els_dt_document:find_candidates_with_otp(callback, 'module'),
Behaviours = [
els_uri:module(Uri)
|| Uri <- Candidates,
lists:prefix(Begin, atom_to_list(els_uri:module(Uri))),
is_behaviour(Uri)
],
OtpBehaviours ++ Behaviours.
Behaviours.

-spec is_behaviour(uri()) -> boolean().
is_behaviour(Uri) ->
Expand Down
32 changes: 31 additions & 1 deletion apps/els_lsp/src/els_dt_document.erl
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
wrapping_functions/3,
find_candidates/1,
find_candidates/2,
find_candidates_with_otp/2,
get_words/1
]).

Expand Down Expand Up @@ -66,7 +67,7 @@
kind :: kind() | '_',
text :: binary() | '_',
pois :: [els_poi:poi()] | '_' | ondemand,
source :: source() | '$2',
source :: source() | '_' | '$2',
words :: sets:set() | '_' | '$3',
version :: version() | '_'
}).
Expand Down Expand Up @@ -300,6 +301,35 @@ find_candidates(Pattern, Kind) ->
end,
lists:filtermap(Fun, All).

-spec find_candidates_with_otp(atom() | string(), module | header | '_') -> [uri()].
find_candidates_with_otp(Pattern, Kind) ->
%% ets:fun2ms(fun(#els_dt_document{source = Source, uri = Uri, words = Words})
%% when Source =/= otp -> {Uri, Words} end).
MS = [
{
#els_dt_document{
uri = '$1',
id = '_',
kind = Kind,
text = '_',
pois = '_',
source = '_',
words = '$3',
version = '_'
},
[],
[{{'$1', '$3'}}]
}
],
All = ets:select(name(), MS),
Fun = fun({Uri, Words}) ->
case sets:is_element(Pattern, Words) of
true -> {true, Uri};
false -> false
end
end,
lists:filtermap(Fun, All).

-spec get_words(binary()) -> sets:set().
get_words(Text) ->
case erl_scan:string(els_utils:to_list(Text)) of
Expand Down
20 changes: 0 additions & 20 deletions apps/els_lsp/test/els_completion_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -311,26 +311,6 @@ attribute_behaviour(Config) ->
Uri = ?config(completion_attributes_uri, Config),
TriggerKindInvoked = ?COMPLETION_TRIGGER_KIND_INVOKED,
Expected = [
#{
insertTextFormat => ?INSERT_TEXT_FORMAT_PLAIN_TEXT,
kind => ?COMPLETION_ITEM_KIND_MODULE,
label => <<"gen_event">>
},
#{
insertTextFormat => ?INSERT_TEXT_FORMAT_PLAIN_TEXT,
kind => ?COMPLETION_ITEM_KIND_MODULE,
label => <<"gen_server">>
},
#{
insertTextFormat => ?INSERT_TEXT_FORMAT_PLAIN_TEXT,
kind => ?COMPLETION_ITEM_KIND_MODULE,
label => <<"gen_statem">>
},
#{
insertTextFormat => ?INSERT_TEXT_FORMAT_PLAIN_TEXT,
kind => ?COMPLETION_ITEM_KIND_MODULE,
label => <<"supervisor">>
},
#{
insertTextFormat => ?INSERT_TEXT_FORMAT_PLAIN_TEXT,
kind => ?COMPLETION_ITEM_KIND_MODULE,
Expand Down

0 comments on commit f8a6ee1

Please sign in to comment.