Skip to content

Commit 001f8ff

Browse files
committed
complete regular variants
1 parent f628fc0 commit 001f8ff

File tree

4 files changed

+88
-16
lines changed

4 files changed

+88
-16
lines changed

analysis/src/CompletionBackEnd.ml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1487,6 +1487,7 @@ type extractedType =
14871487
| Tuple of QueryEnv.t * Types.type_expr list
14881488
| Toption of QueryEnv.t * Types.type_expr
14891489
| Tbool of QueryEnv.t
1490+
| Tvariant of {env: QueryEnv.t; constructors: Constructor.t list}
14901491

14911492
(* This is a more general extraction function for pulling out the type of a type_expr. We already have other similar functions, but they are all specialized on something (variants, records, etc). *)
14921493
let rec extractType ~env ~package (t : Types.type_expr) =
@@ -1498,6 +1499,13 @@ let rec extractType ~env ~package (t : Types.type_expr) =
14981499
| Tconstr (Path.Pident {name = "bool"}, [], _) ->
14991500
(* Handle bool. TODO: Look up how the compiler does this and copy that behavior. *)
15001501
Some (Tbool env)
1502+
| Tconstr (path, _, _) -> (
1503+
match References.digConstructor ~env ~package path with
1504+
| Some (env, {item = {decl = {type_manifest = Some t1}}}) ->
1505+
extractType ~env ~package t1
1506+
| Some (env, {item = {kind = Type.Variant constructors}}) ->
1507+
Some (Tvariant {env; constructors})
1508+
| _ -> None)
15011509
| Ttuple expressions -> Some (Tuple (env, expressions))
15021510
| _ -> None
15031511

@@ -1519,6 +1527,21 @@ let completeTypedValue t ~env ~full ~prefix =
15191527
items
15201528
|> List.filter (fun (item : Completion.t) ->
15211529
Utils.startsWith item.name prefix)
1530+
| Some (Tvariant {env; constructors}) ->
1531+
let items =
1532+
constructors
1533+
|> List.filter_map (fun (constructor : Constructor.t) ->
1534+
if
1535+
prefix <> ""
1536+
&& not (Utils.startsWith constructor.cname.txt prefix)
1537+
then None
1538+
else
1539+
Some
1540+
(Completion.create ~name:constructor.cname.txt
1541+
~kind:(Constructor (constructor, "" (* TODO *)))
1542+
~env))
1543+
in
1544+
items
15221545
| _ -> []
15231546

15241547
let processCompletable ~debug ~full ~scope ~env ~pos ~forHover

analysis/src/CompletionFrontEnd.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ let extractJsxProps ~(compName : Longident.t Location.loc) ~args =
109109
let extractCompletableArgValueInfo exp =
110110
match exp.Parsetree.pexp_desc with
111111
| Pexp_ident {txt} -> Some (Utils.flattenLongIdent txt |> List.hd)
112+
| Pexp_construct ({txt}, _) -> Some (Utils.flattenLongIdent txt |> List.hd)
112113
| _ -> None
113114

114115
let isExprHole exp =

analysis/tests/src/CompletionFunctionArguments.res

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,15 @@ let someOtherFn = (includeName, age) => {
2626

2727
// let _ = someOtherFn(f)
2828
// ^com
29+
30+
type someVariant = One | Two | Three(int)
31+
32+
let someFnTakingVariant = (~config: someVariant) => {
33+
ignore(config)
34+
}
35+
36+
// let _ = someFnTakingVariant(~config=)
37+
// ^com
38+
39+
// let _ = someFnTakingVariant(~config=O)
40+
// ^com

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

Lines changed: 52 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
Complete src/CompletionFunctionArguments.res 8:24
2-
posCursor:[8:24] posNoWhite:[8:23] Found expr:[8:11->8:25]
3-
Pexp_apply ...[8:11->8:17] (~isOn8:19->8:23=...__ghost__[0:-1->0:-1])
1+
Complete src/CompletionFunctionArguments.res 10:24
2+
posCursor:[10:24] posNoWhite:[10:23] Found expr:[10:11->10:25]
3+
Pexp_apply ...[10:11->10:17] (~isOn10:19->10:23=...__ghost__[0:-1->0:-1])
44
Completable: Value[someFn](~isOn)
55
[{
66
"label": "true",
@@ -16,9 +16,9 @@ Completable: Value[someFn](~isOn)
1616
"documentation": null
1717
}]
1818

19-
Complete src/CompletionFunctionArguments.res 11:25
20-
posCursor:[11:25] posNoWhite:[11:24] Found expr:[11:11->11:26]
21-
Pexp_apply ...[11:11->11:17] (~isOn11:19->11:23=...[11:24->11:25])
19+
Complete src/CompletionFunctionArguments.res 13:25
20+
posCursor:[13:25] posNoWhite:[13:24] Found expr:[13:11->13:26]
21+
Pexp_apply ...[13:11->13:17] (~isOn13:19->13:23=...[13:24->13:25])
2222
Completable: Value[someFn](~isOn=t)
2323
[{
2424
"label": "true",
@@ -28,13 +28,13 @@ Completable: Value[someFn](~isOn=t)
2828
"documentation": null
2929
}]
3030

31-
Complete src/CompletionFunctionArguments.res 16:27
32-
posCursor:[16:27] posNoWhite:[16:26] Found expr:[14:8->20:1]
33-
Pexp_apply ...[14:8->14:14] (~isOn15:3->15:7=...[16:7->18:8])
34-
posCursor:[16:27] posNoWhite:[16:26] Found expr:[16:7->18:8]
35-
posCursor:[16:27] posNoWhite:[16:26] Found expr:[16:7->16:28]
36-
posCursor:[16:27] posNoWhite:[16:26] Found expr:[16:14->16:28]
37-
Pexp_apply ...[16:14->16:20] (~isOn16:22->16:26=...__ghost__[0:-1->0:-1])
31+
Complete src/CompletionFunctionArguments.res 18:27
32+
posCursor:[18:27] posNoWhite:[18:26] Found expr:[16:8->22:1]
33+
Pexp_apply ...[16:8->16:14] (~isOn17:3->17:7=...[18:7->20:8])
34+
posCursor:[18:27] posNoWhite:[18:26] Found expr:[18:7->20:8]
35+
posCursor:[18:27] posNoWhite:[18:26] Found expr:[18:7->18:28]
36+
posCursor:[18:27] posNoWhite:[18:26] Found expr:[18:14->18:28]
37+
Pexp_apply ...[18:14->18:20] (~isOn18:22->18:26=...__ghost__[0:-1->0:-1])
3838
Completable: Value[someFn](~isOn)
3939
[{
4040
"label": "true",
@@ -50,9 +50,9 @@ Completable: Value[someFn](~isOn)
5050
"documentation": null
5151
}]
5252

53-
Complete src/CompletionFunctionArguments.res 26:24
54-
posCursor:[26:24] posNoWhite:[26:23] Found expr:[26:11->26:25]
55-
Pexp_apply ...[26:11->26:22] (...[26:23->26:24])
53+
Complete src/CompletionFunctionArguments.res 28:24
54+
posCursor:[28:24] posNoWhite:[28:23] Found expr:[28:11->28:25]
55+
Pexp_apply ...[28:11->28:22] (...[28:23->28:24])
5656
Completable: Value[someOtherFn]($0=f)
5757
[{
5858
"label": "false",
@@ -62,3 +62,39 @@ Completable: Value[someOtherFn]($0=f)
6262
"documentation": null
6363
}]
6464

65+
Complete src/CompletionFunctionArguments.res 37:39
66+
posCursor:[37:39] posNoWhite:[37:38] Found expr:[37:11->37:40]
67+
Pexp_apply ...[37:11->37:30] (~config37:32->37:38=...__ghost__[0:-1->0:-1])
68+
Completable: Value[someFnTakingVariant](~config)
69+
[{
70+
"label": "One",
71+
"kind": 4,
72+
"tags": [],
73+
"detail": "One\n\n",
74+
"documentation": null
75+
}, {
76+
"label": "Two",
77+
"kind": 4,
78+
"tags": [],
79+
"detail": "Two\n\n",
80+
"documentation": null
81+
}, {
82+
"label": "Three",
83+
"kind": 4,
84+
"tags": [],
85+
"detail": "Three(int)\n\n",
86+
"documentation": null
87+
}]
88+
89+
Complete src/CompletionFunctionArguments.res 40:40
90+
posCursor:[40:40] posNoWhite:[40:39] Found expr:[40:11->40:41]
91+
Pexp_apply ...[40:11->40:30] (~config40:32->40:38=...[40:39->40:40])
92+
Completable: Value[someFnTakingVariant](~config=O)
93+
[{
94+
"label": "One",
95+
"kind": 4,
96+
"tags": [],
97+
"detail": "One\n\n",
98+
"documentation": null
99+
}]
100+

0 commit comments

Comments
 (0)