Skip to content

Commit 45aebb2

Browse files
committed
typed expression compltion for lowercase JSX tags
1 parent e7bd5ed commit 45aebb2

File tree

4 files changed

+54
-10
lines changed

4 files changed

+54
-10
lines changed

analysis/src/CompletionBackEnd.ml

+27-10
Original file line numberDiff line numberDiff line change
@@ -756,18 +756,12 @@ let rec getCompletionsForContextPath ~full ~opens ~rawOpens ~allFiles ~pos ~env
756756
| Lazy -> ["Lazy"]
757757
| Char -> ["Char"])
758758
| TypExpr t -> (
759-
let rec expandPath (path : Path.t) =
760-
match path with
761-
| Pident id -> [Ident.name id]
762-
| Pdot (p, s, _) -> s :: expandPath p
763-
| Papply _ -> []
764-
in
765759
match t.Types.desc with
766760
| Tconstr (path, _typeArgs, _)
767761
| Tlink {desc = Tconstr (path, _typeArgs, _)}
768762
| Tsubst {desc = Tconstr (path, _typeArgs, _)}
769763
| Tpoly ({desc = Tconstr (path, _typeArgs, _)}, []) -> (
770-
match expandPath path with
764+
match Utils.expandPath path with
771765
| _ :: pathRev ->
772766
(* type path is relative to the completion environment
773767
express it from the root of the file *)
@@ -882,10 +876,33 @@ let rec getCompletionsForContextPath ~full ~opens ~rawOpens ~allFiles ~pos ~env
882876
~opens ~allFiles ~pos ~env ~scope
883877
|> completionsGetTypeEnv
884878
in
879+
let isBuiltin =
880+
match pathToComponent with
881+
| [elName] when Char.lowercase_ascii elName.[0] = elName.[0] -> true
882+
| _ -> false
883+
in
885884
let targetLabel =
886-
CompletionJsx.getJsxLabels ~componentPath:pathToComponent ~findTypeOfValue
887-
~package
888-
|> List.find_opt (fun (label, _, _) -> label = propName)
885+
if isBuiltin then
886+
let rec digToTypeForCompletion path ~env =
887+
match
888+
path
889+
|> getCompletionsForPath ~completionContext:Type ~exact:true
890+
~package ~opens ~allFiles ~pos ~env ~scope
891+
with
892+
| {kind = Type {kind = Abstract (Some (p, _))}; env} :: _ ->
893+
let pathRev = p |> Utils.expandPath in
894+
pathRev |> List.rev |> digToTypeForCompletion ~env
895+
| {kind = Type {kind = Record fields}; env} :: _ -> (
896+
match fields |> List.find_opt (fun f -> f.fname.txt = propName) with
897+
| None -> None
898+
| Some f -> Some (f.fname.txt, f.typ, env))
899+
| _ -> None
900+
in
901+
["ReactDOM"; "domProps"] |> digToTypeForCompletion ~env
902+
else
903+
CompletionJsx.getJsxLabels ~componentPath:pathToComponent
904+
~findTypeOfValue ~package
905+
|> List.find_opt (fun (label, _, _) -> label = propName)
889906
in
890907
match targetLabel with
891908
| None -> []

analysis/src/Utils.ml

+6
Original file line numberDiff line numberDiff line change
@@ -206,3 +206,9 @@ let rangeOfLoc (loc : Location.t) =
206206
let start = loc |> Loc.start |> mkPosition in
207207
let end_ = loc |> Loc.end_ |> mkPosition in
208208
{Protocol.start; end_}
209+
210+
let rec expandPath (path : Path.t) =
211+
match path with
212+
| Pident id -> [Ident.name id]
213+
| Pdot (p, s, _) -> s :: expandPath p
214+
| Papply _ -> []

analysis/tests/src/CompletionJsxProps.res

+3
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,6 @@
1313
// let _ = <CompletionSupport.TestComponent polyArg=#t
1414
// ^com
1515

16+
// let _ = <div muted= />
17+
// ^com
18+

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

+18
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,21 @@ Completable: Cexpression CJsxPropValue [CompletionSupport, TestComponent] polyAr
132132
"insertTextFormat": 2
133133
}]
134134

135+
Complete src/CompletionJsxProps.res 15:22
136+
posCursor:[15:22] posNoWhite:[15:21] Found expr:[15:12->15:25]
137+
JSX <div:[15:12->15:15] muted[15:16->15:21]=...__ghost__[0:-1->0:-1]> _children:15:23
138+
Completable: Cexpression CJsxPropValue [div] muted
139+
[{
140+
"label": "true",
141+
"kind": 4,
142+
"tags": [],
143+
"detail": "bool",
144+
"documentation": null
145+
}, {
146+
"label": "false",
147+
"kind": 4,
148+
"tags": [],
149+
"detail": "bool",
150+
"documentation": null
151+
}]
152+

0 commit comments

Comments
 (0)