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

Simplify attribute_spec code #1543

Merged
merged 1 commit into from
Sep 20, 2024
Merged
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: 7 additions & 24 deletions apps/els_lsp/src/els_completion_provider.erl
Original file line number Diff line number Diff line change
Expand Up @@ -379,25 +379,6 @@ 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 @@ -530,8 +511,12 @@ docs_attributes() ->

-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}}} ->
POIs = els_incomplete_parser:parse_after(Text, Line),
case [P || #{kind := function} = P <- POIs] of
[] ->
[];
FunPOIs ->
[#{id := {Id, Arity}} | _] = els_poi:sort(FunPOIs),
Args = [els_arg:new(I, "_") || I <- lists:seq(1, Arity)],
SnippetSupport = snippet_support(),
FunBin = format_function(Id, Args, SnippetSupport, spec),
Expand All @@ -543,9 +528,7 @@ attribute_spec(#{text := Text}, Line) ->
N = integer_to_binary(Arity + 1),
<<" -> ${", N/binary, ":_}.">>
end,
[snippet(<<"-spec">>, <<"spec ", FunBin/binary, RetBin/binary>>)];
error ->
[]
[snippet(<<"-spec">>, <<"spec ", FunBin/binary, RetBin/binary>>)]
end.

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