Skip to content

Commit 81c4f54

Browse files
committed
handle or patterns (including broken patterns) in variant/polyvariant payloads
1 parent bcc0272 commit 81c4f54

File tree

3 files changed

+112
-22
lines changed

3 files changed

+112
-22
lines changed

analysis/src/CompletionFrontEnd.ml

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -541,17 +541,6 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor ~text =
541541
{constructorName = getUnqualifiedName txt; itemNum = 1};
542542
]
543543
@ patternPath )
544-
| Ppat_construct ({txt}, Some pat)
545-
when locHasCursor pat.ppat_loc && isPatternTuple pat = false ->
546-
(* Single payload *)
547-
pat
548-
|> traversePattern
549-
~patternPath:
550-
([
551-
Completable.PVariantPayload
552-
{constructorName = getUnqualifiedName txt; itemNum = 0};
553-
]
554-
@ patternPath)
555544
| Ppat_construct ({txt}, Some {ppat_loc; ppat_desc = Ppat_tuple tupleItems})
556545
when locHasCursor ppat_loc ->
557546
tupleItems
@@ -571,6 +560,15 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor ~text =
571560
};
572561
]
573562
@ patternPath)
563+
| Ppat_construct ({txt}, Some p) when locHasCursor pat.ppat_loc ->
564+
p
565+
|> traversePattern
566+
~patternPath:
567+
([
568+
Completable.PVariantPayload
569+
{constructorName = getUnqualifiedName txt; itemNum = 0};
570+
]
571+
@ patternPath)
574572
| Ppat_variant
575573
( txt,
576574
Some {ppat_loc; ppat_desc = Ppat_construct ({txt = Lident "()"}, _)}
@@ -590,17 +588,6 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor ~text =
590588
( "",
591589
[Completable.PPolyvariantPayload {constructorName = txt; itemNum = 1}]
592590
@ patternPath )
593-
| Ppat_variant (txt, Some pat)
594-
when locHasCursor pat.ppat_loc && isPatternTuple pat = false ->
595-
(* Single payload *)
596-
pat
597-
|> traversePattern
598-
~patternPath:
599-
([
600-
Completable.PPolyvariantPayload
601-
{constructorName = txt; itemNum = 0};
602-
]
603-
@ patternPath)
604591
| Ppat_variant (txt, Some {ppat_loc; ppat_desc = Ppat_tuple tupleItems})
605592
when locHasCursor ppat_loc ->
606593
tupleItems
@@ -614,6 +601,15 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor ~text =
614601
{constructorName = txt; itemNum = itemNum + 1};
615602
]
616603
@ patternPath)
604+
| Ppat_variant (txt, Some p) when locHasCursor pat.ppat_loc ->
605+
p
606+
|> traversePattern
607+
~patternPath:
608+
([
609+
Completable.PPolyvariantPayload
610+
{constructorName = txt; itemNum = 0};
611+
]
612+
@ patternPath)
617613
| _ -> None
618614
in
619615
let completePattern (pat : Parsetree.pattern) =

analysis/tests/src/CompletionPattern.res

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,3 +179,15 @@ let s = (true, Some(true), [false])
179179

180180
// switch z { | One | }
181181
// ^com
182+
183+
// switch z { | One | Two(true | ) }
184+
// ^com
185+
186+
// switch z { | One | Three({test: true}, true | ) }
187+
// ^com
188+
189+
// switch b { | #one | #two(true | ) }
190+
// ^com
191+
192+
// switch b { | #one | #three({test: true}, true | ) }
193+
// ^com

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

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -752,3 +752,85 @@ Completable: Cpattern Value[z]
752752
"insertTextFormat": 2
753753
}]
754754

755+
Complete src/CompletionPattern.res 182:32
756+
looking for: Cpath Value[z]
757+
posCursor:[182:32] posNoWhite:[182:31] Found expr:[182:3->182:37]
758+
posCursor:[182:32] posNoWhite:[182:31] Found pattern:[182:16->182:34]
759+
posCursor:[182:32] posNoWhite:[182:31] Found pattern:[182:22->182:34]
760+
Completable: Cpattern Value[z]->variantPayload::Two($0)
761+
[{
762+
"label": "true",
763+
"kind": 4,
764+
"tags": [],
765+
"detail": "bool",
766+
"documentation": null
767+
}, {
768+
"label": "false",
769+
"kind": 4,
770+
"tags": [],
771+
"detail": "bool",
772+
"documentation": null
773+
}]
774+
775+
Complete src/CompletionPattern.res 185:48
776+
looking for: Cpath Value[z]
777+
posCursor:[185:48] posNoWhite:[185:47] Found expr:[185:3->185:53]
778+
posCursor:[185:48] posNoWhite:[185:47] Found pattern:[185:16->185:50]
779+
posCursor:[185:48] posNoWhite:[185:47] Found pattern:[185:22->185:50]
780+
posCursor:[185:48] posNoWhite:[185:47] Found pattern:[185:27->185:53]
781+
Completable: Cpattern Value[z]->variantPayload::Three($1)
782+
[{
783+
"label": "true",
784+
"kind": 4,
785+
"tags": [],
786+
"detail": "bool",
787+
"documentation": null
788+
}, {
789+
"label": "false",
790+
"kind": 4,
791+
"tags": [],
792+
"detail": "bool",
793+
"documentation": null
794+
}]
795+
796+
Complete src/CompletionPattern.res 188:34
797+
looking for: Cpath Value[b]
798+
posCursor:[188:34] posNoWhite:[188:33] Found expr:[188:3->188:39]
799+
posCursor:[188:34] posNoWhite:[188:33] Found pattern:[188:16->188:36]
800+
posCursor:[188:34] posNoWhite:[188:33] Found pattern:[188:23->188:36]
801+
Completable: Cpattern Value[b]->polyvariantPayload::two($0)
802+
[{
803+
"label": "true",
804+
"kind": 4,
805+
"tags": [],
806+
"detail": "bool",
807+
"documentation": null
808+
}, {
809+
"label": "false",
810+
"kind": 4,
811+
"tags": [],
812+
"detail": "bool",
813+
"documentation": null
814+
}]
815+
816+
Complete src/CompletionPattern.res 191:50
817+
looking for: Cpath Value[b]
818+
posCursor:[191:50] posNoWhite:[191:49] Found expr:[191:3->191:55]
819+
posCursor:[191:50] posNoWhite:[191:49] Found pattern:[191:16->191:52]
820+
posCursor:[191:50] posNoWhite:[191:49] Found pattern:[191:23->191:52]
821+
posCursor:[191:50] posNoWhite:[191:49] Found pattern:[191:29->191:52]
822+
Completable: Cpattern Value[b]->polyvariantPayload::three($1)
823+
[{
824+
"label": "true",
825+
"kind": 4,
826+
"tags": [],
827+
"detail": "bool",
828+
"documentation": null
829+
}, {
830+
"label": "false",
831+
"kind": 4,
832+
"tags": [],
833+
"detail": "bool",
834+
"documentation": null
835+
}]
836+

0 commit comments

Comments
 (0)