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

Inlay Hints: better support for destructuring #540

Merged
merged 3 commits into from
Aug 2, 2022
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
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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just noticed this now -- would you add a comment about the line number hardcoded in the test?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

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