Skip to content
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

Do not expand type t in patterns #986

Merged
merged 2 commits into from
May 25, 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 @@ -24,6 +24,7 @@
- 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
- Hover: print signature above docstrings. https://github.com/rescript-lang/rescript-vscode/pull/969
- Adjust function template snippet return. https://github.com/rescript-lang/rescript-vscode/pull/985
- Don't expand `type t` maker functions in patterns. https://github.com/rescript-lang/rescript-vscode/pull/986

#### :rocket: New Feature

Expand Down
21 changes: 19 additions & 2 deletions analysis/src/CompletionBackEnd.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1355,8 +1355,9 @@ let rec completeTypedValue ?(typeArgContext : typeArgContext option) ~rawOpens
let printConstructorArgs = printConstructorArgs ~mode in
let create = Completion.create ?typeArgContext in
match t with
| TtypeT {env; path} ->
if Debug.verbose () then print_endline "[complete_typed_value]--> TtypeT";
| TtypeT {env; path} when mode = Expression ->
if Debug.verbose () then
print_endline "[complete_typed_value]--> TtypeT (Expression)";
(* Find all values in the module with type t *)
let valueWithTypeT t =
match t.Types.desc with
Expand Down Expand Up @@ -1445,6 +1446,22 @@ let rec completeTypedValue ?(typeArgContext : typeArgContext option) ~rawOpens
create "false" ~kind:(Label "bool") ~env;
]
|> filterItems ~prefix
| TtypeT {env; path} ->
if Debug.verbose () then
print_endline "[complete_typed_value]--> TtypeT (Pattern)";
(* This is in patterns. Emit an alias/binding with the module name as a value name. *)
if prefix <> "" then []
else
let moduleName =
match path |> Utils.expandPath with
| _t :: moduleName :: _rest -> String.uncapitalize_ascii moduleName
| _ -> "value"
in
[
create moduleName ~kind:(Label moduleName) ~env
~insertText:("${0:" ^ moduleName ^ "}")
~includesSnippets:true;
]
| Tvariant {env; constructors; variantDecl; variantName} ->
if Debug.verbose () then print_endline "[complete_typed_value]--> Tvariant";
constructors
Expand Down
9 changes: 9 additions & 0 deletions analysis/tests/src/CompletionTypeT.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
let date = Some(Js.Date.make())

type withDate = {date: Js.Date.t}

// let x = switch date { | }
// ^com

// let x: withDate = {date: }
// ^com
112 changes: 112 additions & 0 deletions analysis/tests/src/expected/CompletionTypeT.res.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
Complete src/CompletionTypeT.res 4:26
XXX Not found!
Completable: Cpattern Value[date]
Package opens Pervasives.JsxModules.place holder
Resolved opens 1 pervasives
ContextPath Value[date]
Path date
[{
"label": "None",
"kind": 12,
"tags": [],
"detail": "Js.Date.t",
"documentation": null
}, {
"label": "Some(_)",
"kind": 12,
"tags": [],
"detail": "Js.Date.t",
"documentation": null,
"insertText": "Some(${1:_})",
"insertTextFormat": 2
}, {
"label": "Some(date)",
"kind": 4,
"tags": [],
"detail": "date",
"documentation": null,
"insertText": "Some(${0:date})",
"insertTextFormat": 2
}]

Complete src/CompletionTypeT.res 7:27
XXX Not found!
Completable: Cexpression Type[withDate]->recordField(date)
Package opens Pervasives.JsxModules.place holder
Resolved opens 1 pervasives
ContextPath Type[withDate]
Path withDate
[{
"label": "Js.Date.makeWithYMD()",
"kind": 12,
"tags": [],
"detail": "(~year: float, ~month: float, ~date: float, unit) => t",
"documentation": null,
"insertText": "Js.Date.makeWithYMD($0)",
"insertTextFormat": 2
}, {
"label": "Js.Date.makeWithYMDHM()",
"kind": 12,
"tags": [],
"detail": "(\n ~year: float,\n ~month: float,\n ~date: float,\n ~hours: float,\n ~minutes: float,\n unit,\n) => t",
"documentation": null,
"insertText": "Js.Date.makeWithYMDHM($0)",
"insertTextFormat": 2
}, {
"label": "Js.Date.make()",
"kind": 12,
"tags": [],
"detail": "unit => t",
"documentation": null,
"insertText": "Js.Date.make($0)",
"insertTextFormat": 2
}, {
"label": "Js.Date.fromString()",
"kind": 12,
"tags": [],
"detail": "string => t",
"documentation": null,
"insertText": "Js.Date.fromString($0)",
"insertTextFormat": 2
}, {
"label": "Js.Date.fromFloat()",
"kind": 12,
"tags": [],
"detail": "float => t",
"documentation": null,
"insertText": "Js.Date.fromFloat($0)",
"insertTextFormat": 2
}, {
"label": "Js.Date.parse()",
"kind": 12,
"tags": [],
"detail": "string => t",
"documentation": null,
"insertText": "Js.Date.parse($0)",
"insertTextFormat": 2
}, {
"label": "Js.Date.makeWithYM()",
"kind": 12,
"tags": [],
"detail": "(~year: float, ~month: float, unit) => t",
"documentation": null,
"insertText": "Js.Date.makeWithYM($0)",
"insertTextFormat": 2
}, {
"label": "Js.Date.makeWithYMDHMS()",
"kind": 12,
"tags": [],
"detail": "(\n ~year: float,\n ~month: float,\n ~date: float,\n ~hours: float,\n ~minutes: float,\n ~seconds: float,\n unit,\n) => t",
"documentation": null,
"insertText": "Js.Date.makeWithYMDHMS($0)",
"insertTextFormat": 2
}, {
"label": "Js.Date.makeWithYMDH()",
"kind": 12,
"tags": [],
"detail": "(\n ~year: float,\n ~month: float,\n ~date: float,\n ~hours: float,\n unit,\n) => t",
"documentation": null,
"insertText": "Js.Date.makeWithYMDH($0)",
"insertTextFormat": 2
}]

Loading