Skip to content

Commit ce08941

Browse files
zthcristianoc
authored andcommitted
handle jsx context
1 parent 799bf02 commit ce08941

File tree

6 files changed

+67
-5
lines changed

6 files changed

+67
-5
lines changed

analysis/src/CompletionBackEnd.ml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -978,25 +978,25 @@ and getCompletionsForContextPath ~full ~opens ~rawOpens ~allFiles ~pos ~env
978978
in
979979
let targetLabel =
980980
if lowercaseComponent then
981-
let rec digToTypeForCompletion path ~env =
981+
let rec digToTypeForCompletion path =
982982
match
983983
path
984984
|> getCompletionsForPath ~completionContext:Type ~exact:true
985985
~package ~opens ~allFiles ~pos ~env ~scope
986986
with
987-
| {kind = Type {kind = Abstract (Some (p, _))}; env} :: _ ->
987+
| {kind = Type {kind = Abstract (Some (p, _))}} :: _ ->
988988
(* This case happens when what we're looking for is a type alias.
989989
This is the case in newer rescript-react versions where
990990
ReactDOM.domProps is an alias for JsxEvent.t. *)
991991
let pathRev = p |> Utils.expandPath in
992-
pathRev |> List.rev |> digToTypeForCompletion ~env
993-
| {kind = Type {kind = Record fields}; env} :: _ -> (
992+
pathRev |> List.rev |> digToTypeForCompletion
993+
| {kind = Type {kind = Record fields}} :: _ -> (
994994
match fields |> List.find_opt (fun f -> f.fname.txt = propName) with
995995
| None -> None
996996
| Some f -> Some (f.fname.txt, f.typ, env))
997997
| _ -> None
998998
in
999-
["ReactDOM"; "domProps"] |> digToTypeForCompletion ~env
999+
["ReactDOM"; "domProps"] |> digToTypeForCompletion
10001000
else
10011001
CompletionJsx.getJsxLabels ~componentPath:pathToComponent
10021002
~findTypeOfValue ~package

analysis/src/CompletionFrontEnd.ml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,19 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor ~text =
665665
expr iterator arg.exp;
666666
resetCurrentCtxPath previousCtxPath))
667667
| Some argCompletable -> setResult argCompletable
668+
and iterateJsxProps ~iterator (props : CompletionJsx.jsxProps) =
669+
props.props
670+
|> List.iter (fun (prop : CompletionJsx.prop) ->
671+
let previousCtxPath = !currentCtxPath in
672+
setCurrentCtxPath
673+
(CJsxPropValue
674+
{
675+
pathToComponent =
676+
Utils.flattenLongIdent ~jsx:true props.compName.txt;
677+
propName = prop.name;
678+
});
679+
expr iterator prop.exp;
680+
resetCurrentCtxPath previousCtxPath)
668681
and expr (iterator : Ast_iterator.iterator) (expr : Parsetree.expression) =
669682
let oldInJsxContext = !inJsxContext in
670683
let processed = ref false in
@@ -796,6 +809,7 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor ~text =
796809
| [prefix] when Char.lowercase_ascii prefix.[0] = prefix.[0] ->
797810
ChtmlElement {prefix}
798811
| _ -> Cpath (CPId (compNamePath, Module)))
812+
else iterateJsxProps ~iterator jsxProps
799813
| Pexp_apply
800814
( {pexp_desc = Pexp_ident {txt = Lident "|."}},
801815
[

analysis/tests/src/CompletionInferValues.res

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,17 @@ let reactEventFn = (cb: ReactEvent.Mouse.t => unit) => {
3131

3232
// reactEventFn(event => { event->pr });
3333
// ^com
34+
35+
module Div = {
36+
@react.component
37+
let make = (~onMouseEnter: option<JsxEvent.Mouse.t => unit>=?) => {
38+
let _ = onMouseEnter
39+
React.null
40+
}
41+
}
42+
43+
// let _ = <div onMouseEnter={event => { event->pr }} />
44+
// ^com
45+
46+
// let _ = <Div onMouseEnter={event => { event->pr }} />
47+
// ^com

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1364,6 +1364,9 @@ posCursor:[336:26] posNoWhite:[336:25] Found expr:[336:16->338:5]
13641364
posCursor:[336:26] posNoWhite:[336:25] Found pattern:[336:20->338:5]
13651365
posCursor:[336:26] posNoWhite:[336:25] Found type:[336:23->338:5]
13661366
Ptyp_constr Res:[336:23->338:5]
1367+
posCursor:[336:26] posNoWhite:[336:25] Found pattern:[336:20->338:5]
1368+
posCursor:[336:26] posNoWhite:[336:25] Found type:[336:23->338:5]
1369+
Ptyp_constr Res:[336:23->338:5]
13671370
Completable: Cpath Type[Res]
13681371
Raw opens: 2 Shadow.B.place holder ... Shadow.A.place holder
13691372
Resolved opens 2 Completion.res Completion.res

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,31 @@ Completable: Cpath Value[event]->pr
9999
"documentation": null
100100
}]
101101

102+
Complete src/CompletionInferValues.res 42:50
103+
posCursor:[42:50] posNoWhite:[42:49] Found expr:[42:12->42:56]
104+
JSX <div:[42:12->42:15] onMouseEnter[42:16->42:28]=...[42:36->42:52]> _children:42:54
105+
posCursor:[42:50] posNoWhite:[42:49] Found expr:[42:36->42:52]
106+
posCursor:[42:50] posNoWhite:[42:49] Found expr:[42:41->42:50]
107+
Completable: Cpath Value[event]->pr <<jsx>>
108+
[{
109+
"label": "JsxEvent.Mouse.preventDefault",
110+
"kind": 12,
111+
"tags": [],
112+
"detail": "t => unit",
113+
"documentation": null
114+
}]
115+
116+
Complete src/CompletionInferValues.res 45:50
117+
posCursor:[45:50] posNoWhite:[45:49] Found expr:[45:12->45:56]
118+
JSX <Div:[45:12->45:15] onMouseEnter[45:16->45:28]=...[45:36->45:52]> _children:45:54
119+
posCursor:[45:50] posNoWhite:[45:49] Found expr:[45:36->45:52]
120+
posCursor:[45:50] posNoWhite:[45:49] Found expr:[45:41->45:50]
121+
Completable: Cpath Value[event]->pr <<jsx>>
122+
[{
123+
"label": "JsxEvent.Mouse.preventDefault",
124+
"kind": 12,
125+
"tags": [],
126+
"detail": "t => unit",
127+
"documentation": null
128+
}]
129+

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ posCursor:[6:26] posNoWhite:[6:25] Found expr:[6:16->8:5]
77
posCursor:[6:26] posNoWhite:[6:25] Found pattern:[6:20->8:5]
88
posCursor:[6:26] posNoWhite:[6:25] Found type:[6:23->8:5]
99
Ptyp_constr Res:[6:23->8:5]
10+
posCursor:[6:26] posNoWhite:[6:25] Found pattern:[6:20->8:5]
11+
posCursor:[6:26] posNoWhite:[6:25] Found type:[6:23->8:5]
12+
Ptyp_constr Res:[6:23->8:5]
1013
Completable: Cpath Type[Res]
1114
[{
1215
"label": "RescriptReactErrorBoundary",

0 commit comments

Comments
 (0)