Skip to content

Commit 1c75be4

Browse files
committed
fixes + autowrap insertion text with braces when in jsx
1 parent 4c6403f commit 1c75be4

File tree

3 files changed

+48
-12
lines changed

3 files changed

+48
-12
lines changed

analysis/src/CompletionBackEnd.ml

+21-3
Original file line numberDiff line numberDiff line change
@@ -876,20 +876,23 @@ let rec getCompletionsForContextPath ~full ~opens ~rawOpens ~allFiles ~pos ~env
876876
~opens ~allFiles ~pos ~env ~scope
877877
|> completionsGetTypeEnv
878878
in
879-
let isBuiltin =
879+
let lowercaseComponent =
880880
match pathToComponent with
881881
| [elName] when Char.lowercase_ascii elName.[0] = elName.[0] -> true
882882
| _ -> false
883883
in
884884
let targetLabel =
885-
if isBuiltin then
885+
if lowercaseComponent then
886886
let rec digToTypeForCompletion path ~env =
887887
match
888888
path
889889
|> getCompletionsForPath ~completionContext:Type ~exact:true
890890
~package ~opens ~allFiles ~pos ~env ~scope
891891
with
892892
| {kind = Type {kind = Abstract (Some (p, _))}; env} :: _ ->
893+
(* This case happens when what we're looking for is a type alias.
894+
This is the case in newer rescript-react versions where
895+
ReactDOM.domProps is an alias for JsxEvent.t. *)
893896
let pathRev = p |> Utils.expandPath in
894897
pathRev |> List.rev |> digToTypeForCompletion ~env
895898
| {kind = Type {kind = Record fields}; env} :: _ -> (
@@ -1149,7 +1152,7 @@ let rec completeTypedValue (t : SharedTypes.completionType) ~env ~full ~prefix
11491152
(* Pretty print a few common patterns. *)
11501153
match Path.head p |> Ident.name with
11511154
| "unit" -> "()"
1152-
| "ReactEvent" -> "event"
1155+
| "ReactEvent" | "JsxEvent" -> "event"
11531156
| _ -> "v" ^ indexText)
11541157
in
11551158
let mkFnArgs ~asSnippet =
@@ -1333,10 +1336,25 @@ let rec processCompletable ~debug ~full ~scope ~env ~pos ~forHover
13331336
with
13341337
| None -> []
13351338
| Some (typ, env, completionContext) -> (
1339+
let isJsx =
1340+
match contextPath with
1341+
| CJsxPropValue _ | CPPipe {inJsx = true} -> true
1342+
| _ -> false
1343+
in
13361344
let items =
13371345
typ
13381346
|> completeTypedValue ~mode:Expression ~env ~full ~prefix
13391347
~completionContext
1348+
|> List.map (fun (c : Completion.t) ->
1349+
if isJsx then
1350+
{
1351+
c with
1352+
insertText =
1353+
(match c.insertText with
1354+
| None -> None
1355+
| Some text -> Some ("{" ^ text ^ "}"));
1356+
}
1357+
else c)
13401358
in
13411359
match (prefix, completionContext) with
13421360
| "", _ -> items

analysis/tests/src/CompletionJsxProps.res

+3
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,6 @@
1616
// let _ = <div muted= />
1717
// ^com
1818

19+
// let _ = <div onMouseEnter= />
20+
// ^com
21+

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

+24-9
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Completable: Cexpression CJsxPropValue [CompletionSupport, TestComponent] test=T
3939
"detail": "Two\n\ntype testVariant = One | Two | Three(int)",
4040
"documentation": null,
4141
"sortText": "A Two",
42-
"insertText": "Two",
42+
"insertText": "{Two}",
4343
"insertTextFormat": 2
4444
}, {
4545
"label": "Three(_)",
@@ -48,7 +48,7 @@ Completable: Cexpression CJsxPropValue [CompletionSupport, TestComponent] test=T
4848
"detail": "Three(int)\n\ntype testVariant = One | Two | Three(int)",
4949
"documentation": null,
5050
"sortText": "A Three(_)",
51-
"insertText": "Three(${1:_})",
51+
"insertText": "{Three(${1:_})}",
5252
"insertTextFormat": 2
5353
}, {
5454
"label": "TableclothMap",
@@ -74,31 +74,31 @@ Completable: Cexpression CJsxPropValue [CompletionSupport, TestComponent] polyAr
7474
"tags": [],
7575
"detail": "#one\n\n[#one | #three(int, bool) | #two | #two2]",
7676
"documentation": null,
77-
"insertText": "#one",
77+
"insertText": "{#one}",
7878
"insertTextFormat": 2
7979
}, {
8080
"label": "#three(_, _)",
8181
"kind": 4,
8282
"tags": [],
8383
"detail": "#three(int, bool)\n\n[#one | #three(int, bool) | #two | #two2]",
8484
"documentation": null,
85-
"insertText": "#three(${1:_}, ${2:_})",
85+
"insertText": "{#three(${1:_}, ${2:_})}",
8686
"insertTextFormat": 2
8787
}, {
8888
"label": "#two",
8989
"kind": 4,
9090
"tags": [],
9191
"detail": "#two\n\n[#one | #three(int, bool) | #two | #two2]",
9292
"documentation": null,
93-
"insertText": "#two",
93+
"insertText": "{#two}",
9494
"insertTextFormat": 2
9595
}, {
9696
"label": "#two2",
9797
"kind": 4,
9898
"tags": [],
9999
"detail": "#two2\n\n[#one | #three(int, bool) | #two | #two2]",
100100
"documentation": null,
101-
"insertText": "#two2",
101+
"insertText": "{#two2}",
102102
"insertTextFormat": 2
103103
}]
104104

@@ -112,23 +112,23 @@ Completable: Cexpression CJsxPropValue [CompletionSupport, TestComponent] polyAr
112112
"tags": [],
113113
"detail": "#three(int, bool)\n\n[#one | #three(int, bool) | #two | #two2]",
114114
"documentation": null,
115-
"insertText": "three(${1:_}, ${2:_})",
115+
"insertText": "{three(${1:_}, ${2:_})}",
116116
"insertTextFormat": 2
117117
}, {
118118
"label": "#two",
119119
"kind": 4,
120120
"tags": [],
121121
"detail": "#two\n\n[#one | #three(int, bool) | #two | #two2]",
122122
"documentation": null,
123-
"insertText": "two",
123+
"insertText": "{two}",
124124
"insertTextFormat": 2
125125
}, {
126126
"label": "#two2",
127127
"kind": 4,
128128
"tags": [],
129129
"detail": "#two2\n\n[#one | #three(int, bool) | #two | #two2]",
130130
"documentation": null,
131-
"insertText": "two2",
131+
"insertText": "{two2}",
132132
"insertTextFormat": 2
133133
}]
134134

@@ -150,3 +150,18 @@ Completable: Cexpression CJsxPropValue [div] muted
150150
"documentation": null
151151
}]
152152

153+
Complete src/CompletionJsxProps.res 18:29
154+
posCursor:[18:29] posNoWhite:[18:28] Found expr:[18:12->18:32]
155+
JSX <div:[18:12->18:15] onMouseEnter[18:16->18:28]=...__ghost__[0:-1->0:-1]> _children:18:30
156+
Completable: Cexpression CJsxPropValue [div] onMouseEnter
157+
[{
158+
"label": "event => {}",
159+
"kind": 12,
160+
"tags": [],
161+
"detail": "JsxEvent.Mouse.t => unit",
162+
"documentation": null,
163+
"sortText": "A",
164+
"insertText": "{${1:event} => {$0}}",
165+
"insertTextFormat": 2
166+
}]
167+

0 commit comments

Comments
 (0)