Skip to content

Commit f8a6ee1

Browse files
authored
Add OTP support in "Add include_lib" action (#1561)
Also fixes completion for OTP behaviours
1 parent 2bda865 commit f8a6ee1

File tree

4 files changed

+34
-31
lines changed

4 files changed

+34
-31
lines changed

apps/els_lsp/src/els_code_actions.erl

+1-2
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,8 @@ add_include_lib_record(Uri, Range, _Data, [Record]) ->
191191
-spec add_include_file(uri(), range(), els_poi:poi_kind(), atom(), els_poi:poi_id()) -> [map()].
192192
add_include_file(Uri, Range, Kind, Name, Id) ->
193193
%% TODO: Add support for -include() also
194-
%% TODO: Doesn't work for OTP headers
195194
CandidateUris =
196-
els_dt_document:find_candidates(Name, 'header'),
195+
els_dt_document:find_candidates_with_otp(Name, 'header'),
197196
Uris = [
198197
CandidateUri
199198
|| CandidateUri <- CandidateUris,

apps/els_lsp/src/els_completion_provider.erl

+2-8
Original file line numberDiff line numberDiff line change
@@ -946,20 +946,14 @@ item_kind_module(Module) ->
946946

947947
-spec behaviour_modules(list()) -> [atom()].
948948
behaviour_modules(Begin) ->
949-
OtpBehaviours = [
950-
gen_event,
951-
gen_server,
952-
gen_statem,
953-
supervisor
954-
],
955-
Candidates = els_dt_document:find_candidates(callback),
949+
Candidates = els_dt_document:find_candidates_with_otp(callback, 'module'),
956950
Behaviours = [
957951
els_uri:module(Uri)
958952
|| Uri <- Candidates,
959953
lists:prefix(Begin, atom_to_list(els_uri:module(Uri))),
960954
is_behaviour(Uri)
961955
],
962-
OtpBehaviours ++ Behaviours.
956+
Behaviours.
963957

964958
-spec is_behaviour(uri()) -> boolean().
965959
is_behaviour(Uri) ->

apps/els_lsp/src/els_dt_document.erl

+31-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
wrapping_functions/3,
4040
find_candidates/1,
4141
find_candidates/2,
42+
find_candidates_with_otp/2,
4243
get_words/1
4344
]).
4445

@@ -66,7 +67,7 @@
6667
kind :: kind() | '_',
6768
text :: binary() | '_',
6869
pois :: [els_poi:poi()] | '_' | ondemand,
69-
source :: source() | '$2',
70+
source :: source() | '_' | '$2',
7071
words :: sets:set() | '_' | '$3',
7172
version :: version() | '_'
7273
}).
@@ -300,6 +301,35 @@ find_candidates(Pattern, Kind) ->
300301
end,
301302
lists:filtermap(Fun, All).
302303

304+
-spec find_candidates_with_otp(atom() | string(), module | header | '_') -> [uri()].
305+
find_candidates_with_otp(Pattern, Kind) ->
306+
%% ets:fun2ms(fun(#els_dt_document{source = Source, uri = Uri, words = Words})
307+
%% when Source =/= otp -> {Uri, Words} end).
308+
MS = [
309+
{
310+
#els_dt_document{
311+
uri = '$1',
312+
id = '_',
313+
kind = Kind,
314+
text = '_',
315+
pois = '_',
316+
source = '_',
317+
words = '$3',
318+
version = '_'
319+
},
320+
[],
321+
[{{'$1', '$3'}}]
322+
}
323+
],
324+
All = ets:select(name(), MS),
325+
Fun = fun({Uri, Words}) ->
326+
case sets:is_element(Pattern, Words) of
327+
true -> {true, Uri};
328+
false -> false
329+
end
330+
end,
331+
lists:filtermap(Fun, All).
332+
303333
-spec get_words(binary()) -> sets:set().
304334
get_words(Text) ->
305335
case erl_scan:string(els_utils:to_list(Text)) of

apps/els_lsp/test/els_completion_SUITE.erl

-20
Original file line numberDiff line numberDiff line change
@@ -311,26 +311,6 @@ attribute_behaviour(Config) ->
311311
Uri = ?config(completion_attributes_uri, Config),
312312
TriggerKindInvoked = ?COMPLETION_TRIGGER_KIND_INVOKED,
313313
Expected = [
314-
#{
315-
insertTextFormat => ?INSERT_TEXT_FORMAT_PLAIN_TEXT,
316-
kind => ?COMPLETION_ITEM_KIND_MODULE,
317-
label => <<"gen_event">>
318-
},
319-
#{
320-
insertTextFormat => ?INSERT_TEXT_FORMAT_PLAIN_TEXT,
321-
kind => ?COMPLETION_ITEM_KIND_MODULE,
322-
label => <<"gen_server">>
323-
},
324-
#{
325-
insertTextFormat => ?INSERT_TEXT_FORMAT_PLAIN_TEXT,
326-
kind => ?COMPLETION_ITEM_KIND_MODULE,
327-
label => <<"gen_statem">>
328-
},
329-
#{
330-
insertTextFormat => ?INSERT_TEXT_FORMAT_PLAIN_TEXT,
331-
kind => ?COMPLETION_ITEM_KIND_MODULE,
332-
label => <<"supervisor">>
333-
},
334314
#{
335315
insertTextFormat => ?INSERT_TEXT_FORMAT_PLAIN_TEXT,
336316
kind => ?COMPLETION_ITEM_KIND_MODULE,

0 commit comments

Comments
 (0)