Skip to content

Commit dc55067

Browse files
authored
Inlay Hints: better support for destructuring (#540)
* better support for destructuring * update output test
1 parent b16d87a commit dc55067

File tree

5 files changed

+106
-14
lines changed

5 files changed

+106
-14
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
#### :bug: Bug Fix
2323

2424
- Fix Incorrect semantic highlighting of `external` declarations https://github.com/rescript-lang/rescript-vscode/pull/517
25-
2625
- Fix issue where doc comment with nested comments inside is not shown properly on hover https://github.com/rescript-lang/rescript-vscode/pull/526
2726
- Fix server crashes when open file is removed from disk with inlayHints enabled https://github.com/rescript-lang/rescript-vscode/issues/538
27+
- Fix inlay hint for destructured record/array https://github.com/rescript-lang/rescript-vscode/issues/536
2828

2929
## v1.4.2
3030

analysis/src/Commands.ml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -394,8 +394,11 @@ let test ~path =
394394
indent indent newText)))
395395
| "dia" -> diagnosticSyntax ~path
396396
| "hin" ->
397-
let line_start = 0 in
398-
let line_end = 6 in
397+
(* Get all inlay Hint between line 1 and n.
398+
Don't get the first line = 0.
399+
*)
400+
let line_start = 1 in
401+
let line_end = 34 in
399402
print_endline
400403
("Inlay Hint " ^ path ^ " " ^ string_of_int line_start ^ ":"
401404
^ string_of_int line_end);

analysis/src/Hint.ml

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ let inlay ~path ~pos ~maxLength ~debug =
5151
| _ -> processFunction e)
5252
| _ -> ()
5353
in
54+
let rec processPattern (pat : Parsetree.pattern) =
55+
match pat.ppat_desc with
56+
| Ppat_tuple pl -> pl |> List.iter processPattern
57+
| Ppat_record (fields, _) ->
58+
fields |> List.iter (fun (_, p) -> processPattern p)
59+
| Ppat_array fields -> fields |> List.iter processPattern
60+
| Ppat_var {loc} -> push loc Type
61+
| _ -> ()
62+
in
5463
let value_binding (iterator : Ast_iterator.iterator)
5564
(vb : Parsetree.value_binding) =
5665
(match vb with
@@ -66,10 +75,8 @@ let inlay ~path ~pos ~maxLength ~debug =
6675
};
6776
} ->
6877
push vb.pvb_pat.ppat_loc Type
69-
| {pvb_pat = {ppat_desc = Ppat_tuple tuples}} ->
70-
List.iter
71-
(fun (tuple : Parsetree.pattern) -> push tuple.ppat_loc Type)
72-
tuples
78+
| {pvb_pat = {ppat_desc = Ppat_tuple _}} -> processPattern vb.pvb_pat
79+
| {pvb_pat = {ppat_desc = Ppat_record _}} -> processPattern vb.pvb_pat
7380
| {
7481
pvb_pat = {ppat_desc = Ppat_var _};
7582
pvb_expr = {pexp_desc = Pexp_fun (_, _, pat, e)};

analysis/tests/src/InlayHint.res

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
let not_include = "Not Include"
12
let string = "ReScript"
23
let number = 1
34
let float = 1.1
@@ -17,4 +18,19 @@ let tuple = ("ReScript", "lol")
1718

1819
let (lang, _) = tuple
1920

21+
type foo = {
22+
name: string,
23+
age: int,
24+
}
25+
26+
let bar = () => ({name: "ReScript", age: 2}, tuple)
27+
let ({name, age}, t) = bar()
28+
29+
let alice = {
30+
name: "Alice",
31+
age: 42,
32+
};
33+
34+
let {name, age} = alice;
35+
2036
//^hin

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

Lines changed: 73 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,102 @@
1-
Inlay Hint src/InlayHint.res 0:6
1+
Inlay Hint src/InlayHint.res 1:34
22
[{
3-
"position": {"line": 5, "character": 15},
3+
"position": {"line": 33, "character": 14},
44
"label": ": int",
55
"kind": 1,
66
"paddingLeft": true,
77
"paddingRight": false
88
}, {
9-
"position": {"line": 5, "character": 12},
9+
"position": {"line": 33, "character": 9},
10+
"label": ": string",
11+
"kind": 1,
12+
"paddingLeft": true,
13+
"paddingRight": false
14+
}, {
15+
"position": {"line": 28, "character": 9},
16+
"label": ": foo",
17+
"kind": 1,
18+
"paddingLeft": true,
19+
"paddingRight": false
20+
}, {
21+
"position": {"line": 26, "character": 19},
22+
"label": ": (string, string)",
23+
"kind": 1,
24+
"paddingLeft": true,
25+
"paddingRight": false
26+
}, {
27+
"position": {"line": 26, "character": 15},
28+
"label": ": int",
29+
"kind": 1,
30+
"paddingLeft": true,
31+
"paddingRight": false
32+
}, {
33+
"position": {"line": 26, "character": 10},
34+
"label": ": string",
35+
"kind": 1,
36+
"paddingLeft": true,
37+
"paddingRight": false
38+
}, {
39+
"position": {"line": 18, "character": 9},
40+
"label": ": string",
41+
"kind": 1,
42+
"paddingLeft": true,
43+
"paddingRight": false
44+
}, {
45+
"position": {"line": 16, "character": 9},
46+
"label": ": (string, string)",
47+
"kind": 1,
48+
"paddingLeft": true,
49+
"paddingRight": false
50+
}, {
51+
"position": {"line": 14, "character": 17},
52+
"label": ": string",
53+
"kind": 1,
54+
"paddingLeft": true,
55+
"paddingRight": false
56+
}, {
57+
"position": {"line": 10, "character": 24},
1058
"label": ": int",
1159
"kind": 1,
1260
"paddingLeft": true,
1361
"paddingRight": false
1462
}, {
15-
"position": {"line": 3, "character": 8},
63+
"position": {"line": 8, "character": 10},
64+
"label": ": int",
65+
"kind": 1,
66+
"paddingLeft": true,
67+
"paddingRight": false
68+
}, {
69+
"position": {"line": 6, "character": 15},
70+
"label": ": int",
71+
"kind": 1,
72+
"paddingLeft": true,
73+
"paddingRight": false
74+
}, {
75+
"position": {"line": 6, "character": 12},
76+
"label": ": int",
77+
"kind": 1,
78+
"paddingLeft": true,
79+
"paddingRight": false
80+
}, {
81+
"position": {"line": 4, "character": 8},
1682
"label": ": char",
1783
"kind": 1,
1884
"paddingLeft": true,
1985
"paddingRight": false
2086
}, {
21-
"position": {"line": 2, "character": 9},
87+
"position": {"line": 3, "character": 9},
2288
"label": ": float",
2389
"kind": 1,
2490
"paddingLeft": true,
2591
"paddingRight": false
2692
}, {
27-
"position": {"line": 1, "character": 10},
93+
"position": {"line": 2, "character": 10},
2894
"label": ": int",
2995
"kind": 1,
3096
"paddingLeft": true,
3197
"paddingRight": false
3298
}, {
33-
"position": {"line": 0, "character": 10},
99+
"position": {"line": 1, "character": 10},
34100
"label": ": string",
35101
"kind": 1,
36102
"paddingLeft": true,

0 commit comments

Comments
 (0)