Skip to content

Commit

Permalink
Inlay Hints: better support for destructuring (#540)
Browse files Browse the repository at this point in the history
* better support for destructuring

* update output test
  • Loading branch information
aspeddro authored Aug 2, 2022
1 parent b16d87a commit dc55067
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 14 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
#### :bug: Bug Fix

- Fix Incorrect semantic highlighting of `external` declarations https://github.com/rescript-lang/rescript-vscode/pull/517

- Fix issue where doc comment with nested comments inside is not shown properly on hover https://github.com/rescript-lang/rescript-vscode/pull/526
- Fix server crashes when open file is removed from disk with inlayHints enabled https://github.com/rescript-lang/rescript-vscode/issues/538
- Fix inlay hint for destructured record/array https://github.com/rescript-lang/rescript-vscode/issues/536

## v1.4.2

Expand Down
7 changes: 5 additions & 2 deletions analysis/src/Commands.ml
Original file line number Diff line number Diff line change
Expand Up @@ -394,8 +394,11 @@ let test ~path =
indent indent newText)))
| "dia" -> diagnosticSyntax ~path
| "hin" ->
let line_start = 0 in
let line_end = 6 in
(* Get all inlay Hint between line 1 and n.
Don't get the first line = 0.
*)
let line_start = 1 in
let line_end = 34 in
print_endline
("Inlay Hint " ^ path ^ " " ^ string_of_int line_start ^ ":"
^ string_of_int line_end);
Expand Down
15 changes: 11 additions & 4 deletions analysis/src/Hint.ml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ let inlay ~path ~pos ~maxLength ~debug =
| _ -> processFunction e)
| _ -> ()
in
let rec processPattern (pat : Parsetree.pattern) =
match pat.ppat_desc with
| Ppat_tuple pl -> pl |> List.iter processPattern
| Ppat_record (fields, _) ->
fields |> List.iter (fun (_, p) -> processPattern p)
| Ppat_array fields -> fields |> List.iter processPattern
| Ppat_var {loc} -> push loc Type
| _ -> ()
in
let value_binding (iterator : Ast_iterator.iterator)
(vb : Parsetree.value_binding) =
(match vb with
Expand All @@ -66,10 +75,8 @@ let inlay ~path ~pos ~maxLength ~debug =
};
} ->
push vb.pvb_pat.ppat_loc Type
| {pvb_pat = {ppat_desc = Ppat_tuple tuples}} ->
List.iter
(fun (tuple : Parsetree.pattern) -> push tuple.ppat_loc Type)
tuples
| {pvb_pat = {ppat_desc = Ppat_tuple _}} -> processPattern vb.pvb_pat
| {pvb_pat = {ppat_desc = Ppat_record _}} -> processPattern vb.pvb_pat
| {
pvb_pat = {ppat_desc = Ppat_var _};
pvb_expr = {pexp_desc = Pexp_fun (_, _, pat, e)};
Expand Down
16 changes: 16 additions & 0 deletions analysis/tests/src/InlayHint.res
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
let not_include = "Not Include"
let string = "ReScript"
let number = 1
let float = 1.1
Expand All @@ -17,4 +18,19 @@ let tuple = ("ReScript", "lol")

let (lang, _) = tuple

type foo = {
name: string,
age: int,
}

let bar = () => ({name: "ReScript", age: 2}, tuple)
let ({name, age}, t) = bar()

let alice = {
name: "Alice",
age: 42,
};

let {name, age} = alice;

//^hin
80 changes: 73 additions & 7 deletions analysis/tests/src/expected/InlayHint.res.txt
Original file line number Diff line number Diff line change
@@ -1,36 +1,102 @@
Inlay Hint src/InlayHint.res 0:6
Inlay Hint src/InlayHint.res 1:34
[{
"position": {"line": 5, "character": 15},
"position": {"line": 33, "character": 14},
"label": ": int",
"kind": 1,
"paddingLeft": true,
"paddingRight": false
}, {
"position": {"line": 5, "character": 12},
"position": {"line": 33, "character": 9},
"label": ": string",
"kind": 1,
"paddingLeft": true,
"paddingRight": false
}, {
"position": {"line": 28, "character": 9},
"label": ": foo",
"kind": 1,
"paddingLeft": true,
"paddingRight": false
}, {
"position": {"line": 26, "character": 19},
"label": ": (string, string)",
"kind": 1,
"paddingLeft": true,
"paddingRight": false
}, {
"position": {"line": 26, "character": 15},
"label": ": int",
"kind": 1,
"paddingLeft": true,
"paddingRight": false
}, {
"position": {"line": 26, "character": 10},
"label": ": string",
"kind": 1,
"paddingLeft": true,
"paddingRight": false
}, {
"position": {"line": 18, "character": 9},
"label": ": string",
"kind": 1,
"paddingLeft": true,
"paddingRight": false
}, {
"position": {"line": 16, "character": 9},
"label": ": (string, string)",
"kind": 1,
"paddingLeft": true,
"paddingRight": false
}, {
"position": {"line": 14, "character": 17},
"label": ": string",
"kind": 1,
"paddingLeft": true,
"paddingRight": false
}, {
"position": {"line": 10, "character": 24},
"label": ": int",
"kind": 1,
"paddingLeft": true,
"paddingRight": false
}, {
"position": {"line": 3, "character": 8},
"position": {"line": 8, "character": 10},
"label": ": int",
"kind": 1,
"paddingLeft": true,
"paddingRight": false
}, {
"position": {"line": 6, "character": 15},
"label": ": int",
"kind": 1,
"paddingLeft": true,
"paddingRight": false
}, {
"position": {"line": 6, "character": 12},
"label": ": int",
"kind": 1,
"paddingLeft": true,
"paddingRight": false
}, {
"position": {"line": 4, "character": 8},
"label": ": char",
"kind": 1,
"paddingLeft": true,
"paddingRight": false
}, {
"position": {"line": 2, "character": 9},
"position": {"line": 3, "character": 9},
"label": ": float",
"kind": 1,
"paddingLeft": true,
"paddingRight": false
}, {
"position": {"line": 1, "character": 10},
"position": {"line": 2, "character": 10},
"label": ": int",
"kind": 1,
"paddingLeft": true,
"paddingRight": false
}, {
"position": {"line": 0, "character": 10},
"position": {"line": 1, "character": 10},
"label": ": string",
"kind": 1,
"paddingLeft": true,
Expand Down

0 comments on commit dc55067

Please sign in to comment.