Skip to content

Commit 89deb9c

Browse files
authored
Do not expand type t in patterns (#986)
* do not expand type t in patterns * changelog
1 parent 65d6f05 commit 89deb9c

File tree

4 files changed

+141
-2
lines changed

4 files changed

+141
-2
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
- 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
2525
- Hover: print signature above docstrings. https://github.com/rescript-lang/rescript-vscode/pull/969
2626
- Adjust function template snippet return. https://github.com/rescript-lang/rescript-vscode/pull/985
27+
- Don't expand `type t` maker functions in patterns. https://github.com/rescript-lang/rescript-vscode/pull/986
2728

2829
#### :rocket: New Feature
2930

analysis/src/CompletionBackEnd.ml

+19-2
Original file line numberDiff line numberDiff line change
@@ -1355,8 +1355,9 @@ let rec completeTypedValue ?(typeArgContext : typeArgContext option) ~rawOpens
13551355
let printConstructorArgs = printConstructorArgs ~mode in
13561356
let create = Completion.create ?typeArgContext in
13571357
match t with
1358-
| TtypeT {env; path} ->
1359-
if Debug.verbose () then print_endline "[complete_typed_value]--> TtypeT";
1358+
| TtypeT {env; path} when mode = Expression ->
1359+
if Debug.verbose () then
1360+
print_endline "[complete_typed_value]--> TtypeT (Expression)";
13601361
(* Find all values in the module with type t *)
13611362
let valueWithTypeT t =
13621363
match t.Types.desc with
@@ -1445,6 +1446,22 @@ let rec completeTypedValue ?(typeArgContext : typeArgContext option) ~rawOpens
14451446
create "false" ~kind:(Label "bool") ~env;
14461447
]
14471448
|> filterItems ~prefix
1449+
| TtypeT {env; path} ->
1450+
if Debug.verbose () then
1451+
print_endline "[complete_typed_value]--> TtypeT (Pattern)";
1452+
(* This is in patterns. Emit an alias/binding with the module name as a value name. *)
1453+
if prefix <> "" then []
1454+
else
1455+
let moduleName =
1456+
match path |> Utils.expandPath with
1457+
| _t :: moduleName :: _rest -> String.uncapitalize_ascii moduleName
1458+
| _ -> "value"
1459+
in
1460+
[
1461+
create moduleName ~kind:(Label moduleName) ~env
1462+
~insertText:("${0:" ^ moduleName ^ "}")
1463+
~includesSnippets:true;
1464+
]
14481465
| Tvariant {env; constructors; variantDecl; variantName} ->
14491466
if Debug.verbose () then print_endline "[complete_typed_value]--> Tvariant";
14501467
constructors
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
let date = Some(Js.Date.make())
2+
3+
type withDate = {date: Js.Date.t}
4+
5+
// let x = switch date { | }
6+
// ^com
7+
8+
// let x: withDate = {date: }
9+
// ^com
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
Complete src/CompletionTypeT.res 4:26
2+
XXX Not found!
3+
Completable: Cpattern Value[date]
4+
Package opens Pervasives.JsxModules.place holder
5+
Resolved opens 1 pervasives
6+
ContextPath Value[date]
7+
Path date
8+
[{
9+
"label": "None",
10+
"kind": 12,
11+
"tags": [],
12+
"detail": "Js.Date.t",
13+
"documentation": null
14+
}, {
15+
"label": "Some(_)",
16+
"kind": 12,
17+
"tags": [],
18+
"detail": "Js.Date.t",
19+
"documentation": null,
20+
"insertText": "Some(${1:_})",
21+
"insertTextFormat": 2
22+
}, {
23+
"label": "Some(date)",
24+
"kind": 4,
25+
"tags": [],
26+
"detail": "date",
27+
"documentation": null,
28+
"insertText": "Some(${0:date})",
29+
"insertTextFormat": 2
30+
}]
31+
32+
Complete src/CompletionTypeT.res 7:27
33+
XXX Not found!
34+
Completable: Cexpression Type[withDate]->recordField(date)
35+
Package opens Pervasives.JsxModules.place holder
36+
Resolved opens 1 pervasives
37+
ContextPath Type[withDate]
38+
Path withDate
39+
[{
40+
"label": "Js.Date.makeWithYMD()",
41+
"kind": 12,
42+
"tags": [],
43+
"detail": "(~year: float, ~month: float, ~date: float, unit) => t",
44+
"documentation": null,
45+
"insertText": "Js.Date.makeWithYMD($0)",
46+
"insertTextFormat": 2
47+
}, {
48+
"label": "Js.Date.makeWithYMDHM()",
49+
"kind": 12,
50+
"tags": [],
51+
"detail": "(\n ~year: float,\n ~month: float,\n ~date: float,\n ~hours: float,\n ~minutes: float,\n unit,\n) => t",
52+
"documentation": null,
53+
"insertText": "Js.Date.makeWithYMDHM($0)",
54+
"insertTextFormat": 2
55+
}, {
56+
"label": "Js.Date.make()",
57+
"kind": 12,
58+
"tags": [],
59+
"detail": "unit => t",
60+
"documentation": null,
61+
"insertText": "Js.Date.make($0)",
62+
"insertTextFormat": 2
63+
}, {
64+
"label": "Js.Date.fromString()",
65+
"kind": 12,
66+
"tags": [],
67+
"detail": "string => t",
68+
"documentation": null,
69+
"insertText": "Js.Date.fromString($0)",
70+
"insertTextFormat": 2
71+
}, {
72+
"label": "Js.Date.fromFloat()",
73+
"kind": 12,
74+
"tags": [],
75+
"detail": "float => t",
76+
"documentation": null,
77+
"insertText": "Js.Date.fromFloat($0)",
78+
"insertTextFormat": 2
79+
}, {
80+
"label": "Js.Date.parse()",
81+
"kind": 12,
82+
"tags": [],
83+
"detail": "string => t",
84+
"documentation": null,
85+
"insertText": "Js.Date.parse($0)",
86+
"insertTextFormat": 2
87+
}, {
88+
"label": "Js.Date.makeWithYM()",
89+
"kind": 12,
90+
"tags": [],
91+
"detail": "(~year: float, ~month: float, unit) => t",
92+
"documentation": null,
93+
"insertText": "Js.Date.makeWithYM($0)",
94+
"insertTextFormat": 2
95+
}, {
96+
"label": "Js.Date.makeWithYMDHMS()",
97+
"kind": 12,
98+
"tags": [],
99+
"detail": "(\n ~year: float,\n ~month: float,\n ~date: float,\n ~hours: float,\n ~minutes: float,\n ~seconds: float,\n unit,\n) => t",
100+
"documentation": null,
101+
"insertText": "Js.Date.makeWithYMDHMS($0)",
102+
"insertTextFormat": 2
103+
}, {
104+
"label": "Js.Date.makeWithYMDH()",
105+
"kind": 12,
106+
"tags": [],
107+
"detail": "(\n ~year: float,\n ~month: float,\n ~date: float,\n ~hours: float,\n unit,\n) => t",
108+
"documentation": null,
109+
"insertText": "Js.Date.makeWithYMDH($0)",
110+
"insertTextFormat": 2
111+
}]
112+

0 commit comments

Comments
 (0)