From 47e542355ddbcca1a07631124fcbf0b03687fc63 Mon Sep 17 00:00:00 2001 From: Pedro Castro Date: Mon, 1 Aug 2022 20:39:05 -0300 Subject: [PATCH 1/2] better support for destructuring --- CHANGELOG.md | 2 +- analysis/src/Commands.ml | 2 +- analysis/src/Hint.ml | 15 ++-- analysis/tests/src/InlayHint.res | 15 ++++ analysis/tests/src/expected/InlayHint.res.txt | 68 ++++++++++++++++++- 5 files changed, 95 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e957cdf72..5741cd2a8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,8 +22,8 @@ #### :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 inlay hint for destructured record/array https://github.com/rescript-lang/rescript-vscode/issues/536 ## v1.4.2 diff --git a/analysis/src/Commands.ml b/analysis/src/Commands.ml index 5fcbca9ac..1a9c730e8 100644 --- a/analysis/src/Commands.ml +++ b/analysis/src/Commands.ml @@ -395,7 +395,7 @@ let test ~path = | "dia" -> diagnosticSyntax ~path | "hin" -> let line_start = 0 in - let line_end = 6 in + let line_end = 34 in print_endline ("Inlay Hint " ^ path ^ " " ^ string_of_int line_start ^ ":" ^ string_of_int line_end); diff --git a/analysis/src/Hint.ml b/analysis/src/Hint.ml index 0f90ff8ea..69f03c812 100644 --- a/analysis/src/Hint.ml +++ b/analysis/src/Hint.ml @@ -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 @@ -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)}; diff --git a/analysis/tests/src/InlayHint.res b/analysis/tests/src/InlayHint.res index ef49412e9..0912fd3b6 100644 --- a/analysis/tests/src/InlayHint.res +++ b/analysis/tests/src/InlayHint.res @@ -17,4 +17,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 \ No newline at end of file diff --git a/analysis/tests/src/expected/InlayHint.res.txt b/analysis/tests/src/expected/InlayHint.res.txt index a129f9331..09d386f26 100644 --- a/analysis/tests/src/expected/InlayHint.res.txt +++ b/analysis/tests/src/expected/InlayHint.res.txt @@ -1,5 +1,71 @@ -Inlay Hint src/InlayHint.res 0:6 +Inlay Hint src/InlayHint.res 0:34 [{ + "position": {"line": 32, "character": 14}, + "label": ": int", + "kind": 1, + "paddingLeft": true, + "paddingRight": false +}, { + "position": {"line": 32, "character": 9}, + "label": ": string", + "kind": 1, + "paddingLeft": true, + "paddingRight": false +}, { + "position": {"line": 27, "character": 9}, + "label": ": foo", + "kind": 1, + "paddingLeft": true, + "paddingRight": false +}, { + "position": {"line": 25, "character": 19}, + "label": ": (string, string)", + "kind": 1, + "paddingLeft": true, + "paddingRight": false +}, { + "position": {"line": 25, "character": 15}, + "label": ": int", + "kind": 1, + "paddingLeft": true, + "paddingRight": false +}, { + "position": {"line": 25, "character": 10}, + "label": ": string", + "kind": 1, + "paddingLeft": true, + "paddingRight": false +}, { + "position": {"line": 17, "character": 9}, + "label": ": string", + "kind": 1, + "paddingLeft": true, + "paddingRight": false +}, { + "position": {"line": 15, "character": 9}, + "label": ": (string, string)", + "kind": 1, + "paddingLeft": true, + "paddingRight": false +}, { + "position": {"line": 13, "character": 17}, + "label": ": string", + "kind": 1, + "paddingLeft": true, + "paddingRight": false +}, { + "position": {"line": 9, "character": 24}, + "label": ": int", + "kind": 1, + "paddingLeft": true, + "paddingRight": false +}, { + "position": {"line": 7, "character": 10}, + "label": ": int", + "kind": 1, + "paddingLeft": true, + "paddingRight": false +}, { "position": {"line": 5, "character": 15}, "label": ": int", "kind": 1, From 6b8f59c64b4a2f2e1ce88f190d5ac1ed47f6145c Mon Sep 17 00:00:00 2001 From: Pedro Castro Date: Tue, 2 Aug 2022 16:46:23 -0300 Subject: [PATCH 2/2] update output test --- analysis/tests/src/expected/InlayHint.res.txt | 36 +++++++++---------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/analysis/tests/src/expected/InlayHint.res.txt b/analysis/tests/src/expected/InlayHint.res.txt index 09d386f26..b9bef15fa 100644 --- a/analysis/tests/src/expected/InlayHint.res.txt +++ b/analysis/tests/src/expected/InlayHint.res.txt @@ -1,102 +1,102 @@ -Inlay Hint src/InlayHint.res 0:34 +Inlay Hint src/InlayHint.res 1:34 [{ - "position": {"line": 32, "character": 14}, + "position": {"line": 33, "character": 14}, "label": ": int", "kind": 1, "paddingLeft": true, "paddingRight": false }, { - "position": {"line": 32, "character": 9}, + "position": {"line": 33, "character": 9}, "label": ": string", "kind": 1, "paddingLeft": true, "paddingRight": false }, { - "position": {"line": 27, "character": 9}, + "position": {"line": 28, "character": 9}, "label": ": foo", "kind": 1, "paddingLeft": true, "paddingRight": false }, { - "position": {"line": 25, "character": 19}, + "position": {"line": 26, "character": 19}, "label": ": (string, string)", "kind": 1, "paddingLeft": true, "paddingRight": false }, { - "position": {"line": 25, "character": 15}, + "position": {"line": 26, "character": 15}, "label": ": int", "kind": 1, "paddingLeft": true, "paddingRight": false }, { - "position": {"line": 25, "character": 10}, + "position": {"line": 26, "character": 10}, "label": ": string", "kind": 1, "paddingLeft": true, "paddingRight": false }, { - "position": {"line": 17, "character": 9}, + "position": {"line": 18, "character": 9}, "label": ": string", "kind": 1, "paddingLeft": true, "paddingRight": false }, { - "position": {"line": 15, "character": 9}, + "position": {"line": 16, "character": 9}, "label": ": (string, string)", "kind": 1, "paddingLeft": true, "paddingRight": false }, { - "position": {"line": 13, "character": 17}, + "position": {"line": 14, "character": 17}, "label": ": string", "kind": 1, "paddingLeft": true, "paddingRight": false }, { - "position": {"line": 9, "character": 24}, + "position": {"line": 10, "character": 24}, "label": ": int", "kind": 1, "paddingLeft": true, "paddingRight": false }, { - "position": {"line": 7, "character": 10}, + "position": {"line": 8, "character": 10}, "label": ": int", "kind": 1, "paddingLeft": true, "paddingRight": false }, { - "position": {"line": 5, "character": 15}, + "position": {"line": 6, "character": 15}, "label": ": int", "kind": 1, "paddingLeft": true, "paddingRight": false }, { - "position": {"line": 5, "character": 12}, + "position": {"line": 6, "character": 12}, "label": ": int", "kind": 1, "paddingLeft": true, "paddingRight": false }, { - "position": {"line": 3, "character": 8}, + "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,