Skip to content

Commit 5f7fc56

Browse files
committedApr 28, 2021
In Jsx autocomplete, don't list props already used.
Fixes #175.
1 parent e67ed97 commit 5f7fc56

File tree

5 files changed

+33
-30
lines changed

5 files changed

+33
-30
lines changed
 

‎CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
## master
22
- Fix issue jumping to variables defined in complex patterns.
3+
- In Jsx autocomplete, don't suggest props already assigned.
34

45
## beta 2
56

‎analysis/src/NewCompletions.ml

+3-2
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ let mkItem ~name ~kind ~detail ~deprecated ~docstring =
432432
let processCompletable ~findItems ~package ~rawOpens
433433
(completable : PartialParser.completable) =
434434
match completable with
435-
| Cjsx (componentPath, prefix) ->
435+
| Cjsx (componentPath, prefix, identsSeen) ->
436436
let items = findItems ~exact:true (componentPath @ ["make"]) in
437437
let labels =
438438
match items with
@@ -473,7 +473,8 @@ let processCompletable ~findItems ~package ~rawOpens
473473
else
474474
keyLabel
475475
:: (labels
476-
|> List.filter (fun (name, _t) -> Utils.startsWith name prefix)
476+
|> List.filter (fun (name, _t) ->
477+
Utils.startsWith name prefix && not (List.mem name identsSeen))
477478
|> List.map mkLabel)
478479
| Cpath parts ->
479480
let items = parts |> findItems ~exact:false in

‎analysis/src/PartialParser.ml

+28-22
Original file line numberDiff line numberDiff line change
@@ -57,57 +57,60 @@ let findCallFromArgument text offset =
5757
(* arg ::= id | id = [?] val *)
5858
(* val ::= id | "abc" | 42 | {...} | (...) | [...] *)
5959
let findJsxContext text offset =
60-
let rec loop i =
60+
let rec loop identsSeen i =
6161
let i = skipWhite text i in
6262
if i > 0 then
6363
match text.[i] with
6464
| '}' ->
6565
let i1 = findBackSkippingCommentsAndStrings text '{' '}' (i - 1) 0 in
66-
if i1 > 0 then beforeValue i1 else None
66+
if i1 > 0 then beforeValue identsSeen i1 else None
6767
| ')' ->
6868
let i1 = findBackSkippingCommentsAndStrings text '(' ')' (i - 1) 0 in
69-
if i1 > 0 then beforeValue i1 else None
69+
if i1 > 0 then beforeValue identsSeen i1 else None
7070
| ']' ->
7171
let i1 = findBackSkippingCommentsAndStrings text '[' ']' (i - 1) 0 in
72-
if i1 > 0 then beforeValue i1 else None
72+
if i1 > 0 then beforeValue identsSeen i1 else None
7373
| '"' ->
7474
let i1 = findBack text '"' (i - 1) in
75-
if i1 > 0 then beforeValue i1 else None
75+
if i1 > 0 then beforeValue identsSeen i1 else None
7676
| _ ->
7777
let i1 = startOfLident text i in
7878
let ident = String.sub text i1 (i - i1 + 1) in
7979
if i1 >= 1 && ident <> "" then
8080
match ident.[0] with
81-
| 'A' .. 'Z' when i1 >= 1 && text.[i1 - 1] = '<' -> Some ident
82-
| _ -> beforeIdent (i1 - 1)
81+
| 'A' .. 'Z' when i1 >= 1 && text.[i1 - 1] = '<' ->
82+
Some (ident, identsSeen)
83+
| _ -> beforeIdent identsSeen (i1 - 1)
8384
else None
8485
else None
85-
and beforeIdent i =
86+
and beforeIdent identsSeen i =
8687
let i = skipWhite text i in
8788
if i > 0 then
8889
match text.[i] with
89-
| '?' -> fromEquals (i - 1)
90-
| '=' -> fromEquals i
91-
| _ -> loop (i - 1)
90+
| '?' -> fromEquals identsSeen (i - 1)
91+
| '=' -> fromEquals identsSeen i
92+
| _ -> loop identsSeen (i - 1)
9293
else None
93-
and beforeValue i =
94+
and beforeValue identsSeen i =
9495
let i = skipWhite text i in
9596
if i > 0 then
96-
match text.[i] with '?' -> fromEquals (i - 1) | _ -> fromEquals i
97+
match text.[i] with
98+
| '?' -> fromEquals identsSeen (i - 1)
99+
| _ -> fromEquals identsSeen i
97100
else None
98-
and fromEquals i =
101+
and fromEquals identsSeen i =
99102
let i = skipWhite text i in
100103
if i > 0 then
101104
match text.[i] with
102105
| '=' -> (
103106
let i = skipWhite text (i - 1) in
104107
let i1 = startOfLident text i in
105108
let ident = String.sub text i1 (i - i1 + 1) in
106-
match ident with "" -> None | _ -> loop (i1 - 1))
109+
match ident with "" -> None | _ -> loop (ident :: identsSeen) (i1 - 1))
107110
| _ -> None
108111
else None
109112
in
110-
loop offset
113+
loop [] offset
111114

112115
type pipe = PipeId of string | PipeArray | PipeString
113116

@@ -116,8 +119,8 @@ type completable =
116119
| Clabel of string list * string
117120
(** e.g. (["M", "foo"], "label") for M.foo(...~label...) *)
118121
| Cpath of string list (** e.g. ["M", "foo"] for M.foo *)
119-
| Cjsx of string list * string
120-
(** E.g. (["M", "Comp"], "id") for <M.Comp ... id *)
122+
| Cjsx of string list * string * string list
123+
(** E.g. (["M", "Comp"], "id", ["id1", "id2"]) for <M.Comp id1=... id2=... ... id *)
121124
| Cpipe of pipe * string (** E.g. ("x", "foo") for "x->foo" *)
122125

123126
let isLowercaseIdent id =
@@ -140,8 +143,8 @@ let findCompletable text offset =
140143
| [id] when String.lowercase_ascii id = id -> (
141144
match findJsxContext text (offset - len - 1) with
142145
| None -> Cpath parts
143-
| Some componentName ->
144-
Cjsx (Str.split (Str.regexp_string ".") componentName, id))
146+
| Some (componentName, identsSeen) ->
147+
Cjsx (Str.split (Str.regexp_string ".") componentName, id, identsSeen))
145148
| _ -> Cpath parts
146149
in
147150
let mkPipe off partialName =
@@ -179,8 +182,11 @@ let findCompletable text offset =
179182
(* autocomplete with no id: check if inside JSX *)
180183
match findJsxContext text (offset - 1) with
181184
| None -> None
182-
| Some componentName ->
183-
Some (Cjsx (Str.split (Str.regexp_string ".") componentName, "")))
185+
| Some (componentName, identsSeen) ->
186+
Some
187+
(Cjsx
188+
(Str.split (Str.regexp_string ".") componentName, "", identsSeen))
189+
)
184190
| _ -> if i = offset - 1 then None else Some (mkPath (suffix i))
185191
in
186192
if offset > String.length text || offset = 0 then None else loop (offset - 1)

‎analysis/tests/.bsb.lock

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
81015

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

-6
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,6 @@ Complete tests/src/Jsx.res 7:2
88
"tags": [],
99
"detail": "string",
1010
"documentation": null
11-
}, {
12-
"label": "second",
13-
"kind": 4,
14-
"tags": [],
15-
"detail": "option<string>",
16-
"documentation": null
1711
}, {
1812
"label": "first",
1913
"kind": 4,

0 commit comments

Comments
 (0)
Please sign in to comment.