From 88a772f6e97001778d68dd1ef4a1fafb6fc9c977 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A9di-R=C3=A9mi=20Hashim?= Date: Fri, 23 May 2025 21:32:09 +0100 Subject: [PATCH 1/5] Show variant inline record type on hover --- analysis/src/Hover.ml | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/analysis/src/Hover.ml b/analysis/src/Hover.ml index f0979d695c..fc77ae8ec4 100644 --- a/analysis/src/Hover.ml +++ b/analysis/src/Hover.ml @@ -270,17 +270,13 @@ let newHover ~full:{file; package} ~supportsMarkdownLinks locItem = | `Declared -> let typeString, docstring = t |> fromType ~docstring in typeString :: docstring - | `Constructor {cname = {txt}; args; docstring} -> - let typeString, docstring = t |> fromType ~docstring in - let argsString = - match args with - | InlineRecord _ | Args [] -> "" - | Args args -> - args - |> List.map (fun (t, _) -> Shared.typeToString t) - |> String.concat ", " |> Printf.sprintf "(%s)" + | `Constructor constructor -> + let typeString, docstring = + t |> fromType ~docstring:constructor.docstring in - typeString :: Markdown.codeBlock (txt ^ argsString) :: docstring + typeString + :: Markdown.codeBlock (CompletionBackEnd.showConstructor constructor) + :: docstring | `Field -> let typeString, docstring = t |> fromType ~docstring in typeString :: docstring) From 4aa0757ca25d1bb1710da017b13391f3dada402e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A9di-R=C3=A9mi=20Hashim?= Date: Sat, 24 May 2025 01:38:41 +0100 Subject: [PATCH 2/5] Show variant constructor type information below type on hover --- analysis/src/Hover.ml | 25 ++++++++++++------- .../tests/src/expected/Hover.res.txt | 2 +- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/analysis/src/Hover.ml b/analysis/src/Hover.ml index fc77ae8ec4..91a848a78f 100644 --- a/analysis/src/Hover.ml +++ b/analysis/src/Hover.ml @@ -137,13 +137,21 @@ let expandTypes ~file ~package ~supportsMarkdownLinks typ = `Default ) (* Produces a hover with relevant types expanded in the main type being hovered. *) -let hoverWithExpandedTypes ~file ~package ~supportsMarkdownLinks typ = - let typeString = Markdown.codeBlock (typ |> Shared.typeToString) in +let hoverWithExpandedTypes ~file ~package ~supportsMarkdownLinks ?constructor + typ = let expandedTypes, expansionType = expandTypes ~file ~package ~supportsMarkdownLinks typ in match expansionType with - | `Default -> typeString :: expandedTypes |> String.concat "\n" + | `Default -> + let typeString = Shared.typeToString typ in + let typeString = + match constructor with + | Some constructor -> + typeString ^ "\n" ^ CompletionBackEnd.showConstructor constructor + | None -> typeString + in + Markdown.codeBlock typeString :: expandedTypes |> String.concat "\n" | `InlineType -> expandedTypes |> String.concat "\n" (* Leverages autocomplete functionality to produce a hover for a position. This @@ -256,8 +264,9 @@ let newHover ~full:{file; package} ~supportsMarkdownLinks locItem = | Const_int64 _ -> "int64" | Const_bigint _ -> "bigint")) | Typed (_, t, locKind) -> - let fromType ~docstring typ = - ( hoverWithExpandedTypes ~file ~package ~supportsMarkdownLinks typ, + let fromType ~docstring ?constructor typ = + ( hoverWithExpandedTypes ~file ~package ~supportsMarkdownLinks + ?constructor typ, docstring ) in let parts = @@ -272,11 +281,9 @@ let newHover ~full:{file; package} ~supportsMarkdownLinks locItem = typeString :: docstring | `Constructor constructor -> let typeString, docstring = - t |> fromType ~docstring:constructor.docstring + t |> fromType ~docstring:constructor.docstring ~constructor in - typeString - :: Markdown.codeBlock (CompletionBackEnd.showConstructor constructor) - :: docstring + typeString :: docstring | `Field -> let typeString, docstring = t |> fromType ~docstring in typeString :: docstring) diff --git a/tests/analysis_tests/tests/src/expected/Hover.res.txt b/tests/analysis_tests/tests/src/expected/Hover.res.txt index bf4f292975..f6e6e6f6f5 100644 --- a/tests/analysis_tests/tests/src/expected/Hover.res.txt +++ b/tests/analysis_tests/tests/src/expected/Hover.res.txt @@ -301,7 +301,7 @@ Hover src/Hover.res 248:19 {"contents": {"kind": "markdown", "value": "```rescript\nbool\n```\n---\n Mighty fine field here. "}} Hover src/Hover.res 253:20 -{"contents": {"kind": "markdown", "value": "```rescript\nvariant\n```\n\n---\n\n```\n \n```\n```rescript\ntype variant = CoolVariant | OtherCoolVariant\n```\nGo to: [Type definition](command:rescript-vscode.go_to_location?%5B%22Hover.res%22%2C251%2C0%5D)\n\n---\n```rescript\nCoolVariant\n```\n---\n Cool variant! "}} +{"contents": {"kind": "markdown", "value": "```rescript\nvariant\nCoolVariant\n```\n\n---\n\n```\n \n```\n```rescript\ntype variant = CoolVariant | OtherCoolVariant\n```\nGo to: [Type definition](command:rescript-vscode.go_to_location?%5B%22Hover.res%22%2C251%2C0%5D)\n\n---\n Cool variant! "}} Hover src/Hover.res 257:23 Nothing at that position. Now trying to use completion. From b6097626d77248b1301a4b8a062c9b7d7c918f6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A9di-R=C3=A9mi=20Hashim?= Date: Sat, 24 May 2025 01:49:02 +0100 Subject: [PATCH 3/5] Refactor Hover.newHover --- analysis/src/Hover.ml | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/analysis/src/Hover.ml b/analysis/src/Hover.ml index 91a848a78f..d7b27ebb28 100644 --- a/analysis/src/Hover.ml +++ b/analysis/src/Hover.ml @@ -264,28 +264,22 @@ let newHover ~full:{file; package} ~supportsMarkdownLinks locItem = | Const_int64 _ -> "int64" | Const_bigint _ -> "bigint")) | Typed (_, t, locKind) -> - let fromType ~docstring ?constructor typ = - ( hoverWithExpandedTypes ~file ~package ~supportsMarkdownLinks - ?constructor typ, - docstring ) + let fromType ?constructor typ = + hoverWithExpandedTypes ~file ~package ~supportsMarkdownLinks ?constructor + typ in let parts = match References.definedForLoc ~file ~package locKind with | None -> - let typeString, docstring = t |> fromType ~docstring:[] in - typeString :: docstring + let typeString = t |> fromType in + [typeString] | Some (docstring, res) -> ( match res with - | `Declared -> - let typeString, docstring = t |> fromType ~docstring in + | `Declared | `Field -> + let typeString = t |> fromType in typeString :: docstring | `Constructor constructor -> - let typeString, docstring = - t |> fromType ~docstring:constructor.docstring ~constructor - in - typeString :: docstring - | `Field -> - let typeString, docstring = t |> fromType ~docstring in - typeString :: docstring) + let typeString = t |> fromType ~constructor in + typeString :: constructor.docstring) in Some (String.concat Markdown.divider parts) From 8a49e357b4ac66560afcbb2dd6b708a16fe05b2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A9di-R=C3=A9mi=20Hashim?= Date: Sat, 24 May 2025 17:13:45 +0100 Subject: [PATCH 4/5] Add analysis tests --- tests/analysis_tests/tests/src/Hover.res | 8 +++++++ .../tests/src/expected/Hover.res.txt | 22 ++++++++++++------- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/tests/analysis_tests/tests/src/Hover.res b/tests/analysis_tests/tests/src/Hover.res index 5dbad3c17c..e245419438 100644 --- a/tests/analysis_tests/tests/src/Hover.res +++ b/tests/analysis_tests/tests/src/Hover.res @@ -254,6 +254,14 @@ type variant = | /** Cool variant! */ CoolVariant | /** Other cool variant */ Ot let coolVariant = CoolVariant // ^hov +type payloadVariants = InlineRecord({field1: int, field2: bool}) | Args(int, bool) + +let payloadVariant = InlineRecord({field1: 1, field2: true}) +// ^hov + +let payloadVariant2 = Args(1, true) +// ^hov + // Hover on unsaved // let fff = "hello"; fff // ^hov diff --git a/tests/analysis_tests/tests/src/expected/Hover.res.txt b/tests/analysis_tests/tests/src/expected/Hover.res.txt index f6e6e6f6f5..b31b9aa14c 100644 --- a/tests/analysis_tests/tests/src/expected/Hover.res.txt +++ b/tests/analysis_tests/tests/src/expected/Hover.res.txt @@ -303,10 +303,16 @@ Hover src/Hover.res 248:19 Hover src/Hover.res 253:20 {"contents": {"kind": "markdown", "value": "```rescript\nvariant\nCoolVariant\n```\n\n---\n\n```\n \n```\n```rescript\ntype variant = CoolVariant | OtherCoolVariant\n```\nGo to: [Type definition](command:rescript-vscode.go_to_location?%5B%22Hover.res%22%2C251%2C0%5D)\n\n---\n Cool variant! "}} -Hover src/Hover.res 257:23 +Hover src/Hover.res 258:22 +{"contents": {"kind": "markdown", "value": "```rescript\npayloadVariants\nInlineRecord({field1: int, field2: bool})\n```\n\n---\n\n```\n \n```\n```rescript\ntype payloadVariants =\n | InlineRecord({field1: int, field2: bool})\n | Args(int, bool)\n```\nGo to: [Type definition](command:rescript-vscode.go_to_location?%5B%22Hover.res%22%2C256%2C0%5D)\n"}} + +Hover src/Hover.res 261:23 +{"contents": {"kind": "markdown", "value": "```rescript\npayloadVariants\nArgs(int, bool)\n```\n\n---\n\n```\n \n```\n```rescript\ntype payloadVariants =\n | InlineRecord({field1: int, field2: bool})\n | Args(int, bool)\n```\nGo to: [Type definition](command:rescript-vscode.go_to_location?%5B%22Hover.res%22%2C256%2C0%5D)\n"}} + +Hover src/Hover.res 265:23 Nothing at that position. Now trying to use completion. -posCursor:[257:23] posNoWhite:[257:22] Found expr:[257:22->257:25] -Pexp_ident fff:[257:22->257:25] +posCursor:[265:23] posNoWhite:[265:22] Found expr:[265:22->265:25] +Pexp_ident fff:[265:22->265:25] Completable: Cpath Value[fff] Package opens Stdlib.place holder Pervasives.JsxModules.place holder Resolved opens 1 Stdlib @@ -317,10 +323,10 @@ Resolved opens 1 Stdlib ContextPath string {"contents": {"kind": "markdown", "value": "```rescript\nstring\n```"}} -Hover src/Hover.res 260:33 +Hover src/Hover.res 268:33 Nothing at that position. Now trying to use completion. -posCursor:[260:33] posNoWhite:[260:32] Found expr:[260:31->260:40] -Pexp_ident someField:[260:31->260:40] +posCursor:[268:33] posNoWhite:[268:32] Found expr:[268:31->268:40] +Pexp_ident someField:[268:31->268:40] Completable: Cpath Value[someField] Package opens Stdlib.place holder Pervasives.JsxModules.place holder Resolved opens 1 Stdlib @@ -333,9 +339,9 @@ ContextPath Value[x] Path x {"contents": {"kind": "markdown", "value": "```rescript\nbool\n```"}} -Hover src/Hover.res 263:8 +Hover src/Hover.res 271:8 {"contents": {"kind": "markdown", "value": "\n [`Belt.Array`]()\n\n **mutable array**: Utilities functions\n\n```rescript\nmodule Array: {\n module Id\n module Array\n module SortArray\n module MutableQueue\n module MutableStack\n module List\n module Range\n module Set\n module Map\n module MutableSet\n module MutableMap\n module HashSet\n module HashMap\n module Option\n module Result\n module Int\n module Float\n}\n```"}} -Hover src/Hover.res 266:6 +Hover src/Hover.res 274:6 {"contents": {"kind": "markdown", "value": "```rescript\ntype aliased = variant\n```\n\n---\n\n```\n \n```\n```rescript\ntype variant = CoolVariant | OtherCoolVariant\n```\nGo to: [Type definition](command:rescript-vscode.go_to_location?%5B%22Hover.res%22%2C251%2C0%5D)\n"}} From ff9bc9e636105967f2538ed3c046002a7443d709 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A9di-R=C3=A9mi=20Hashim?= Date: Sat, 24 May 2025 17:38:30 +0100 Subject: [PATCH 5/5] Add CHANGELOG entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4e25a06b4f..b6088034e6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ - Add `RegExp.flags`. https://github.com/rescript-lang/rescript/pull/7461 - Add `Array.findLast`, `Array.findLastWithIndex`, `Array.findLastIndex`, `Array.findLastIndexWithIndex` and `Array.findLastIndexOpt`. https://github.com/rescript-lang/rescript/pull/7503 - Add `options` argument to `Console.dir`. https://github.com/rescript-lang/rescript/pull/7504 +- Show variant constructor's inline record types on hover. https://github.com/rescript-lang/rescript/pull/7519 #### :bug: Bug fix