Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make completions calls async #1480

Merged
merged 1 commit into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions apps/els_core/include/els_core.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,11 @@
-define(COMPLETION_TRIGGER_KIND_CHARACTER, 2).
-define(COMPLETION_TRIGGER_KIND_FOR_INCOMPLETE_COMPLETIONS, 3).

-type completion_trigger_kind() ::
?COMPLETION_TRIGGER_KIND_INVOKED
| ?COMPLETION_TRIGGER_KIND_CHARACTER
| ?COMPLETION_TRIGGER_KIND_FOR_INCOMPLETE_COMPLETIONS.

%%------------------------------------------------------------------------------
%% Versioned Text Document Identifier
%%------------------------------------------------------------------------------
Expand Down
49 changes: 39 additions & 10 deletions apps/els_lsp/src/els_completion_provider.erl
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ trigger_characters() ->
<<" ">>
].

-spec handle_request(els_provider:provider_request()) -> {response, any()}.
-spec handle_request(els_provider:provider_request()) ->
{response, any()} | {async, uri(), pid()}.
handle_request({completion, Params}) ->
#{
<<"position">> := #{
Expand All @@ -55,14 +56,30 @@ handle_request({completion, Params}) ->
},
<<"textDocument">> := #{<<"uri">> := Uri}
} = Params,
{ok, #{text := Text} = Document} = els_utils:lookup_document(Uri),
Context = maps:get(
<<"context">>,
Params,
#{<<"triggerKind">> => ?COMPLETION_TRIGGER_KIND_INVOKED}
),
TriggerKind = maps:get(<<"triggerKind">>, Context),
TriggerCharacter = maps:get(<<"triggerCharacter">>, Context, <<>>),
Job = run_completion_job(Uri, Line, Character, TriggerKind, TriggerCharacter),
{async, Uri, Job};
handle_request({resolve, CompletionItem}) ->
{response, resolve(CompletionItem)}.

%%==============================================================================
%% Internal functions
%%==============================================================================
-spec run_completion_job(
uri(),
line(),
column(),
completion_trigger_kind(),
binary()
) -> pid().
run_completion_job(Uri, Line, Character, TriggerKind, TriggerCharacter) ->
{ok, #{text := Text} = Document} = els_utils:lookup_document(Uri),
%% We subtract 1 to strip the character that triggered the
%% completion from the string.
Length =
Expand All @@ -79,20 +96,32 @@ handle_request({completion, Params}) ->
?COMPLETION_TRIGGER_KIND_FOR_INCOMPLETE_COMPLETIONS ->
els_text:line(Text, Line, Character)
end,
?LOG_INFO("Find completions for ~s", [Prefix]),
Opts = #{
trigger => TriggerCharacter,
document => Document,
line => Line + 1,
column => Character
},
Completions = find_completions(Prefix, TriggerKind, Opts),
{response, Completions};
handle_request({resolve, CompletionItem}) ->
{response, resolve(CompletionItem)}.
Config = #{
task => fun find_completions/2,
entries => [{Prefix, TriggerKind, Opts}],
title => <<"Completion">>,
on_complete =>
fun(Resp) ->
els_server ! {result, Resp, self()},
ok
end
},
{ok, Pid} = els_background_job:new(Config),
Pid.

-spec find_completions({binary(), completion_trigger_kind(), options()}, any()) -> items().
find_completions({Prefix, TriggerKind, Opts}, _) ->
Result = find_completions(Prefix, TriggerKind, Opts),
?LOG_INFO("Found completions: ~p", [length(Result)]),
Result.

%%==============================================================================
%% Internal functions
%%==============================================================================
-spec resolve(map()) -> map().
resolve(
#{
Expand Down Expand Up @@ -131,7 +160,7 @@ resolve(
resolve(CompletionItem) ->
CompletionItem.

-spec find_completions(binary(), integer(), options()) -> items().
-spec find_completions(binary(), completion_trigger_kind(), options()) -> items().
find_completions(
Prefix,
?COMPLETION_TRIGGER_KIND_CHARACTER,
Expand Down
4 changes: 2 additions & 2 deletions apps/els_lsp/src/els_methods.erl
Original file line number Diff line number Diff line change
Expand Up @@ -285,9 +285,9 @@ textdocument_hover(Params, State) ->
-spec textdocument_completion(params(), els_server:state()) -> result().
textdocument_completion(Params, State) ->
Provider = els_completion_provider,
{response, Response} =
{async, Uri, Job} =
els_provider:handle_request(Provider, {completion, Params}),
{response, Response, State}.
{async, Uri, Job, State}.

%%==============================================================================
%% completionItem/resolve
Expand Down
Loading