Skip to content

Commit e25d852

Browse files
committed
expand options in typed expr/pattern completion
1 parent 24420ed commit e25d852

File tree

4 files changed

+173
-1
lines changed

4 files changed

+173
-1
lines changed

analysis/src/CompletionBackEnd.ml

+17-1
Original file line numberDiff line numberDiff line change
@@ -1796,7 +1796,7 @@ let printConstructorArgs argsLen ~asSnippet =
17961796
if List.length !args > 0 then "(" ^ (!args |> String.concat ", ") ^ ")"
17971797
else ""
17981798

1799-
let completeTypedValue (t : Types.type_expr) ~env ~full ~prefix
1799+
let rec completeTypedValue (t : Types.type_expr) ~env ~full ~prefix
18001800
~completionContext =
18011801
match t |> extractType ~env ~package:full.package with
18021802
| Some (Tbool env) ->
@@ -1849,6 +1849,21 @@ let completeTypedValue (t : Types.type_expr) ~env ~full ~prefix
18491849
~env ())
18501850
|> filterItems ~prefix
18511851
| Some (Toption (env, t)) ->
1852+
let innerType = Utils.unwrapIfOption t in
1853+
let expandedCompletions =
1854+
innerType
1855+
|> completeTypedValue ~env ~full ~prefix ~completionContext
1856+
|> List.map (fun (c : Completion.t) ->
1857+
{
1858+
c with
1859+
name = "Some(" ^ c.name ^ ")";
1860+
sortText = None;
1861+
insertText =
1862+
(match c.insertText with
1863+
| None -> None
1864+
| Some insertText -> Some ("Some(" ^ insertText ^ ")"));
1865+
})
1866+
in
18521867
[
18531868
Completion.create ~name:"None"
18541869
~kind:(Label (t |> Shared.typeToString))
@@ -1857,6 +1872,7 @@ let completeTypedValue (t : Types.type_expr) ~env ~full ~prefix
18571872
~kind:(Label (t |> Shared.typeToString))
18581873
~env ~insertText:"Some(${1:_})" ();
18591874
]
1875+
@ expandedCompletions
18601876
|> filterItems ~prefix
18611877
| Some (Tuple (env, exprs, typ)) ->
18621878
let numExprs = List.length exprs in

analysis/tests/src/CompletionExpressions.res

+8
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,11 @@ let fnTakingRecordWithOptionalField = (r: recordWithOptionalField) => {
108108

109109
// let _ = fnTakingRecordWithOptionalField({someOptField: })
110110
// ^com
111+
type recordWithOptVariant = {someVariant: option<someVariant>}
112+
113+
let fnTakingRecordWithOptVariant = (r: recordWithOptVariant) => {
114+
ignore(r)
115+
}
116+
117+
// let _ = fnTakingRecordWithOptVariant({someVariant: })
118+
// ^com

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

+88
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,14 @@ Completable: Cexpression CArgument Value[fnTakingRecord]($0)->recordField(nested
167167
"documentation": null,
168168
"insertText": "Some(${1:_})",
169169
"insertTextFormat": 2
170+
}, {
171+
"label": "Some({})",
172+
"kind": 12,
173+
"tags": [],
174+
"detail": "otherRecord",
175+
"documentation": null,
176+
"insertText": "Some({$0})",
177+
"insertTextFormat": 2
170178
}]
171179

172180
Complete src/CompletionExpressions.res 44:46
@@ -328,6 +336,18 @@ Completable: Cexpression CArgument Value[fnTakingArray]($0)->array
328336
"documentation": null,
329337
"insertText": "Some(${1:_})",
330338
"insertTextFormat": 2
339+
}, {
340+
"label": "Some(true)",
341+
"kind": 4,
342+
"tags": [],
343+
"detail": "bool",
344+
"documentation": null
345+
}, {
346+
"label": "Some(false)",
347+
"kind": 4,
348+
"tags": [],
349+
"detail": "bool",
350+
"documentation": null
331351
}]
332352

333353
Complete src/CompletionExpressions.res 75:26
@@ -378,6 +398,18 @@ Completable: Cexpression CArgument Value[fnTakingArray]($0)->array
378398
"documentation": null,
379399
"insertText": "Some(${1:_})",
380400
"insertTextFormat": 2
401+
}, {
402+
"label": "Some(true)",
403+
"kind": 4,
404+
"tags": [],
405+
"detail": "bool",
406+
"documentation": null
407+
}, {
408+
"label": "Some(false)",
409+
"kind": 4,
410+
"tags": [],
411+
"detail": "bool",
412+
"documentation": null
381413
}]
382414

383415
Complete src/CompletionExpressions.res 84:31
@@ -398,6 +430,18 @@ Completable: Cexpression CArgument Value[fnTakingArray]($0)->array
398430
"documentation": null,
399431
"insertText": "Some(${1:_})",
400432
"insertTextFormat": 2
433+
}, {
434+
"label": "Some(true)",
435+
"kind": 4,
436+
"tags": [],
437+
"detail": "bool",
438+
"documentation": null
439+
}, {
440+
"label": "Some(false)",
441+
"kind": 4,
442+
"tags": [],
443+
"detail": "bool",
444+
"documentation": null
401445
}]
402446

403447
Complete src/CompletionExpressions.res 89:38
@@ -445,3 +489,47 @@ Completable: Cexpression CArgument Value[fnTakingRecordWithOptionalField]($0)->r
445489
"documentation": null
446490
}]
447491

492+
Complete src/CompletionExpressions.res 116:53
493+
posCursor:[116:53] posNoWhite:[116:52] Found expr:[116:11->116:56]
494+
Pexp_apply ...[116:11->116:39] (...[116:40->116:56])
495+
Completable: Cexpression CArgument Value[fnTakingRecordWithOptVariant]($0)->recordField(someVariant)
496+
[{
497+
"label": "None",
498+
"kind": 4,
499+
"tags": [],
500+
"detail": "someVariant",
501+
"documentation": null
502+
}, {
503+
"label": "Some(_)",
504+
"kind": 4,
505+
"tags": [],
506+
"detail": "someVariant",
507+
"documentation": null,
508+
"insertText": "Some(${1:_})",
509+
"insertTextFormat": 2
510+
}, {
511+
"label": "Some(One)",
512+
"kind": 4,
513+
"tags": [],
514+
"detail": "One\n\ntype someVariant = One | Two | Three(int, string)",
515+
"documentation": null,
516+
"insertText": "Some(One)",
517+
"insertTextFormat": 2
518+
}, {
519+
"label": "Some(Two)",
520+
"kind": 4,
521+
"tags": [],
522+
"detail": "Two\n\ntype someVariant = One | Two | Three(int, string)",
523+
"documentation": null,
524+
"insertText": "Some(Two)",
525+
"insertTextFormat": 2
526+
}, {
527+
"label": "Some(Three(_, _))",
528+
"kind": 4,
529+
"tags": [],
530+
"detail": "Three(int, string)\n\ntype someVariant = One | Two | Three(int, string)",
531+
"documentation": null,
532+
"insertText": "Some(Three(${1:_}, ${2:_}))",
533+
"insertTextFormat": 2
534+
}]
535+

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

+60
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,18 @@ Completable: Cpattern Value[p]->variantPayload::Test($2)
497497
"documentation": null,
498498
"insertText": "Some(${1:_})",
499499
"insertTextFormat": 2
500+
}, {
501+
"label": "Some(true)",
502+
"kind": 4,
503+
"tags": [],
504+
"detail": "bool",
505+
"documentation": null
506+
}, {
507+
"label": "Some(false)",
508+
"kind": 4,
509+
"tags": [],
510+
"detail": "bool",
511+
"documentation": null
500512
}]
501513

502514
Complete src/CompletionPattern.res 140:23
@@ -573,6 +585,18 @@ Completable: Cpattern Value[v]->polyvariantPayload::test($2)
573585
"documentation": null,
574586
"insertText": "Some(${1:_})",
575587
"insertTextFormat": 2
588+
}, {
589+
"label": "Some(true)",
590+
"kind": 4,
591+
"tags": [],
592+
"detail": "bool",
593+
"documentation": null
594+
}, {
595+
"label": "Some(false)",
596+
"kind": 4,
597+
"tags": [],
598+
"detail": "bool",
599+
"documentation": null
576600
}]
577601

578602
Complete src/CompletionPattern.res 156:24
@@ -647,6 +671,18 @@ Completable: Cpattern Value[s]->tuple($1)
647671
"documentation": null,
648672
"insertText": "Some(${1:_})",
649673
"insertTextFormat": 2
674+
}, {
675+
"label": "Some(true)",
676+
"kind": 4,
677+
"tags": [],
678+
"detail": "bool",
679+
"documentation": null
680+
}, {
681+
"label": "Some(false)",
682+
"kind": 4,
683+
"tags": [],
684+
"detail": "bool",
685+
"documentation": null
650686
}]
651687

652688
Complete src/CompletionPattern.res 170:22
@@ -667,6 +703,18 @@ Completable: Cpattern Value[s]->tuple($1)
667703
"documentation": null,
668704
"insertText": "Some(${1:_})",
669705
"insertTextFormat": 2
706+
}, {
707+
"label": "Some(true)",
708+
"kind": 4,
709+
"tags": [],
710+
"detail": "bool",
711+
"documentation": null
712+
}, {
713+
"label": "Some(false)",
714+
"kind": 4,
715+
"tags": [],
716+
"detail": "bool",
717+
"documentation": null
670718
}]
671719

672720
Complete src/CompletionPattern.res 173:35
@@ -700,6 +748,18 @@ Completable: Cpattern Value[s]->tuple($1)
700748
"documentation": null,
701749
"insertText": "Some(${1:_})",
702750
"insertTextFormat": 2
751+
}, {
752+
"label": "Some(true)",
753+
"kind": 4,
754+
"tags": [],
755+
"detail": "bool",
756+
"documentation": null
757+
}, {
758+
"label": "Some(false)",
759+
"kind": 4,
760+
"tags": [],
761+
"detail": "bool",
762+
"documentation": null
703763
}]
704764

705765
Complete src/CompletionPattern.res 179:21

0 commit comments

Comments
 (0)