Skip to content

Commit 5ed8bb4

Browse files
committed
identify typed context for labelled argument assignments
1 parent 87508b3 commit 5ed8bb4

File tree

5 files changed

+48
-9
lines changed

5 files changed

+48
-9
lines changed

analysis/src/CompletionBackEnd.ml

+1
Original file line numberDiff line numberDiff line change
@@ -1337,6 +1337,7 @@ let processCompletable ~debug ~package ~scope ~env ~pos ~forHover
13371337
in
13381338
match completable with
13391339
| Cnone -> []
1340+
| CtypedContext _contextPath -> []
13401341
| Cpath contextPath ->
13411342
contextPath
13421343
|> getCompletionsForContextPath ~package ~opens ~rawOpens ~allFiles ~pos

analysis/src/CompletionFrontEnd.ml

+12-2
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,8 @@ type label = labelled option
137137
type arg = {label: label; exp: Parsetree.expression}
138138

139139
let findNamedArgCompletable ~(args : arg list) ~endPos ~posBeforeCursor
140-
~(contextPath : Completable.contextPath) ~posAfterFunExpr =
140+
~(contextPath : Completable.contextPath) ~posAfterFunExpr ~charBeforeCursor
141+
~debug =
141142
let allNames =
142143
List.fold_right
143144
(fun arg allLabels ->
@@ -149,11 +150,19 @@ let findNamedArgCompletable ~(args : arg list) ~endPos ~posBeforeCursor
149150
let rec loop args =
150151
match args with
151152
| {label = Some labelled; exp} :: rest ->
153+
(* Figure out if we're completing the labelled argument name, or assigning to the labelled argument *)
152154
if
153155
labelled.posStart <= posBeforeCursor
154156
&& posBeforeCursor < labelled.posEnd
157+
&& charBeforeCursor <> Some '='
158+
(* We need to account for empty assignments being parsed as regular labelled arguments without an assignment *)
155159
then Some (Completable.CnamedArg (contextPath, labelled.name, allNames))
156-
else if exp.pexp_loc |> Loc.hasPos ~pos:posBeforeCursor then None
160+
else if
161+
exp.pexp_loc |> Loc.hasPos ~pos:posBeforeCursor
162+
|| charBeforeCursor = Some '='
163+
then (
164+
if debug then Printf.printf "found typed context \n";
165+
Some (Completable.CtypedContext contextPath))
157166
else loop rest
158167
| {label = None; exp} :: rest ->
159168
if exp.pexp_loc |> Loc.hasPos ~pos:posBeforeCursor then None
@@ -592,6 +601,7 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor ~text =
592601
| Some contextPath ->
593602
findNamedArgCompletable ~contextPath ~args
594603
~endPos:(Loc.end_ expr.pexp_loc) ~posBeforeCursor
604+
~charBeforeCursor ~debug
595605
~posAfterFunExpr:(Loc.end_ funExpr.pexp_loc)
596606
| None -> None
597607
in

analysis/src/SharedTypes.ml

+2
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,7 @@ module Completable = struct
433433
| Cpath of contextPath
434434
| Cjsx of string list * string * string list
435435
(** E.g. (["M", "Comp"], "id", ["id1", "id2"]) for <M.Comp id1=... id2=... ... id *)
436+
| CtypedContext of contextPath (** WIP, just a dummy arg for now *)
436437

437438
let toString =
438439
let str s = if s = "" then "\"\"" else s in
@@ -471,4 +472,5 @@ module Completable = struct
471472
| Cnone -> "Cnone"
472473
| Cjsx (sl1, s, sl2) ->
473474
"Cjsx(" ^ (sl1 |> list) ^ ", " ^ str s ^ ", " ^ (sl2 |> list) ^ ")"
475+
| CtypedContext cp -> "CtypedContext(" ^ (cp |> contextPathToString) ^ ")"
474476
end

analysis/tests/src/TypeContextCompletion.res

+6
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,15 @@ let someVariantToString = (~someVariant) =>
88
| Four => "Four"
99
}
1010

11+
// let x = someVariantToString(~someVaria
12+
// ^com
13+
1114
// let x = someVariantToString(~someVariant=
1215
// ^com
1316

17+
// let x = someVariantToString(~someVariant=T
18+
// ^com
19+
1420
module SomeComponent = {
1521
@react.component
1622
let make = (~whatever) => {
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,32 @@
1-
Complete src/TypeContextCompletion.res 10:44
2-
posCursor:[10:44] posNoWhite:[10:43] Found expr:[10:11->18:1]
3-
Pexp_apply ...[10:11->10:30] (~someVariant10:32->10:43=...[13:0->17:3])
4-
Completable: CnamedArg(Value[someVariantToString], "", [someVariant])
1+
Complete src/TypeContextCompletion.res 10:41
2+
posCursor:[10:41] posNoWhite:[10:40] Found expr:[10:11->24:1]
3+
Pexp_apply ...[10:11->10:30] (~someVaria10:32->10:41=...[10:32->10:41], ...[19:0->23:3])
4+
Completable: CnamedArg(Value[someVariantToString], someVaria, [someVaria])
55
Found type for function (~someVariant: someVariant) => string
6+
[{
7+
"label": "someVariant",
8+
"kind": 4,
9+
"tags": [],
10+
"detail": "someVariant",
11+
"documentation": null
12+
}]
13+
14+
Complete src/TypeContextCompletion.res 13:44
15+
posCursor:[13:44] posNoWhite:[13:43] Found expr:[13:11->24:1]
16+
Pexp_apply ...[13:11->13:30] (~someVariant13:32->13:43=...[19:0->23:3])
17+
found typed context
18+
Completable: CtypedContext(Value[someVariantToString])
19+
[]
20+
21+
Complete src/TypeContextCompletion.res 16:45
22+
posCursor:[16:45] posNoWhite:[16:44] Found expr:[16:11->24:1]
23+
Pexp_apply ...[16:11->16:30] (~someVariant16:32->16:43=...[16:44->16:45], ...[19:0->23:3])
24+
found typed context
25+
Completable: CtypedContext(Value[someVariantToString])
626
[]
727

8-
Complete src/TypeContextCompletion.res 20:37
9-
posCursor:[20:37] posNoWhite:[20:36] Found expr:[20:14->20:37]
10-
JSX <SomeComponent:[20:14->20:27] whatever[20:28->20:36]=...__ghost__[0:-1->0:-1]> _children:None
28+
Complete src/TypeContextCompletion.res 26:37
29+
posCursor:[26:37] posNoWhite:[26:36] Found expr:[26:14->26:37]
30+
JSX <SomeComponent:[26:14->26:27] whatever[26:28->26:36]=...__ghost__[0:-1->0:-1]> _children:None
1131
[]
1232

0 commit comments

Comments
 (0)