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 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"}} +