Skip to content

Commit

Permalink
Find references asynchronously (#1380)
Browse files Browse the repository at this point in the history
  • Loading branch information
robertoaloi authored Sep 19, 2022
1 parent f1525fc commit d5adfbd
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
2 changes: 1 addition & 1 deletion apps/els_lsp/src/els_definition_provider.erl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
%%==============================================================================
%% els_provider functions
%%==============================================================================
-spec handle_request(any()) -> {response, any()}.
-spec handle_request(any()) -> {response, any()} | {async, uri(), pid()}.
handle_request({definition, Params}) ->
#{
<<"position">> := #{
Expand Down
14 changes: 8 additions & 6 deletions apps/els_lsp/src/els_methods.erl
Original file line number Diff line number Diff line change
Expand Up @@ -306,9 +306,12 @@ completionitem_resolve(Params, State) ->
-spec textdocument_definition(params(), els_server:state()) -> result().
textdocument_definition(Params, State) ->
Provider = els_definition_provider,
{response, Response} =
els_provider:handle_request(Provider, {definition, Params}),
{response, Response, State}.
case els_provider:handle_request(Provider, {definition, Params}) of
{response, Response} ->
{response, Response, State};
{async, Uri, Job} ->
{async, Uri, Job, State}
end.

%%==============================================================================
%% textDocument/references
Expand All @@ -317,9 +320,8 @@ textdocument_definition(Params, State) ->
-spec textdocument_references(params(), els_server:state()) -> result().
textdocument_references(Params, State) ->
Provider = els_references_provider,
{response, Response} =
els_provider:handle_request(Provider, {references, Params}),
{response, Response, State}.
{async, Uri, Job} = els_provider:handle_request(Provider, {references, Params}),
{async, Uri, Job, State}.

%%==============================================================================
%% textDocument/documentHightlight
Expand Down
31 changes: 28 additions & 3 deletions apps/els_lsp/src/els_references_provider.erl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
%% Includes
%%==============================================================================
-include("els_lsp.hrl").
-include_lib("kernel/include/logger.hrl").

%%==============================================================================
%% Types
Expand All @@ -25,7 +26,7 @@
%%==============================================================================
%% els_provider functions
%%==============================================================================
-spec handle_request(any()) -> {response, [location()] | null}.
-spec handle_request(any()) -> {async, uri(), pid()}.
handle_request({references, Params}) ->
#{
<<"position">> := #{
Expand All @@ -34,15 +35,39 @@ handle_request({references, Params}) ->
},
<<"textDocument">> := #{<<"uri">> := Uri}
} = Params,
?LOG_DEBUG(
"Starting references job " "[uri=~p, line=~p, character=~p]",
[Uri, Line, Character]
),
Job = run_references_job(Uri, Line, Character),
{async, Uri, Job}.

-spec run_references_job(uri(), line(), column()) -> pid().
run_references_job(Uri, Line, Character) ->
Config = #{
task => fun get_references/2,
entries => [{Uri, Line, Character}],
title => <<"References">>,
on_complete =>
fun(ReferencesResp) ->
els_server ! {result, ReferencesResp, self()},
ok
end
},
{ok, Pid} = els_background_job:new(Config),
Pid.

-spec get_references({uri(), integer(), integer()}, _) -> null | [location()].
get_references({Uri, Line, Character}, _) ->
{ok, Document} = els_utils:lookup_document(Uri),
Refs =
case els_dt_document:get_element_at_pos(Document, Line + 1, Character + 1) of
[POI | _] -> find_references(Uri, POI);
[] -> []
end,
case Refs of
[] -> {response, null};
Rs -> {response, Rs}
[] -> null;
Rs -> Rs
end.

%%==============================================================================
Expand Down

0 comments on commit d5adfbd

Please sign in to comment.