Skip to content

Commit 583a42a

Browse files
committed
ppx autocomplete: handle variant and polymorphic variant without braces.
1 parent 49ce244 commit 583a42a

File tree

3 files changed

+48
-8
lines changed

3 files changed

+48
-8
lines changed

analysis/src/PartialParser.ml

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,27 @@ let findCallFromArgument text offset =
5151
in
5252
loop ~i:offset ~nClosed:0
5353

54-
(* Figure out whether id should be autocompleted as component prop. *)
55-
(* Find JSX context ctx for component M to autocomplete id (already parsed) as a prop. *)
56-
(* ctx ::= <M args id *)
57-
(* arg ::= id | id = [?] val *)
58-
(* val ::= id | "abc" | 42 | {...} | (...) | [...] *)
54+
(* skip A or #A if present *)
55+
let skipOptVariant text i =
56+
prerr_endline "skipOptIdent";
57+
if i > 0 then
58+
match text.[i] with
59+
| 'a' .. 'z' | 'A' .. 'Z' | '_' | '0' .. '9' ->
60+
let i = startOfLident text i - 1 in
61+
let i =
62+
if i > 0 then match text.[i] with '#' -> i - 1 | _ -> i else i
63+
in
64+
i
65+
| _ -> i
66+
else i
67+
68+
(* Figure out whether id should be autocompleted as component prop.
69+
Find JSX context ctx for component M to autocomplete id (already parsed) as a prop.
70+
ctx ::= <M args id
71+
arg ::= id | id = [?] val
72+
val ::= id | "abc" | 42 | {...} | optVariant (...) | [...]
73+
optVariant ::= A | #a | _nothing_
74+
*)
5975
let findJsxContext text offset =
6076
let rec loop identsSeen i =
6177
let i = skipWhite text i in
@@ -66,7 +82,7 @@ let findJsxContext text offset =
6682
if i1 > 0 then beforeValue identsSeen i1 else None
6783
| ')' ->
6884
let i1 = findBackSkippingCommentsAndStrings text '(' ')' (i - 1) 0 in
69-
if i1 > 0 then beforeValue identsSeen i1 else None
85+
if i1 > 0 then beforeParen identsSeen i1 else None
7086
| ']' ->
7187
let i1 = findBackSkippingCommentsAndStrings text '[' ']' (i - 1) 0 in
7288
if i1 > 0 then beforeValue identsSeen i1 else None
@@ -91,6 +107,9 @@ let findJsxContext text offset =
91107
| '=' -> fromEquals identsSeen i
92108
| _ -> loop identsSeen (i - 1)
93109
else None
110+
and beforeParen identsSeen i =
111+
let i = skipWhite text i in
112+
beforeValue identsSeen (skipOptVariant text i)
94113
and beforeValue identsSeen i =
95114
let i = skipWhite text i in
96115
if i > 0 then

analysis/tests/src/Jsx.res

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,8 @@ let make = (~first) => React.string(first)
1818
let y = 44
1919

2020
//^com <M prop={A(3)} k
21+
2122
//^com <M prop=A(3) k
23+
24+
25+
//^com <M prop = #A (3) k

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,23 @@ Complete tests/src/Jsx.res 18:2
7979
"documentation": null
8080
}]
8181

82-
Complete tests/src/Jsx.res 19:2
83-
[]
82+
Complete tests/src/Jsx.res 20:2
83+
skipOptIdent
84+
[{
85+
"label": "key",
86+
"kind": 4,
87+
"tags": [],
88+
"detail": "string",
89+
"documentation": null
90+
}]
91+
92+
Complete tests/src/Jsx.res 23:2
93+
skipOptIdent
94+
[{
95+
"label": "key",
96+
"kind": 4,
97+
"tags": [],
98+
"detail": "string",
99+
"documentation": null
100+
}]
84101

0 commit comments

Comments
 (0)