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 support for record field hover docs and provide index information… #1586

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
62 changes: 62 additions & 0 deletions apps/els_lsp/src/els_docs.erl
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,48 @@ docs(Uri, #{kind := record_expr} = POI) ->
_ ->
[]
end;
docs(Uri, #{kind := record_field, id := {Rid, Field}} = POI) ->
case els_code_navigation:goto_definition(Uri, POI) of
{ok, [{DefUri, #{range := #{from := From}}}]} ->
{ok, #{pois := POIs}} = els_utils:lookup_document(DefUri),
case lists:search(
fun(#{id := Id, kind := Kind}) ->
Id == Rid andalso Kind == record
end, POIs)
of
{value, #{data := #{field_list := FieldList, value_range := #{to := {Line, C}}}}} ->
{To, Index} = record_field_docs(FieldList, Field, {Line, C - 3}, 2, Rid, POIs),
ValueText = get_valuetext(DefUri, #{from => From, to => To}),
[{code_line, string:trim(ValueText, trailing)},
{text, "---"}, {text, lists:concat(["index: ", Index])}];
false ->
[]
end;
_ ->
[]
end;
docs(Uri, #{kind := record_def_field, id := {Rid, Field}}) ->
{ok, #{pois := POIs}} = els_utils:lookup_document(Uri),
case lists:search(
fun(#{id := Id, kind := Kind}) ->
Id == Rid andalso Kind == record
end, POIs)
of
{value, #{data := #{field_list := FieldList}}} ->
Index =
catch lists:foldl(
fun(Elem, AccIn) ->
case Elem of
Field ->
throw(AccIn);
_ ->
AccIn + 1
end
end, 2, FieldList),
[{text, lists:concat(["index: ", Index])}];
false ->
[]
end;
docs(_M, #{kind := Kind, id := {M, F, A}}) when
Kind =:= type_application;
Kind =:= type_definition
Expand Down Expand Up @@ -157,6 +199,26 @@ function_docs(Type, M, F, A, false = _DocsMemo) ->
end
end.

-spec record_field_docs([atom()], atom(), tuple(), integer(), atom(), [els_poi:poi()]) ->
{tuple(), integer()}.
record_field_docs([], _Field, To, Index, _Rid, _POIs) ->
{To, Index};
record_field_docs([Field], Field, To, Index, _Rid, _POIs) ->
{To, Index};
record_field_docs([Field, Next | _], Field, To, Index, Rid, POIs) ->
case lists:search(
fun(#{id := Id, kind := Kind}) ->
Id == {Rid, Next} andalso Kind == record_def_field
end, POIs)
of
{value, #{range := #{from := From}}} ->
{From, Index};
false ->
{To, Index}
end;
record_field_docs([_ | T], Field, To, Index, Rid, POIs) ->
record_field_docs(T, Field, To, Index + 1, Rid, POIs).

-spec type_docs(application_type(), atom(), atom(), non_neg_integer()) ->
[els_markup_content:doc_entry()].
type_docs(Type, M, F, A) ->
Expand Down