From a7bc2d8f37908d2da4f14582d2827623c9ccd68b Mon Sep 17 00:00:00 2001 From: Gabriel Nordeborn Date: Sat, 6 Jan 2024 12:32:14 +0100 Subject: [PATCH 1/2] expand type aliases when hovering --- analysis/src/Hover.ml | 47 +++++++++++++---------- analysis/tests/src/Hover.res | 5 ++- analysis/tests/src/expected/Hover.res.txt | 3 ++ 3 files changed, 33 insertions(+), 22 deletions(-) diff --git a/analysis/src/Hover.ml b/analysis/src/Hover.ml index 1b76a283a..f26881b5c 100644 --- a/analysis/src/Hover.ml +++ b/analysis/src/Hover.ml @@ -100,27 +100,27 @@ let findRelevantTypesFromType ~file ~package typ = let constructors = Shared.findTypeConstructors typesToSearch in constructors |> List.filter_map (fromConstructorPath ~env:envToSearch) +let expandTypes ~file ~package ~supportsMarkdownLinks typ = + findRelevantTypesFromType typ ~file ~package + |> List.map (fun {decl; env; loc; path} -> + let linkToTypeDefinitionStr = + if supportsMarkdownLinks then + Markdown.goToDefinitionText ~env ~pos:loc.Warnings.loc_start + else "" + in + Markdown.divider + ^ (if supportsMarkdownLinks then Markdown.spacing else "") + ^ Markdown.codeBlock + (decl + |> Shared.declToString ~printNameAsIs:true + (SharedTypes.pathIdentToString path)) + ^ linkToTypeDefinitionStr ^ "\n") + (* 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 types = findRelevantTypesFromType typ ~file ~package in - let typeDefinitions = - types - |> List.map (fun {decl; env; loc; path} -> - let linkToTypeDefinitionStr = - if supportsMarkdownLinks then - Markdown.goToDefinitionText ~env ~pos:loc.Warnings.loc_start - else "" - in - Markdown.divider - ^ (if supportsMarkdownLinks then Markdown.spacing else "") - ^ Markdown.codeBlock - (decl - |> Shared.declToString ~printNameAsIs:true - (SharedTypes.pathIdentToString path)) - ^ linkToTypeDefinitionStr ^ "\n") - in - typeString :: typeDefinitions |> String.concat "\n" + typeString :: expandTypes ~file ~package ~supportsMarkdownLinks typ + |> String.concat "\n" (* Leverages autocomplete functionality to produce a hover for a position. This makes it (most often) work with unsaved content. *) @@ -166,9 +166,14 @@ let getHoverViaCompletions ~debug ~path ~pos ~currentFile ~forHover let newHover ~full:{file; package} ~supportsMarkdownLinks locItem = match locItem.locType with - | TypeDefinition (name, decl, _stamp) -> - let typeDef = Shared.declToString name decl in - Some (Markdown.codeBlock typeDef) + | TypeDefinition (name, decl, _stamp) -> ( + let typeDef = Markdown.codeBlock (Shared.declToString name decl) in + match decl.type_manifest with + | None -> Some typeDef + | Some typ -> + Some + (typeDef :: expandTypes ~file ~package ~supportsMarkdownLinks typ + |> String.concat "\n")) | LModule (Definition (stamp, _tip)) | LModule (LocalReference (stamp, _tip)) -> ( match Stamps.findModule file.stamps stamp with diff --git a/analysis/tests/src/Hover.res b/analysis/tests/src/Hover.res index bbfff37a5..704946272 100644 --- a/analysis/tests/src/Hover.res +++ b/analysis/tests/src/Hover.res @@ -262,4 +262,7 @@ let coolVariant = CoolVariant // ^hov module Arr = Belt.Array -// ^hov \ No newline at end of file +// ^hov + +type aliased = variant +// ^hov \ No newline at end of file diff --git a/analysis/tests/src/expected/Hover.res.txt b/analysis/tests/src/expected/Hover.res.txt index 9fdad7048..773bd50f3 100644 --- a/analysis/tests/src/expected/Hover.res.txt +++ b/analysis/tests/src/expected/Hover.res.txt @@ -264,3 +264,6 @@ Path x Hover src/Hover.res 263: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 +{"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 9aeca5735c526694b90c9039a35ffa8f99be23d5 Mon Sep 17 00:00:00 2001 From: Gabriel Nordeborn Date: Sat, 6 Jan 2024 12:33:47 +0100 Subject: [PATCH 2/2] changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 945d9c850..4f691f0c9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,8 @@ ## 1.32.0 +- Expand type aliases in hovers. https://github.com/rescript-lang/rescript-vscode/pull/881 + #### :bug: Bug Fix - Fix so that you don't need a leading `#` to complete for polyvariant constructors. https://github.com/rescript-lang/rescript-vscode/pull/874