From 5dcbe31fa8245707f54cb412f7f74be3503c6913 Mon Sep 17 00:00:00 2001 From: Gabriel Nordeborn Date: Sat, 25 May 2024 13:22:01 +0200 Subject: [PATCH 1/2] do not expand type t in patterns --- analysis/src/CompletionBackEnd.ml | 21 +++- analysis/tests/src/CompletionTypeT.res | 9 ++ .../src/expected/CompletionTypeT.res.txt | 112 ++++++++++++++++++ 3 files changed, 140 insertions(+), 2 deletions(-) create mode 100644 analysis/tests/src/CompletionTypeT.res create mode 100644 analysis/tests/src/expected/CompletionTypeT.res.txt diff --git a/analysis/src/CompletionBackEnd.ml b/analysis/src/CompletionBackEnd.ml index 8550c07c5..4d386931a 100644 --- a/analysis/src/CompletionBackEnd.ml +++ b/analysis/src/CompletionBackEnd.ml @@ -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 @@ -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 diff --git a/analysis/tests/src/CompletionTypeT.res b/analysis/tests/src/CompletionTypeT.res new file mode 100644 index 000000000..86371b35f --- /dev/null +++ b/analysis/tests/src/CompletionTypeT.res @@ -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 diff --git a/analysis/tests/src/expected/CompletionTypeT.res.txt b/analysis/tests/src/expected/CompletionTypeT.res.txt new file mode 100644 index 000000000..30c972299 --- /dev/null +++ b/analysis/tests/src/expected/CompletionTypeT.res.txt @@ -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 + }] + From 0eee4b0931f032fe7630f35f4a62349cc182d6a4 Mon Sep 17 00:00:00 2001 From: Gabriel Nordeborn Date: Sat, 25 May 2024 16:59:16 +0200 Subject: [PATCH 2/2] changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 625243662..5e170b956 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,7 @@ - Make JSX completion work for `make` functions of type `React.component`, 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