Skip to content

Commit

Permalink
Store spec args in signature index for better performance (#1520)
Browse files Browse the repository at this point in the history
  • Loading branch information
plux committed May 7, 2024
1 parent 4b497a4 commit f9f4076
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 40 deletions.
18 changes: 18 additions & 0 deletions apps/els_lsp/src/els_arg.erl
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
-export([name/2]).
-export([index/1]).
-export([merge_args/2]).
-export([get_args/2]).

-export_type([arg/0]).
-export_type([args/0]).

-include_lib("els_core/include/els_core.hrl").

-type args() :: [arg()].
-type arg() :: #{
index := pos_integer(),
Expand All @@ -19,6 +22,21 @@
new(Index, Name) ->
#{index => Index, name => Name}.

-spec get_args(uri(), els_poi:poi()) -> els_arg:args().
get_args(Uri, #{
id := {F, A},
data := #{args := Args}
}) ->
M = els_uri:module(Uri),
case els_dt_signatures:lookup({M, F, A}) of
{ok, []} ->
Args;
{ok, [#{args := []} | _]} ->
Args;
{ok, [#{args := SpecArgs} | _]} ->
merge_args(SpecArgs, Args)
end.

-spec name(arg()) -> string().
name(Arg) ->
name("Arg", Arg).
Expand Down
12 changes: 2 additions & 10 deletions apps/els_lsp/src/els_completion_provider.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1139,16 +1139,8 @@ args(#{kind := type_definition, data := POIData}, _Uri) ->
maps:get(args, POIData);
args(#{kind := _Kind, data := POIData}, _Uri = undefined) ->
maps:get(args, POIData);
args(#{kind := function, data := POIData, id := Id}, Uri) ->
%% Try to fetch args from -spec
{ok, [Document]} = els_dt_document:lookup(Uri),
POIs = els_dt_document:pois(Document, [spec]),
case [P || #{id := SpecId} = P <- POIs, SpecId == Id] of
[#{data := #{args := SpecArgs}} | _] when SpecArgs /= [] ->
els_arg:merge_args(SpecArgs, maps:get(args, POIData));
_ ->
maps:get(args, POIData)
end.
args(#{kind := function} = POI, Uri) ->
els_arg:get_args(Uri, POI).

-spec features() -> items().
features() ->
Expand Down
18 changes: 12 additions & 6 deletions apps/els_lsp/src/els_dt_signatures.erl
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,16 @@
-record(els_dt_signatures, {
mfa :: mfa() | '_' | {atom(), '_', '_'},
spec :: binary() | '_',
version :: version() | '_'
version :: version() | '_',
args :: els_arg:args() | '_'
}).
-type els_dt_signatures() :: #els_dt_signatures{}.
-type version() :: null | integer().
-type item() :: #{
mfa := mfa(),
spec := binary(),
version := version()
version := version(),
args := els_arg:args()
}.
-export_type([item/0]).

Expand All @@ -69,24 +71,28 @@ opts() ->
from_item(#{
mfa := MFA,
spec := Spec,
version := Version
version := Version,
args := Args
}) ->
#els_dt_signatures{
mfa = MFA,
spec = Spec,
version = Version
version = Version,
args = Args
}.

-spec to_item(els_dt_signatures()) -> item().
to_item(#els_dt_signatures{
mfa = MFA,
spec = Spec,
version = Version
version = Version,
args = Args
}) ->
#{
mfa => MFA,
spec => Spec,
version => Version
version => Version,
args => Args
}.

-spec insert(item()) -> ok | {error, any()}.
Expand Down
5 changes: 3 additions & 2 deletions apps/els_lsp/src/els_indexing.erl
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,14 @@ index_signatures(Id, Uri, Text, POIs, Version) ->
-spec index_signature(atom(), binary(), els_poi:poi(), version()) -> ok.
index_signature(_M, _Text, #{id := undefined}, _Version) ->
ok;
index_signature(M, Text, #{id := {F, A}, range := Range}, Version) ->
index_signature(M, Text, #{id := {F, A}, range := Range, data := #{args := Args}}, Version) ->
#{from := From, to := To} = Range,
Spec = els_text:range(Text, From, To),
els_dt_signatures:versioned_insert(#{
mfa => {M, F, A},
spec => Spec,
version => Version
version => Version,
args => Args
}).

-spec index_references(atom(), uri(), [els_poi:poi()], version()) -> ok.
Expand Down
26 changes: 4 additions & 22 deletions apps/els_lsp/src/els_inlay_hint_provider.erl
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ arg_hints(Uri, #{kind := application, data := #{args := CallArgs}} = POI) ->
fun(#{index := N, range := ArgRange, name := Name}) ->
case els_code_navigation:goto_definition(Uri, POI) of
{ok, [{DefUri, DefPOI} | _]} ->
DefArgs = get_args(DefUri, DefPOI),
DefArgs = els_arg:get_args(DefUri, DefPOI),
DefArgName = arg_name(N, DefArgs),
case should_show_arg_hint(Name, DefArgName) of
true ->
Expand All @@ -84,7 +84,9 @@ arg_hints(Uri, #{kind := application, data := #{args := CallArgs}} = POI) ->
end
end,
CallArgs
).
);
arg_hints(_Uri, _POI) ->
[].

-spec arg_hint(els_poi:poi_range(), string()) -> inlay_hint().
arg_hint(#{from := {FromL, FromC}}, ArgName) ->
Expand Down Expand Up @@ -137,23 +139,3 @@ arg_name(N, Args) ->
#{name := Name} ->
Name
end.

-spec get_args(uri(), els_poi:poi()) -> els_arg:args().
get_args(Uri, #{
id := {F, A},
data := #{args := Args}
}) ->
{ok, Document} = els_utils:lookup_document(Uri),
SpecPOIs = els_dt_document:pois(Document, [spec]),
SpecMatches = [
SpecArgs
|| #{id := Id, data := #{args := SpecArgs}} <- SpecPOIs,
Id == {F, A},
SpecArgs /= []
],
case SpecMatches of
[] ->
Args;
[SpecArgs | _] ->
els_arg:merge_args(SpecArgs, Args)
end.

0 comments on commit f9f4076

Please sign in to comment.