Skip to content

Commit 25482f0

Browse files
committed
complete bools as arguments
1 parent e380c48 commit 25482f0

File tree

3 files changed

+81
-7
lines changed

3 files changed

+81
-7
lines changed

analysis/src/CompletionBackEnd.ml

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1531,6 +1531,27 @@ let rec extractType ~env ~package (t : Types.type_expr) =
15311531
Some (Polyvariant (env, constructors))
15321532
| Ttuple expressions -> Some (Tuple (env, expressions))
15331533
| _ -> None
1534+
1535+
let completeTypedValue t ~env ~full ~prefix =
1536+
match t |> extractType ~env ~package:full.package with
1537+
| Some (Tbool env) ->
1538+
let items =
1539+
[
1540+
Completion.create ~name:"true"
1541+
~kind:(Label (t |> Shared.typeToString))
1542+
~env;
1543+
Completion.create ~name:"false"
1544+
~kind:(Label (t |> Shared.typeToString))
1545+
~env;
1546+
]
1547+
in
1548+
if prefix = "" then items
1549+
else
1550+
items
1551+
|> List.filter (fun (item : Completion.t) ->
1552+
Utils.startsWith item.name prefix)
1553+
| _ -> []
1554+
15341555
let processCompletable ~debug ~full ~scope ~env ~pos ~forHover
15351556
(completable : Completable.t) =
15361557
let package = full.package in
@@ -1893,7 +1914,23 @@ Note: The `@react.component` decorator requires the react-jsx config to be set i
18931914
in
18941915
(dec2, doc))
18951916
|> List.map mkDecorator
1896-
| Cargument _ -> []
1917+
| Cargument {contextPath; argumentLabel; prefix} -> (
1918+
let labels =
1919+
match
1920+
contextPath
1921+
|> getCompletionsForContextPath ~full ~opens ~rawOpens ~allFiles ~pos
1922+
~env ~exact:true ~scope
1923+
|> completionsGetTypeEnv
1924+
with
1925+
| Some (typ, _env) -> typ |> getArgs ~full ~env
1926+
| None -> []
1927+
in
1928+
let targetLabel =
1929+
labels |> List.find_opt (fun (label, _) -> label = argumentLabel)
1930+
in
1931+
match targetLabel with
1932+
| None -> []
1933+
| Some (_, typ) -> typ |> completeTypedValue ~env ~full ~prefix)
18971934
| CnamedArg (cp, prefix, identsSeen) ->
18981935
let labels =
18991936
match
@@ -1906,6 +1943,7 @@ Note: The `@react.component` decorator requires the react-jsx config to be set i
19061943
if debug then
19071944
Printf.printf "Found type for function %s\n"
19081945
(typ |> Shared.typeToString);
1946+
19091947
typ |> getArgs ~full ~env
19101948
|> List.filter_map (fun arg ->
19111949
match arg with

analysis/tests/src/CompletionFunctionArguments.res

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@ let someOtherFn = (includeName, age) => {
2424
"Hello" ++ (includeName ? " Some Name" : "") ++ ", you are age " ++ Belt.Int.toString(age)
2525
}
2626

27-
// let _ = someOtherFn(t)
27+
// let _ = someOtherFn(f)
2828
// ^com

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

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,31 @@ Complete src/CompletionFunctionArguments.res 8:24
22
posCursor:[8:24] posNoWhite:[8:23] Found expr:[8:11->8:25]
33
Pexp_apply ...[8:11->8:17] (~isOn8:19->8:23=...__ghost__[0:-1->0:-1])
44
Completable: Value[someFn](~isOn)
5-
[]
5+
[{
6+
"label": "true",
7+
"kind": 4,
8+
"tags": [],
9+
"detail": "bool",
10+
"documentation": null
11+
}, {
12+
"label": "false",
13+
"kind": 4,
14+
"tags": [],
15+
"detail": "bool",
16+
"documentation": null
17+
}]
618

719
Complete src/CompletionFunctionArguments.res 11:25
820
posCursor:[11:25] posNoWhite:[11:24] Found expr:[11:11->11:26]
921
Pexp_apply ...[11:11->11:17] (~isOn11:19->11:23=...[11:24->11:25])
1022
Completable: Value[someFn](~isOn=t)
11-
[]
23+
[{
24+
"label": "true",
25+
"kind": 4,
26+
"tags": [],
27+
"detail": "bool",
28+
"documentation": null
29+
}]
1230

1331
Complete src/CompletionFunctionArguments.res 16:27
1432
posCursor:[16:27] posNoWhite:[16:26] Found expr:[14:8->20:1]
@@ -18,11 +36,29 @@ posCursor:[16:27] posNoWhite:[16:26] Found expr:[16:7->16:28]
1836
posCursor:[16:27] posNoWhite:[16:26] Found expr:[16:14->16:28]
1937
Pexp_apply ...[16:14->16:20] (~isOn16:22->16:26=...__ghost__[0:-1->0:-1])
2038
Completable: Value[someFn](~isOn)
21-
[]
39+
[{
40+
"label": "true",
41+
"kind": 4,
42+
"tags": [],
43+
"detail": "bool",
44+
"documentation": null
45+
}, {
46+
"label": "false",
47+
"kind": 4,
48+
"tags": [],
49+
"detail": "bool",
50+
"documentation": null
51+
}]
2252

2353
Complete src/CompletionFunctionArguments.res 26:24
2454
posCursor:[26:24] posNoWhite:[26:23] Found expr:[26:11->26:25]
2555
Pexp_apply ...[26:11->26:22] (...[26:23->26:24])
26-
Completable: Value[someOtherFn]($0=t)
27-
[]
56+
Completable: Value[someOtherFn]($0=f)
57+
[{
58+
"label": "false",
59+
"kind": 4,
60+
"tags": [],
61+
"detail": "bool",
62+
"documentation": null
63+
}]
2864

0 commit comments

Comments
 (0)