Skip to content

Make JSX completion work for React.component #966

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#### :nail_care: Polish

- Make sure doc strings are always on top in hovers. https://github.com/rescript-lang/rescript-vscode/pull/956
- Make JSX completion work for `make` functions of type `React.component<props>`, like what you get when using `React.lazy_`. https://github.com/rescript-lang/rescript-vscode/pull/966

#### :rocket: New Feature

Expand Down
11 changes: 11 additions & 0 deletions analysis/src/CompletionJsx.ml
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,17 @@ let getJsxLabels ~componentPath ~findTypeOfValue ~package =
_ ) ->
(* JSX V3 *)
getFieldsV3 tObj
| Tconstr (p, [propsType], _) when Path.name p = "React.component" -> (
let rec getPropsType (t : Types.type_expr) =
match t.desc with
| Tlink t1 | Tsubst t1 | Tpoly (t1, []) -> getPropsType t1
| Tconstr (path, typeArgs, _) when Path.last path = "props" ->
Some (path, typeArgs)
| _ -> None
in
match propsType |> getPropsType with
| Some (path, typeArgs) -> getFieldsV4 ~path ~typeArgs
| None -> [])
| Tarrow (Nolabel, {desc = Tconstr (path, typeArgs, _)}, _, _)
when Path.last path = "props" ->
(* JSX V4 *)
Expand Down
10 changes: 10 additions & 0 deletions analysis/tests/src/CompletableComponent.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
type status = On | Off

@@jsxConfig({version: 4, mode: "automatic"})

@react.component
let make = (~status: status, ~name: string) => {
ignore(status)
ignore(name)
React.null
}
10 changes: 10 additions & 0 deletions analysis/tests/src/CompletionJsxProps.res
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,13 @@ let tsomeVar = #two

// let _ = <CompletionSupport.TestComponent on={t}
// ^com

@@jsxConfig({version: 4, mode: "automatic"})

module CompletableComponentLazy = {
let loadComponent = () => Js.import(CompletableComponent.make)
let make = React.lazy_(loadComponent)
}

// let _ = <CompletableComponentLazy status=
// ^com
Empty file.
26 changes: 26 additions & 0 deletions analysis/tests/src/expected/CompletionJsxProps.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -330,3 +330,29 @@ Path CompletionSupport.TestComponent.make
"documentation": null
}]

Complete src/CompletionJsxProps.res 44:44
posCursor:[44:44] posNoWhite:[44:43] Found expr:[44:12->44:44]
JSX <CompletableComponentLazy:[44:12->44:36] status[44:37->44:43]=...__ghost__[0:-1->0:-1]> _children:None
Completable: Cexpression CJsxPropValue [CompletableComponentLazy] status
Package opens Pervasives.JsxModules.place holder
Resolved opens 1 pervasives
ContextPath CJsxPropValue [CompletableComponentLazy] status
Path CompletableComponentLazy.make
[{
"label": "On",
"kind": 4,
"tags": [],
"detail": "On",
"documentation": {"kind": "markdown", "value": "```rescript\nOn\n```\n\n```rescript\ntype status = On | Off\n```"},
"insertText": "{On}",
"insertTextFormat": 2
}, {
"label": "Off",
"kind": 4,
"tags": [],
"detail": "Off",
"documentation": {"kind": "markdown", "value": "```rescript\nOff\n```\n\n```rescript\ntype status = On | Off\n```"},
"insertText": "{Off}",
"insertTextFormat": 2
}]

Loading