Skip to content

Commit 60f8849

Browse files
committed
handle multiple payloads in variants/polyvariants via regular tuple handling
1 parent bed266e commit 60f8849

File tree

5 files changed

+85
-44
lines changed

5 files changed

+85
-44
lines changed

analysis/src/CompletionBackEnd.ml

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1780,19 +1780,24 @@ let rec resolveNestedPattern typ ~env ~package ~nested =
17801780
| Some {typ} -> typ |> resolveNestedPattern ~env ~package ~nested)
17811781
| PRecordBody {seenFields}, Some (Trecord {env; typeExpr}) ->
17821782
Some (typeExpr, env, Some (Completable.RecordField {seenFields}))
1783-
| ( PVariantPayload {constructorName; payloadNum},
1784-
Some (Tvariant {env; constructors}) ) -> (
1783+
| PVariantPayload {constructorName}, Some (Tvariant {env; constructors})
1784+
-> (
17851785
match
17861786
constructors
17871787
|> List.find_opt (fun (c : Constructor.t) ->
17881788
c.cname.txt = constructorName)
17891789
with
17901790
| None -> None
17911791
| Some constructor -> (
1792+
let payloadNum, nested =
1793+
match nested with
1794+
| PTupleItem {itemNum} :: nested -> (itemNum, nested)
1795+
| _ -> (0, nested)
1796+
in
17921797
match List.nth_opt constructor.args payloadNum with
17931798
| None -> None
17941799
| Some (typ, _) -> typ |> resolveNestedPattern ~env ~package ~nested))
1795-
| ( PPolyvariantPayload {constructorName; payloadNum},
1800+
| ( PPolyvariantPayload {constructorName},
17961801
Some (Tpolyvariant {env; constructors}) ) -> (
17971802
match
17981803
constructors
@@ -1801,6 +1806,11 @@ let rec resolveNestedPattern typ ~env ~package ~nested =
18011806
with
18021807
| None -> None
18031808
| Some constructor -> (
1809+
let payloadNum, nested =
1810+
match nested with
1811+
| PTupleItem {itemNum} :: nested -> (itemNum, nested)
1812+
| _ -> (0, nested)
1813+
in
18041814
match List.nth_opt constructor.args payloadNum with
18051815
| None -> None
18061816
| Some typ -> typ |> resolveNestedPattern ~env ~package ~nested))

analysis/src/CompletionFrontEnd.ml

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor ~text =
578578
(* Empty payload *)
579579
appendNestedPat
580580
(Completable.PVariantPayload
581-
{constructorName = getUnqualifiedName txt; payloadNum = 0});
581+
{constructorName = getUnqualifiedName txt});
582582
commitFoundPat ~prefix:"" ()
583583
| Ppat_construct
584584
( {txt},
@@ -594,7 +594,7 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor ~text =
594594
(* Single payload *)
595595
appendNestedPat
596596
(Completable.PVariantPayload
597-
{constructorName = getUnqualifiedName txt; payloadNum = 0})
597+
{constructorName = getUnqualifiedName txt})
598598
| Ppat_construct
599599
({txt}, Some {ppat_loc; ppat_desc = Ppat_tuple tupleItems})
600600
when ppat_loc
@@ -604,10 +604,10 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor ~text =
604604
(* TODO: New item with comma *)
605605
match tupleItems |> findPatTupleItemWithCursor ~pos:posBeforeCursor with
606606
| None -> ()
607-
| Some payloadNum ->
607+
| Some _ ->
608608
appendNestedPat
609609
(Completable.PVariantPayload
610-
{constructorName = getUnqualifiedName txt; payloadNum}))
610+
{constructorName = getUnqualifiedName txt}))
611611
| Ppat_variant
612612
( txt,
613613
Some {ppat_loc; ppat_desc = Ppat_construct ({txt = Lident "()"}, _)}
@@ -617,8 +617,7 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor ~text =
617617
= HasCursor ->
618618
(* Empty payload *)
619619
appendNestedPat
620-
(Completable.PPolyvariantPayload
621-
{constructorName = txt; payloadNum = 0});
620+
(Completable.PPolyvariantPayload {constructorName = txt});
622621
commitFoundPat ~prefix:"" ()
623622
| Ppat_variant
624623
( txt,
@@ -633,8 +632,7 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor ~text =
633632
= HasCursor ->
634633
(* Single payload *)
635634
appendNestedPat
636-
(Completable.PPolyvariantPayload
637-
{constructorName = txt; payloadNum = 0})
635+
(Completable.PPolyvariantPayload {constructorName = txt})
638636
| Ppat_variant (txt, Some {ppat_loc; ppat_desc = Ppat_tuple tupleItems})
639637
when ppat_loc
640638
|> CursorPosition.classifyLoc ~pos:posBeforeCursor
@@ -643,10 +641,9 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor ~text =
643641
(* TODO: New item with comma *)
644642
match tupleItems |> findPatTupleItemWithCursor ~pos:posBeforeCursor with
645643
| None -> ()
646-
| Some payloadNum ->
644+
| Some _ ->
647645
appendNestedPat
648-
(Completable.PPolyvariantPayload {constructorName = txt; payloadNum})
649-
)
646+
(Completable.PPolyvariantPayload {constructorName = txt}))
650647
| _ -> ()
651648
in
652649
let case (iterator : Ast_iterator.iterator) (case : Parsetree.case) =

analysis/src/SharedTypes.ml

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -557,21 +557,19 @@ module Completable = struct
557557
| PTupleItem of {itemNum: int}
558558
| PFollowRecordField of {fieldName: string}
559559
| PRecordBody of {seenFields: string list}
560-
| PVariantPayload of {constructorName: string; payloadNum: int}
561-
| PPolyvariantPayload of {constructorName: string; payloadNum: int}
560+
| PVariantPayload of {constructorName: string}
561+
| PPolyvariantPayload of {constructorName: string}
562562
| PArray
563563

564564
let patternPathToString p =
565565
match p with
566566
| PTupleItem {itemNum} -> "tuple($" ^ string_of_int itemNum ^ ")"
567567
| PFollowRecordField {fieldName} -> "recordField(" ^ fieldName ^ ")"
568568
| PRecordBody _ -> "recordBody"
569-
| PVariantPayload {constructorName; payloadNum} ->
570-
"variantPayload::" ^ constructorName ^ "($" ^ string_of_int payloadNum
571-
^ ")"
572-
| PPolyvariantPayload {constructorName; payloadNum} ->
573-
"polyvariantPayload::" ^ constructorName ^ "($" ^ string_of_int payloadNum
574-
^ ")"
569+
| PVariantPayload {constructorName} ->
570+
"variantPayload(" ^ constructorName ^ ")"
571+
| PPolyvariantPayload {constructorName} ->
572+
"polyvariantPayload(" ^ constructorName ^ ")"
575573
| PArray -> "array"
576574

577575
type t =

analysis/tests/src/CompletionPattern.res

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ ignore(z)
9393
// switch z { | Three({})}
9494
// ^com
9595

96+
// switch z { | Three({}, t)}
97+
// ^com
98+
9699
type somePolyVariant = [#one | #two(bool) | #three(someRecord, bool)]
97100
let b: somePolyVariant = #two(true)
98101
ignore(b)
@@ -106,6 +109,9 @@ ignore(b)
106109
// switch b { | #three({})}
107110
// ^com
108111

112+
// switch b { | #three({}, t)}
113+
// ^com
114+
109115
let c: array<bool> = []
110116
ignore(c)
111117

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

Lines changed: 52 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ looking for: Cpath Value[z]
263263
posCursor:[86:20] posNoWhite:[86:19] Found expr:[86:3->86:22]
264264
posCursor:[86:20] posNoWhite:[86:19] Found pattern:[86:16->86:21]
265265
posCursor:[86:20] posNoWhite:[86:19] Found pattern:[86:19->86:21]
266-
Completable: Cpattern Value[z]->variantPayload::Two($0)
266+
Completable: Cpattern Value[z]->variantPayload(Two)
267267
[{
268268
"label": "true",
269269
"kind": 4,
@@ -283,7 +283,7 @@ looking for: Cpath Value[z]
283283
posCursor:[89:21] posNoWhite:[89:20] Found expr:[89:3->89:23]
284284
posCursor:[89:21] posNoWhite:[89:20] Found pattern:[89:16->89:22]
285285
posCursor:[89:21] posNoWhite:[89:20] Found pattern:[89:20->89:21]
286-
Completable: Cpattern Value[z]=t->variantPayload::Two($0)
286+
Completable: Cpattern Value[z]=t->variantPayload(Two)
287287
[{
288288
"label": "true",
289289
"kind": 4,
@@ -297,7 +297,7 @@ looking for: Cpath Value[z]
297297
posCursor:[92:23] posNoWhite:[92:22] Found expr:[92:3->92:26]
298298
posCursor:[92:23] posNoWhite:[92:22] Found pattern:[92:16->92:25]
299299
posCursor:[92:23] posNoWhite:[92:22] Found pattern:[92:22->92:24]
300-
Completable: Cpattern Value[z]->variantPayload::Three($0), recordBody
300+
Completable: Cpattern Value[z]->variantPayload(Three), recordBody
301301
[{
302302
"label": "first",
303303
"kind": 5,
@@ -324,12 +324,27 @@ Completable: Cpattern Value[z]->variantPayload::Three($0), recordBody
324324
"documentation": null
325325
}]
326326

327-
Complete src/CompletionPattern.res 99:21
327+
Complete src/CompletionPattern.res 95:27
328+
looking for: Cpath Value[z]
329+
posCursor:[95:27] posNoWhite:[95:26] Found expr:[95:3->95:29]
330+
posCursor:[95:27] posNoWhite:[95:26] Found pattern:[95:16->95:28]
331+
posCursor:[95:27] posNoWhite:[95:26] Found pattern:[95:21->95:29]
332+
posCursor:[95:27] posNoWhite:[95:26] Found pattern:[95:26->95:27]
333+
Completable: Cpattern Value[z]=t->variantPayload(Three), tuple($1)
334+
[{
335+
"label": "true",
336+
"kind": 4,
337+
"tags": [],
338+
"detail": "bool",
339+
"documentation": null
340+
}]
341+
342+
Complete src/CompletionPattern.res 102:21
328343
looking for: Cpath Value[b]
329-
posCursor:[99:21] posNoWhite:[99:20] Found expr:[99:3->99:23]
330-
posCursor:[99:21] posNoWhite:[99:20] Found pattern:[99:16->99:22]
331-
posCursor:[99:21] posNoWhite:[99:20] Found pattern:[99:20->99:21]
332-
Completable: Cpattern Value[b]->polyvariantPayload::two($0)
344+
posCursor:[102:21] posNoWhite:[102:20] Found expr:[102:3->102:23]
345+
posCursor:[102:21] posNoWhite:[102:20] Found pattern:[102:16->102:22]
346+
posCursor:[102:21] posNoWhite:[102:20] Found pattern:[102:20->102:21]
347+
Completable: Cpattern Value[b]->polyvariantPayload(two)
333348
[{
334349
"label": "true",
335350
"kind": 4,
@@ -344,12 +359,12 @@ Completable: Cpattern Value[b]->polyvariantPayload::two($0)
344359
"documentation": null
345360
}]
346361

347-
Complete src/CompletionPattern.res 102:22
362+
Complete src/CompletionPattern.res 105:22
348363
looking for: Cpath Value[b]
349-
posCursor:[102:22] posNoWhite:[102:21] Found expr:[102:3->102:24]
350-
posCursor:[102:22] posNoWhite:[102:21] Found pattern:[102:16->102:23]
351-
posCursor:[102:22] posNoWhite:[102:21] Found pattern:[102:21->102:22]
352-
Completable: Cpattern Value[b]=t->polyvariantPayload::two($0)
364+
posCursor:[105:22] posNoWhite:[105:21] Found expr:[105:3->105:24]
365+
posCursor:[105:22] posNoWhite:[105:21] Found pattern:[105:16->105:23]
366+
posCursor:[105:22] posNoWhite:[105:21] Found pattern:[105:21->105:22]
367+
Completable: Cpattern Value[b]=t->polyvariantPayload(two)
353368
[{
354369
"label": "true",
355370
"kind": 4,
@@ -358,12 +373,12 @@ Completable: Cpattern Value[b]=t->polyvariantPayload::two($0)
358373
"documentation": null
359374
}]
360375

361-
Complete src/CompletionPattern.res 105:24
376+
Complete src/CompletionPattern.res 108:24
362377
looking for: Cpath Value[b]
363-
posCursor:[105:24] posNoWhite:[105:23] Found expr:[105:3->105:27]
364-
posCursor:[105:24] posNoWhite:[105:23] Found pattern:[105:16->105:26]
365-
posCursor:[105:24] posNoWhite:[105:23] Found pattern:[105:23->105:25]
366-
Completable: Cpattern Value[b]->polyvariantPayload::three($0), recordBody
378+
posCursor:[108:24] posNoWhite:[108:23] Found expr:[108:3->108:27]
379+
posCursor:[108:24] posNoWhite:[108:23] Found pattern:[108:16->108:26]
380+
posCursor:[108:24] posNoWhite:[108:23] Found pattern:[108:23->108:25]
381+
Completable: Cpattern Value[b]->polyvariantPayload(three), recordBody
367382
[{
368383
"label": "first",
369384
"kind": 5,
@@ -390,7 +405,22 @@ Completable: Cpattern Value[b]->polyvariantPayload::three($0), recordBody
390405
"documentation": null
391406
}]
392407

393-
Complete src/CompletionPattern.res 111:15
408+
Complete src/CompletionPattern.res 111:28
409+
looking for: Cpath Value[b]
410+
posCursor:[111:28] posNoWhite:[111:27] Found expr:[111:3->111:30]
411+
posCursor:[111:28] posNoWhite:[111:27] Found pattern:[111:16->111:29]
412+
posCursor:[111:28] posNoWhite:[111:27] Found pattern:[111:22->111:29]
413+
posCursor:[111:28] posNoWhite:[111:27] Found pattern:[111:27->111:28]
414+
Completable: Cpattern Value[b]=t->polyvariantPayload(three), tuple($1)
415+
[{
416+
"label": "true",
417+
"kind": 4,
418+
"tags": [],
419+
"detail": "bool",
420+
"documentation": null
421+
}]
422+
423+
Complete src/CompletionPattern.res 117:15
394424
XXX Not found!
395425
Completable: Cpattern Value[c]
396426
[{
@@ -404,10 +434,10 @@ Completable: Cpattern Value[c]
404434
"insertTextFormat": 2
405435
}]
406436

407-
Complete src/CompletionPattern.res 114:17
437+
Complete src/CompletionPattern.res 120:17
408438
looking for: Cpath Value[c]
409-
posCursor:[114:17] posNoWhite:[114:16] Found expr:[114:3->114:20]
410-
posCursor:[114:17] posNoWhite:[114:16] Found pattern:[114:16->114:18]
439+
posCursor:[120:17] posNoWhite:[120:16] Found expr:[120:3->120:20]
440+
posCursor:[120:17] posNoWhite:[120:16] Found pattern:[120:16->120:18]
411441
Completable: Cpattern Value[c]->array
412442
[{
413443
"label": "true",

0 commit comments

Comments
 (0)