@@ -195,9 +195,9 @@ find_completions(
195
195
find_completions (
196
196
_Prefix ,
197
197
? COMPLETION_TRIGGER_KIND_CHARACTER ,
198
- #{trigger := <<" -" >>, document := Document , column := 1 }
198
+ #{trigger := <<" -" >>, document := Document , column := 1 , line : = Line }
199
199
) ->
200
- attributes (Document );
200
+ attributes (Document , Line );
201
201
find_completions (
202
202
_Prefix ,
203
203
? COMPLETION_TRIGGER_KIND_CHARACTER ,
@@ -304,7 +304,7 @@ find_completions(
304
304
variables (Document );
305
305
% % Check for "-anything"
306
306
[{atom , _ , _ }, {'-' , _ }] ->
307
- attributes (Document );
307
+ attributes (Document , Line );
308
308
% % Check for "-export(["
309
309
[{'[' , _ }, {'(' , _ }, {atom , _ , export }, {'-' , _ }] ->
310
310
unexported_definitions (Document , function );
@@ -379,6 +379,25 @@ find_completions(
379
379
find_completions (_Prefix , _TriggerKind , _Opts ) ->
380
380
[].
381
381
382
+ -spec try_to_parse_next_function (binary (), line (), line ()) ->
383
+ {ok , els_poi :poi ()} | error .
384
+ try_to_parse_next_function (Text , FromL , ToL ) when ToL - FromL < 50 ->
385
+ try els_text :range (Text , {FromL , 1 }, {ToL , 1 }) of
386
+ Str ->
387
+ {ok , POIs } = els_parser :parse (Str ),
388
+ case [P || #{kind := function } = P <- POIs ] of
389
+ [POI | _ ] ->
390
+ {ok , POI };
391
+ _ ->
392
+ try_to_parse_next_function (Text , FromL , ToL + 1 )
393
+ end
394
+ catch
395
+ _ :_ ->
396
+ error
397
+ end ;
398
+ try_to_parse_next_function (_ , _ , _ ) ->
399
+ error .
400
+
382
401
-spec complete_record_field (map (), list ()) -> items ().
383
402
complete_record_field (_Opts , [{atom , _ , _ }, {'=' , _ } | _ ]) ->
384
403
[];
@@ -458,8 +477,8 @@ complete_type_definition(Document, Name, ItemFormat) ->
458
477
% %=============================================================================
459
478
% % Attributes
460
479
% %=============================================================================
461
- -spec attributes (els_dt_document :item ()) -> items ().
462
- attributes (Document ) ->
480
+ -spec attributes (els_dt_document :item (), line () ) -> items ().
481
+ attributes (Document , Line ) ->
463
482
[
464
483
snippet (attribute_behaviour ),
465
484
snippet (attribute_callback ),
@@ -481,7 +500,7 @@ attributes(Document) ->
481
500
snippet (attribute_type ),
482
501
snippet (attribute_vsn ),
483
502
attribute_module (Document )
484
- ] ++ docs_attributes ().
503
+ ] ++ docs_attributes () ++ attribute_spec ( Document , Line ) .
485
504
486
505
-spec attribute_module (els_dt_document :item ()) -> item ().
487
506
attribute_module (#{id := Id }) ->
@@ -509,6 +528,26 @@ docs_attributes() ->
509
528
[].
510
529
-endif .
511
530
531
+ -spec attribute_spec (Document :: els_dt_document :item (), line ()) -> items ().
532
+ attribute_spec (#{text := Text }, Line ) ->
533
+ case try_to_parse_next_function (Text , Line + 1 , Line + 2 ) of
534
+ {ok , #{id := {Id , Arity }}} ->
535
+ Args = [els_arg :new (I , " _" ) || I <- lists :seq (1 , Arity )],
536
+ SnippetSupport = snippet_support (),
537
+ FunBin = format_function (Id , Args , SnippetSupport , spec ),
538
+ RetBin =
539
+ case SnippetSupport of
540
+ false ->
541
+ <<" -> _." >>;
542
+ true ->
543
+ N = integer_to_binary (Arity + 1 ),
544
+ <<" -> ${" , N /binary , " :_}." >>
545
+ end ,
546
+ [snippet (<<" -spec" >>, <<" spec " , FunBin /binary , RetBin /binary >>)];
547
+ error ->
548
+ []
549
+ end .
550
+
512
551
% %=============================================================================
513
552
% % Include paths
514
553
% %=============================================================================
0 commit comments