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

Add inlay hints to show if a function is exported #1521

Merged
merged 1 commit into from
May 7, 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
31 changes: 29 additions & 2 deletions apps/els_lsp/src/els_inlay_hint_provider.erl
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,39 @@ get_inlay_hints({Uri, Range}, _) ->
{ok, [Document]} = els_dt_document:lookup(Uri),
%% Fetch all application POIs that are in the given range
AppPOIs = els_dt_document:pois_in_range(Document, [application], Range),
Res = lists:flatmap(fun(POI) -> arg_hints(Uri, POI) end, AppPOIs),
ArgHints = lists:flatmap(fun(POI) -> arg_hints(Uri, POI) end, AppPOIs),

%% Fetch all function_clause POIs that are in the given range
FunPOIs = els_dt_document:pois_in_range(Document, [function_clause], Range),
%% Fetch all export entries
ExportPOIs = els_dt_document:pois(Document, [export_entry]),
FunHints = lists:flatmap(fun(POI) -> fun_hints(POI, ExportPOIs) end, FunPOIs),

?LOG_DEBUG(
"Inlay hints took ~p ms",
[timer:now_diff(erlang:timestamp(), TS) div 1000]
),
Res.
ArgHints ++ FunHints.

%% @doc Output inlay hints for a function clause,
%% indicate if a function is exported or not
-spec fun_hints(els_poi:poi(), [els_poi:poi()]) -> [inlay_hint()].
fun_hints(#{id := {F, A, _}, range := Range}, ExportPOIs) ->
case lists:any(fun(#{id := Id}) -> Id == {F, A} end, ExportPOIs) of
true ->
[fun_exported_hint(Range)];
false ->
[]
end.

-spec fun_exported_hint(els_poi:poi_range()) -> inlay_hint().
fun_exported_hint(#{from := {FromL, FromC}}) ->
#{
position => #{line => FromL - 1, character => FromC - 1},
label => unicode:characters_to_binary("exp"),
paddingRight => true,
kind => ?INLAY_HINT_KIND_TYPE
}.

-spec arg_hints(uri(), els_poi:poi()) -> [inlay_hint()].
arg_hints(Uri, #{kind := application, data := #{args := CallArgs}} = POI) ->
Expand Down
6 changes: 6 additions & 0 deletions apps/els_lsp/test/els_inlay_hint_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,12 @@ basic(Config) ->
position => #{line => 6, character => 9},
kind => ?INLAY_HINT_KIND_PARAMETER,
paddingRight => true
},
#{
label => <<"exp">>,
position => #{line => 5, character => 0},
kind => ?INLAY_HINT_KIND_TYPE,
paddingRight => true
}
],
Result
Expand Down
Loading