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 -spec attribute completion #1541

Merged
merged 1 commit into from
Sep 19, 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
51 changes: 45 additions & 6 deletions apps/els_lsp/src/els_completion_provider.erl
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,9 @@ find_completions(
find_completions(
_Prefix,
?COMPLETION_TRIGGER_KIND_CHARACTER,
#{trigger := <<"-">>, document := Document, column := 1}
#{trigger := <<"-">>, document := Document, column := 1, line := Line}
) ->
attributes(Document);
attributes(Document, Line);
find_completions(
_Prefix,
?COMPLETION_TRIGGER_KIND_CHARACTER,
Expand Down Expand Up @@ -304,7 +304,7 @@ find_completions(
variables(Document);
%% Check for "-anything"
[{atom, _, _}, {'-', _}] ->
attributes(Document);
attributes(Document, Line);
%% Check for "-export(["
[{'[', _}, {'(', _}, {atom, _, export}, {'-', _}] ->
unexported_definitions(Document, function);
Expand Down Expand Up @@ -379,6 +379,25 @@ find_completions(
find_completions(_Prefix, _TriggerKind, _Opts) ->
[].

-spec try_to_parse_next_function(binary(), line(), line()) ->
{ok, els_poi:poi()} | error.
try_to_parse_next_function(Text, FromL, ToL) when ToL - FromL < 50 ->
try els_text:range(Text, {FromL, 1}, {ToL, 1}) of
Str ->
{ok, POIs} = els_parser:parse(Str),
case [P || #{kind := function} = P <- POIs] of
[POI | _] ->
{ok, POI};
_ ->
try_to_parse_next_function(Text, FromL, ToL + 1)
end
catch
_:_ ->
error
end;
try_to_parse_next_function(_, _, _) ->
error.

-spec complete_record_field(map(), list()) -> items().
complete_record_field(_Opts, [{atom, _, _}, {'=', _} | _]) ->
[];
Expand Down Expand Up @@ -458,8 +477,8 @@ complete_type_definition(Document, Name, ItemFormat) ->
%%=============================================================================
%% Attributes
%%=============================================================================
-spec attributes(els_dt_document:item()) -> items().
attributes(Document) ->
-spec attributes(els_dt_document:item(), line()) -> items().
attributes(Document, Line) ->
[
snippet(attribute_behaviour),
snippet(attribute_callback),
Expand All @@ -481,7 +500,7 @@ attributes(Document) ->
snippet(attribute_type),
snippet(attribute_vsn),
attribute_module(Document)
] ++ docs_attributes().
] ++ docs_attributes() ++ attribute_spec(Document, Line).

-spec attribute_module(els_dt_document:item()) -> item().
attribute_module(#{id := Id}) ->
Expand Down Expand Up @@ -509,6 +528,26 @@ docs_attributes() ->
[].
-endif.

-spec attribute_spec(Document :: els_dt_document:item(), line()) -> items().
attribute_spec(#{text := Text}, Line) ->
case try_to_parse_next_function(Text, Line + 1, Line + 2) of
{ok, #{id := {Id, Arity}}} ->
Args = [els_arg:new(I, "_") || I <- lists:seq(1, Arity)],
SnippetSupport = snippet_support(),
FunBin = format_function(Id, Args, SnippetSupport, spec),
RetBin =
case SnippetSupport of
false ->
<<" -> _.">>;
true ->
N = integer_to_binary(Arity + 1),
<<" -> ${", N/binary, ":_}.">>
end,
[snippet(<<"-spec">>, <<"spec ", FunBin/binary, RetBin/binary>>)];
error ->
[]
end.

%%=============================================================================
%% Include paths
%%=============================================================================
Expand Down
6 changes: 6 additions & 0 deletions apps/els_lsp/test/els_completion_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,12 @@ attributes(Config) ->
insertTextFormat => ?INSERT_TEXT_FORMAT_SNIPPET,
kind => ?COMPLETION_ITEM_KIND_SNIPPET,
label => <<"-module(completion_attributes).">>
},
#{
insertText => <<"spec exported_function() -> ${1:_}.">>,
insertTextFormat => ?INSERT_TEXT_FORMAT_SNIPPET,
kind => ?COMPLETION_ITEM_KIND_SNIPPET,
label => <<"-spec">>
}
] ++ docs_attributes(),
#{result := Completions} =
Expand Down
Loading