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

Expand type aliases when hovering #881

Merged
merged 2 commits into from
Jan 7, 2024
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: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
47 changes: 26 additions & 21 deletions analysis/src/Hover.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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. *)
Expand Down Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion analysis/tests/src/Hover.res
Original file line number Diff line number Diff line change
Expand Up @@ -262,4 +262,7 @@ let coolVariant = CoolVariant
// ^hov

module Arr = Belt.Array
// ^hov
// ^hov

type aliased = variant
// ^hov
3 changes: 3 additions & 0 deletions analysis/tests/src/expected/Hover.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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"}}

Loading