Skip to content

Commit 07d60b2

Browse files
committed
more advanced completion for arrays
1 parent 5f2d21c commit 07d60b2

File tree

3 files changed

+131
-6
lines changed

3 files changed

+131
-6
lines changed

analysis/src/CompletionFrontEnd.ml

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,29 @@ let rec traverseExpr (exp : Parsetree.expression) ~exprPath ~pos
3939
| Pexp_construct ({txt = Lident "()"}, _) -> someIfHasCursor ("", exprPath)
4040
| Pexp_construct ({txt = Lident txt}, None) -> someIfHasCursor (txt, exprPath)
4141
| Pexp_variant (label, None) -> someIfHasCursor ("#" ^ label, exprPath)
42-
| Pexp_array arrayPatterns ->
42+
| Pexp_array arrayPatterns -> (
4343
let nextExprPath = [Completable.EArray] @ exprPath in
44+
(* No fields but still has cursor = empty completion *)
4445
if List.length arrayPatterns = 0 && locHasCursor exp.pexp_loc then
4546
Some ("", nextExprPath)
4647
else
47-
arrayPatterns
48-
|> List.find_map (fun e ->
49-
e
50-
|> traverseExpr ~exprPath:nextExprPath
51-
~firstCharBeforeCursorNoWhite ~pos)
48+
let arrayItemWithCursor =
49+
arrayPatterns
50+
|> List.find_map (fun e ->
51+
e
52+
|> traverseExpr ~exprPath:nextExprPath
53+
~firstCharBeforeCursorNoWhite ~pos)
54+
in
55+
56+
match (arrayItemWithCursor, locHasCursor exp.pexp_loc) with
57+
| Some arrayItemWithCursor, _ -> Some arrayItemWithCursor
58+
| None, true when firstCharBeforeCursorNoWhite = Some ',' ->
59+
(* No item had the cursor, but the entire expr still has the cursor (so
60+
the cursor is in the array somewhere), and the first char before the
61+
cursor is a comma = interpret as compleing for a new value (example:
62+
`[None, <com>, None]`) *)
63+
Some ("", nextExprPath)
64+
| _ -> None)
5265
| Pexp_tuple tupleItems when locHasCursor exp.pexp_loc ->
5366
tupleItems
5467
|> traverseExprTupleItems ~firstCharBeforeCursorNoWhite ~pos

analysis/tests/src/CompletionExpressions.res

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,22 @@ let fnTakingRecord = (r: someRecord) => {
6262

6363
// let _ = fnTakingRecord({age: 123, polyvariant: #three({}, t) })
6464
// ^com
65+
66+
let fnTakingArray = (arr: array<option<bool>>) => {
67+
ignore(arr)
68+
}
69+
70+
// let _ = fnTakingArray()
71+
// ^com
72+
73+
// let _ = fnTakingArray([])
74+
// ^com
75+
76+
// let _ = fnTakingArray([Some()])
77+
// ^com
78+
79+
// let _ = fnTakingArray([None, ])
80+
// ^com
81+
82+
// let _ = fnTakingArray([None, , None])
83+
// ^com

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

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,3 +282,96 @@ Completable: Cargument Value[fnTakingRecord]($0=t)->recordField(polyvariant), po
282282
"documentation": null
283283
}]
284284

285+
Complete src/CompletionExpressions.res 69:25
286+
posCursor:[69:25] posNoWhite:[69:24] Found expr:[69:11->69:26]
287+
Pexp_apply ...[69:11->69:24] (...[69:25->69:26])
288+
Completable: Cargument Value[fnTakingArray]($0)
289+
[{
290+
"label": "[]",
291+
"kind": 12,
292+
"tags": [],
293+
"detail": "option<bool>",
294+
"documentation": null,
295+
"sortText": "a",
296+
"insertText": "[$0]",
297+
"insertTextFormat": 2
298+
}]
299+
300+
Complete src/CompletionExpressions.res 72:26
301+
posCursor:[72:26] posNoWhite:[72:25] Found expr:[72:11->72:28]
302+
Pexp_apply ...[72:11->72:24] (...[72:25->72:27])
303+
Completable: Cargument Value[fnTakingArray]($0)->array
304+
[{
305+
"label": "None",
306+
"kind": 4,
307+
"tags": [],
308+
"detail": "bool",
309+
"documentation": null
310+
}, {
311+
"label": "Some(_)",
312+
"kind": 4,
313+
"tags": [],
314+
"detail": "bool",
315+
"documentation": null,
316+
"insertText": "Some(${1:_})",
317+
"insertTextFormat": 2
318+
}]
319+
320+
Complete src/CompletionExpressions.res 75:31
321+
posCursor:[75:31] posNoWhite:[75:30] Found expr:[75:11->75:34]
322+
Pexp_apply ...[75:11->75:24] (...[75:25->75:33])
323+
Completable: Cargument Value[fnTakingArray]($0)->array, variantPayload::Some($0)
324+
[{
325+
"label": "true",
326+
"kind": 4,
327+
"tags": [],
328+
"detail": "bool",
329+
"documentation": null
330+
}, {
331+
"label": "false",
332+
"kind": 4,
333+
"tags": [],
334+
"detail": "bool",
335+
"documentation": null
336+
}]
337+
338+
Complete src/CompletionExpressions.res 78:31
339+
posCursor:[78:31] posNoWhite:[78:30] Found expr:[78:11->78:34]
340+
Pexp_apply ...[78:11->78:24] (...[78:25->78:33])
341+
Completable: Cargument Value[fnTakingArray]($0)->array
342+
[{
343+
"label": "None",
344+
"kind": 4,
345+
"tags": [],
346+
"detail": "bool",
347+
"documentation": null
348+
}, {
349+
"label": "Some(_)",
350+
"kind": 4,
351+
"tags": [],
352+
"detail": "bool",
353+
"documentation": null,
354+
"insertText": "Some(${1:_})",
355+
"insertTextFormat": 2
356+
}]
357+
358+
Complete src/CompletionExpressions.res 81:31
359+
posCursor:[81:31] posNoWhite:[81:30] Found expr:[81:11->81:40]
360+
Pexp_apply ...[81:11->81:24] (...[81:25->81:39])
361+
Completable: Cargument Value[fnTakingArray]($0)->array
362+
[{
363+
"label": "None",
364+
"kind": 4,
365+
"tags": [],
366+
"detail": "bool",
367+
"documentation": null
368+
}, {
369+
"label": "Some(_)",
370+
"kind": 4,
371+
"tags": [],
372+
"detail": "bool",
373+
"documentation": null,
374+
"insertText": "Some(${1:_})",
375+
"insertTextFormat": 2
376+
}]
377+

0 commit comments

Comments
 (0)