Skip to content

Commit 7d214ea

Browse files
committed
handle completing new items in tuples
1 parent 693166a commit 7d214ea

File tree

3 files changed

+99
-9
lines changed

3 files changed

+99
-9
lines changed

analysis/src/CompletionFrontEnd.ml

+27-9
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,9 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor ~text =
434434
| Ppat_or (p1, p2) ->
435435
[p1; p2] |> List.find_map (fun p -> p |> traversePattern ~patternPath)
436436
| Ppat_var {txt} -> Some (txt, patternPath)
437+
| Ppat_construct ({txt = Lident "()"}, None) ->
438+
(* switch s { | (<com>) }*)
439+
Some ("", patternPath @ [Completable.PTupleItem {itemNum = 0}])
437440
| Ppat_construct ({txt = Lident prefix}, None) ->
438441
Some (prefix, patternPath)
439442
| Ppat_variant (prefix, None) -> Some ("#" ^ prefix, patternPath)
@@ -444,16 +447,31 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor ~text =
444447
arrayPatterns
445448
|> List.find_map (fun pat ->
446449
pat |> traversePattern ~patternPath:nextPatternPath)
447-
| Ppat_tuple patterns ->
450+
| Ppat_tuple tupleItems -> (
448451
let itemNum = ref (-1) in
449-
patterns
450-
|> List.find_map (fun pat ->
451-
itemNum := !itemNum + 1;
452-
pat
453-
|> traversePattern
454-
~patternPath:
455-
([Completable.PTupleItem {itemNum = !itemNum}]
456-
@ patternPath))
452+
let itemWithCursor =
453+
tupleItems
454+
|> List.find_map (fun pat ->
455+
itemNum := !itemNum + 1;
456+
pat
457+
|> traversePattern
458+
~patternPath:
459+
([Completable.PTupleItem {itemNum = !itemNum}]
460+
@ patternPath))
461+
in
462+
match (itemWithCursor, firstCharBeforeCursorNoWhite) with
463+
| None, Some ',' -> (
464+
(* No tuple item has the cursor, but there's a comma before the cursor.
465+
Figure out what arg we're trying to complete. Example: #test(true, <com>, None) *)
466+
let locs = tupleItems |> List.map (fun p -> p.Parsetree.ppat_loc) in
467+
match locs |> lastLocIndexBeforePos ~pos:posBeforeCursor with
468+
| None -> None
469+
| Some itemNum ->
470+
Some
471+
( "",
472+
[Completable.PTupleItem {itemNum = itemNum + 1}] @ patternPath
473+
))
474+
| v, _ -> v)
457475
| Ppat_record ([], _) ->
458476
(* Empty fields means we're in a record body `{}`. Complete for the fields. *)
459477
Some ("", [Completable.PRecordBody {seenFields = []}] @ patternPath)

analysis/tests/src/CompletionPattern.res

+11
Original file line numberDiff line numberDiff line change
@@ -159,3 +159,14 @@ let v: multiPayloadPolyVariant = #test(1, true, Some(false), [])
159159

160160
// switch v { | #test(1, true, None, )}
161161
// ^com
162+
163+
let s = (true, Some(true), [false])
164+
165+
// switch s { | () }
166+
// ^com
167+
168+
// switch s { | (true, ) }
169+
// ^com
170+
171+
// switch s { | (true, , []) }
172+
// ^com

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

+61
Original file line numberDiff line numberDiff line change
@@ -629,3 +629,64 @@ Completable: Cpattern Value[v]->polyvariantPayload::test($3)
629629
"insertTextFormat": 2
630630
}]
631631

632+
Complete src/CompletionPattern.res 164:17
633+
looking for: Cpath Value[s]
634+
posCursor:[164:17] posNoWhite:[164:16] Found expr:[164:3->164:20]
635+
posCursor:[164:17] posNoWhite:[164:16] Found pattern:[164:16->164:18]
636+
Completable: Cpattern Value[s]->tuple($0)
637+
[{
638+
"label": "true",
639+
"kind": 4,
640+
"tags": [],
641+
"detail": "bool",
642+
"documentation": null
643+
}, {
644+
"label": "false",
645+
"kind": 4,
646+
"tags": [],
647+
"detail": "bool",
648+
"documentation": null
649+
}]
650+
651+
Complete src/CompletionPattern.res 167:23
652+
looking for: Cpath Value[s]
653+
posCursor:[167:23] posNoWhite:[167:21] Found expr:[167:3->167:26]
654+
posCursor:[167:23] posNoWhite:[167:21] Found pattern:[167:16->167:24]
655+
Completable: Cpattern Value[s]->tuple($1)
656+
[{
657+
"label": "None",
658+
"kind": 4,
659+
"tags": [],
660+
"detail": "bool",
661+
"documentation": null
662+
}, {
663+
"label": "Some(_)",
664+
"kind": 4,
665+
"tags": [],
666+
"detail": "bool",
667+
"documentation": null,
668+
"insertText": "Some(${1:_})",
669+
"insertTextFormat": 2
670+
}]
671+
672+
Complete src/CompletionPattern.res 170:22
673+
looking for: Cpath Value[s]
674+
posCursor:[170:22] posNoWhite:[170:21] Found expr:[170:3->170:30]
675+
posCursor:[170:22] posNoWhite:[170:21] Found pattern:[170:16->170:28]
676+
Completable: Cpattern Value[s]->tuple($1)
677+
[{
678+
"label": "None",
679+
"kind": 4,
680+
"tags": [],
681+
"detail": "bool",
682+
"documentation": null
683+
}, {
684+
"label": "Some(_)",
685+
"kind": 4,
686+
"tags": [],
687+
"detail": "bool",
688+
"documentation": null,
689+
"insertText": "Some(${1:_})",
690+
"insertTextFormat": 2
691+
}]
692+

0 commit comments

Comments
 (0)