Skip to content

Commit 3887cdf

Browse files
committed
fix jsx prop completion broken by first class regexp literal support
1 parent 17c1832 commit 3887cdf

File tree

2 files changed

+38
-9
lines changed

2 files changed

+38
-9
lines changed

analysis/src/CompletionJsx.ml

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,32 @@ type jsxProps = {
812812
childrenStart: (int * int) option;
813813
}
814814

815+
(**
816+
<div muted= />
817+
818+
This is a special case for JSX props, where the above code is parsed
819+
as <div muted=//, a regexp literal. We leverage that fact to trigger completion
820+
for the JSX prop value.
821+
822+
This code is safe because we also check that the location of the expression is broken,
823+
which only happens when the expression is a parse error/not complete.
824+
*)
825+
let isRegexpJsxHeuristicExpr expr =
826+
match expr.Parsetree.pexp_desc with
827+
| Pexp_extension
828+
( {txt = "re"},
829+
PStr
830+
[
831+
{
832+
pstr_desc =
833+
Pstr_eval
834+
({pexp_desc = Pexp_constant (Pconst_string ("//", _))}, _);
835+
};
836+
] )
837+
when expr.pexp_loc |> Loc.end_ = (Location.none |> Loc.end_) ->
838+
true
839+
| _ -> false
840+
815841
let findJsxPropsCompletable ~jsxProps ~endPos ~posBeforeCursor
816842
~firstCharBeforeCursorNoWhite ~charAtCursor ~posAfterCompName =
817843
let allLabels =
@@ -891,9 +917,14 @@ let findJsxPropsCompletable ~jsxProps ~endPos ~posBeforeCursor
891917
else if prop.exp.pexp_loc |> Loc.end_ = (Location.none |> Loc.end_) then (
892918
if Debug.verbose () then
893919
print_endline "[jsx_props_completable]--> Loc is broken";
894-
if CompletionExpressions.isExprHole prop.exp then (
920+
if
921+
CompletionExpressions.isExprHole prop.exp
922+
|| isRegexpJsxHeuristicExpr prop.exp
923+
then (
895924
if Debug.verbose () then
896-
print_endline "[jsx_props_completable]--> Expr was expr hole";
925+
print_endline
926+
"[jsx_props_completable]--> Expr was expr hole or regexp literal \
927+
heuristic";
897928
Some
898929
(Cexpression
899930
{

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -187,13 +187,12 @@ Path CompletionSupport.TestComponent.make
187187

188188
Complete src/CompletionJsxProps.res 15:22
189189
posCursor:[15:22] posNoWhite:[15:21] Found expr:[15:12->15:25]
190-
JSX <div:[15:12->15:15] muted[15:16->15:21]=...__ghost__[0:-1->0:-1]> _children:15:23
190+
JSX <div:[15:12->15:15] muted[15:16->15:21]=...__ghost__[0:-1->0:-1]> _children:None
191191
Completable: Cexpression CJsxPropValue [div] muted
192192
Package opens Pervasives.JsxModules.place holder
193-
Resolved opens 1 pervasives
194193
ContextPath CJsxPropValue [div] muted
195194
Path ReactDOM.domProps
196-
Path PervasivesU.JsxDOM.domProps
195+
Path JsxDOM.domProps
197196
[{
198197
"label": "true",
199198
"kind": 4,
@@ -210,18 +209,17 @@ Path PervasivesU.JsxDOM.domProps
210209

211210
Complete src/CompletionJsxProps.res 18:29
212211
posCursor:[18:29] posNoWhite:[18:28] Found expr:[18:12->18:32]
213-
JSX <div:[18:12->18:15] onMouseEnter[18:16->18:28]=...__ghost__[0:-1->0:-1]> _children:18:30
212+
JSX <div:[18:12->18:15] onMouseEnter[18:16->18:28]=...__ghost__[0:-1->0:-1]> _children:None
214213
Completable: Cexpression CJsxPropValue [div] onMouseEnter
215214
Package opens Pervasives.JsxModules.place holder
216-
Resolved opens 1 pervasives
217215
ContextPath CJsxPropValue [div] onMouseEnter
218216
Path ReactDOM.domProps
219-
Path PervasivesU.JsxDOM.domProps
217+
Path JsxDOM.domProps
220218
[{
221219
"label": "event => event",
222220
"kind": 12,
223221
"tags": [],
224-
"detail": "JsxEventU.Mouse.t => unit",
222+
"detail": "JsxEvent.Mouse.t => unit",
225223
"documentation": null,
226224
"sortText": "A",
227225
"insertText": "{${1:event} => ${0:event}}",

0 commit comments

Comments
 (0)