Skip to content

Commit 80379fe

Browse files
authored
Expand type aliases when hovering (#881)
* expand type aliases when hovering * changelog
1 parent 2d1742a commit 80379fe

File tree

4 files changed

+35
-22
lines changed

4 files changed

+35
-22
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
## 1.32.0
1616

17+
- Expand type aliases in hovers. https://github.com/rescript-lang/rescript-vscode/pull/881
18+
1719
#### :bug: Bug Fix
1820

1921
- Fix so that you don't need a leading `#` to complete for polyvariant constructors. https://github.com/rescript-lang/rescript-vscode/pull/874

analysis/src/Hover.ml

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -100,27 +100,27 @@ let findRelevantTypesFromType ~file ~package typ =
100100
let constructors = Shared.findTypeConstructors typesToSearch in
101101
constructors |> List.filter_map (fromConstructorPath ~env:envToSearch)
102102

103+
let expandTypes ~file ~package ~supportsMarkdownLinks typ =
104+
findRelevantTypesFromType typ ~file ~package
105+
|> List.map (fun {decl; env; loc; path} ->
106+
let linkToTypeDefinitionStr =
107+
if supportsMarkdownLinks then
108+
Markdown.goToDefinitionText ~env ~pos:loc.Warnings.loc_start
109+
else ""
110+
in
111+
Markdown.divider
112+
^ (if supportsMarkdownLinks then Markdown.spacing else "")
113+
^ Markdown.codeBlock
114+
(decl
115+
|> Shared.declToString ~printNameAsIs:true
116+
(SharedTypes.pathIdentToString path))
117+
^ linkToTypeDefinitionStr ^ "\n")
118+
103119
(* Produces a hover with relevant types expanded in the main type being hovered. *)
104120
let hoverWithExpandedTypes ~file ~package ~supportsMarkdownLinks typ =
105121
let typeString = Markdown.codeBlock (typ |> Shared.typeToString) in
106-
let types = findRelevantTypesFromType typ ~file ~package in
107-
let typeDefinitions =
108-
types
109-
|> List.map (fun {decl; env; loc; path} ->
110-
let linkToTypeDefinitionStr =
111-
if supportsMarkdownLinks then
112-
Markdown.goToDefinitionText ~env ~pos:loc.Warnings.loc_start
113-
else ""
114-
in
115-
Markdown.divider
116-
^ (if supportsMarkdownLinks then Markdown.spacing else "")
117-
^ Markdown.codeBlock
118-
(decl
119-
|> Shared.declToString ~printNameAsIs:true
120-
(SharedTypes.pathIdentToString path))
121-
^ linkToTypeDefinitionStr ^ "\n")
122-
in
123-
typeString :: typeDefinitions |> String.concat "\n"
122+
typeString :: expandTypes ~file ~package ~supportsMarkdownLinks typ
123+
|> String.concat "\n"
124124

125125
(* Leverages autocomplete functionality to produce a hover for a position. This
126126
makes it (most often) work with unsaved content. *)
@@ -166,9 +166,14 @@ let getHoverViaCompletions ~debug ~path ~pos ~currentFile ~forHover
166166

167167
let newHover ~full:{file; package} ~supportsMarkdownLinks locItem =
168168
match locItem.locType with
169-
| TypeDefinition (name, decl, _stamp) ->
170-
let typeDef = Shared.declToString name decl in
171-
Some (Markdown.codeBlock typeDef)
169+
| TypeDefinition (name, decl, _stamp) -> (
170+
let typeDef = Markdown.codeBlock (Shared.declToString name decl) in
171+
match decl.type_manifest with
172+
| None -> Some typeDef
173+
| Some typ ->
174+
Some
175+
(typeDef :: expandTypes ~file ~package ~supportsMarkdownLinks typ
176+
|> String.concat "\n"))
172177
| LModule (Definition (stamp, _tip)) | LModule (LocalReference (stamp, _tip))
173178
-> (
174179
match Stamps.findModule file.stamps stamp with

analysis/tests/src/Hover.res

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,4 +262,7 @@ let coolVariant = CoolVariant
262262
// ^hov
263263

264264
module Arr = Belt.Array
265-
// ^hov
265+
// ^hov
266+
267+
type aliased = variant
268+
// ^hov

analysis/tests/src/expected/Hover.res.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,3 +264,6 @@ Path x
264264
Hover src/Hover.res 263:8
265265
{"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```"}}
266266

267+
Hover src/Hover.res 266:6
268+
{"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"}}
269+

0 commit comments

Comments
 (0)