Skip to content

Commit fe57708

Browse files
committed
add snippets to constructor args when completing
1 parent 1411a6a commit fe57708

File tree

2 files changed

+34
-15
lines changed

2 files changed

+34
-15
lines changed

analysis/src/CompletionBackEnd.ml

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1528,6 +1528,15 @@ let filterItems items ~prefix =
15281528
|> List.filter (fun (item : Completion.t) ->
15291529
Utils.startsWith item.name prefix)
15301530

1531+
let printConstructorArgs argsLen ~asSnippet =
1532+
let args = ref [] in
1533+
for argNum = 1 to argsLen do
1534+
args :=
1535+
!args @ [(if asSnippet then Printf.sprintf "${%i:_}" argNum else "_")]
1536+
done;
1537+
if List.length !args > 0 then "(" ^ (!args |> String.concat ", ") ^ ")"
1538+
else ""
1539+
15311540
let completeTypedValue ~env ~envWhereCompletionStarted ~full ~prefix
15321541
~expandOption =
15331542
let namesUsed = Hashtbl.create 10 in
@@ -1549,22 +1558,22 @@ let completeTypedValue ~env ~envWhereCompletionStarted ~full ~prefix
15491558
| Some (Tvariant {env; constructors; variantDecl; variantName}) ->
15501559
constructors
15511560
|> List.map (fun (constructor : Constructor.t) ->
1552-
Completion.create
1561+
Completion.createWithSnippet
15531562
~name:
15541563
(constructor.cname.txt
1555-
^
1556-
if constructor.args |> List.length > 0 then
1557-
"("
1558-
^ (constructor.args
1559-
|> List.map (fun _ -> "_")
1560-
|> String.concat ", ")
1561-
^ ")"
1562-
else "")
1564+
^ printConstructorArgs
1565+
(List.length constructor.args)
1566+
~asSnippet:false)
1567+
~insertText:
1568+
(constructor.cname.txt
1569+
^ printConstructorArgs
1570+
(List.length constructor.args)
1571+
~asSnippet:true)
15631572
~kind:
15641573
(Constructor
15651574
( constructor,
15661575
variantDecl |> Shared.declToString variantName ))
1567-
~env)
1576+
~env ())
15681577
|> filterItems ~prefix
15691578
| Some (Toption (env, t)) ->
15701579
[

analysis/tests/src/expected/CompletionFunctionArguments.res.txt

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,19 +95,25 @@ Completable: Cargument Value[someFnTakingVariant](~config)
9595
"kind": 4,
9696
"tags": [],
9797
"detail": "One\n\ntype someVariant = One | Two | Three(int, string)",
98-
"documentation": null
98+
"documentation": null,
99+
"insertText": "One",
100+
"insertTextFormat": 2
99101
}, {
100102
"label": "Two",
101103
"kind": 4,
102104
"tags": [],
103105
"detail": "Two\n\ntype someVariant = One | Two | Three(int, string)",
104-
"documentation": null
106+
"documentation": null,
107+
"insertText": "Two",
108+
"insertTextFormat": 2
105109
}, {
106110
"label": "Three(_, _)",
107111
"kind": 4,
108112
"tags": [],
109113
"detail": "Three(int, string)\n\ntype someVariant = One | Two | Three(int, string)",
110-
"documentation": null
114+
"documentation": null,
115+
"insertText": "Three(${1:_}, ${2:_})",
116+
"insertTextFormat": 2
111117
}]
112118

113119
Complete src/CompletionFunctionArguments.res 54:40
@@ -119,7 +125,9 @@ Completable: Cargument Value[someFnTakingVariant](~config=O)
119125
"kind": 4,
120126
"tags": [],
121127
"detail": "One\n\ntype someVariant = One | Two | Three(int, string)",
122-
"documentation": null
128+
"documentation": null,
129+
"insertText": "One",
130+
"insertTextFormat": 2
123131
}, {
124132
"label": "OIncludeMeInCompletions",
125133
"kind": 9,
@@ -151,7 +159,9 @@ Completable: Cargument Value[someFnTakingVariant](~configOpt2=O)
151159
"kind": 4,
152160
"tags": [],
153161
"detail": "One\n\ntype someVariant = One | Two | Three(int, string)",
154-
"documentation": null
162+
"documentation": null,
163+
"insertText": "One",
164+
"insertTextFormat": 2
155165
}, {
156166
"label": "OIncludeMeInCompletions",
157167
"kind": 9,

0 commit comments

Comments
 (0)