Skip to content

Commit f985ab1

Browse files
authored
Show variant constructor's inline record types on hover (#7519)
* Show variant inline record type on hover * Show variant constructor type information below type on hover * Refactor Hover.newHover * Add analysis tests * Add CHANGELOG entry
1 parent dd1f753 commit f985ab1

File tree

4 files changed

+45
-33
lines changed

4 files changed

+45
-33
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
- Add `RegExp.flags`. https://github.com/rescript-lang/rescript/pull/7461
1818
- Add `Array.findLast`, `Array.findLastWithIndex`, `Array.findLastIndex`, `Array.findLastIndexWithIndex` and `Array.findLastIndexOpt`. https://github.com/rescript-lang/rescript/pull/7503
1919
- Add `options` argument to `Console.dir`. https://github.com/rescript-lang/rescript/pull/7504
20+
- Show variant constructor's inline record types on hover. https://github.com/rescript-lang/rescript/pull/7519
2021

2122
#### :bug: Bug fix
2223

analysis/src/Hover.ml

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,21 @@ let expandTypes ~file ~package ~supportsMarkdownLinks typ =
137137
`Default )
138138

139139
(* Produces a hover with relevant types expanded in the main type being hovered. *)
140-
let hoverWithExpandedTypes ~file ~package ~supportsMarkdownLinks typ =
141-
let typeString = Markdown.codeBlock (typ |> Shared.typeToString) in
140+
let hoverWithExpandedTypes ~file ~package ~supportsMarkdownLinks ?constructor
141+
typ =
142142
let expandedTypes, expansionType =
143143
expandTypes ~file ~package ~supportsMarkdownLinks typ
144144
in
145145
match expansionType with
146-
| `Default -> typeString :: expandedTypes |> String.concat "\n"
146+
| `Default ->
147+
let typeString = Shared.typeToString typ in
148+
let typeString =
149+
match constructor with
150+
| Some constructor ->
151+
typeString ^ "\n" ^ CompletionBackEnd.showConstructor constructor
152+
| None -> typeString
153+
in
154+
Markdown.codeBlock typeString :: expandedTypes |> String.concat "\n"
147155
| `InlineType -> expandedTypes |> String.concat "\n"
148156

149157
(* Leverages autocomplete functionality to produce a hover for a position. This
@@ -256,33 +264,22 @@ let newHover ~full:{file; package} ~supportsMarkdownLinks locItem =
256264
| Const_int64 _ -> "int64"
257265
| Const_bigint _ -> "bigint"))
258266
| Typed (_, t, locKind) ->
259-
let fromType ~docstring typ =
260-
( hoverWithExpandedTypes ~file ~package ~supportsMarkdownLinks typ,
261-
docstring )
267+
let fromType ?constructor typ =
268+
hoverWithExpandedTypes ~file ~package ~supportsMarkdownLinks ?constructor
269+
typ
262270
in
263271
let parts =
264272
match References.definedForLoc ~file ~package locKind with
265273
| None ->
266-
let typeString, docstring = t |> fromType ~docstring:[] in
267-
typeString :: docstring
274+
let typeString = t |> fromType in
275+
[typeString]
268276
| Some (docstring, res) -> (
269277
match res with
270-
| `Declared ->
271-
let typeString, docstring = t |> fromType ~docstring in
278+
| `Declared | `Field ->
279+
let typeString = t |> fromType in
272280
typeString :: docstring
273-
| `Constructor {cname = {txt}; args; docstring} ->
274-
let typeString, docstring = t |> fromType ~docstring in
275-
let argsString =
276-
match args with
277-
| InlineRecord _ | Args [] -> ""
278-
| Args args ->
279-
args
280-
|> List.map (fun (t, _) -> Shared.typeToString t)
281-
|> String.concat ", " |> Printf.sprintf "(%s)"
282-
in
283-
typeString :: Markdown.codeBlock (txt ^ argsString) :: docstring
284-
| `Field ->
285-
let typeString, docstring = t |> fromType ~docstring in
286-
typeString :: docstring)
281+
| `Constructor constructor ->
282+
let typeString = t |> fromType ~constructor in
283+
typeString :: constructor.docstring)
287284
in
288285
Some (String.concat Markdown.divider parts)

tests/analysis_tests/tests/src/Hover.res

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,14 @@ type variant = | /** Cool variant! */ CoolVariant | /** Other cool variant */ Ot
254254
let coolVariant = CoolVariant
255255
// ^hov
256256

257+
type payloadVariants = InlineRecord({field1: int, field2: bool}) | Args(int, bool)
258+
259+
let payloadVariant = InlineRecord({field1: 1, field2: true})
260+
// ^hov
261+
262+
let payloadVariant2 = Args(1, true)
263+
// ^hov
264+
257265
// Hover on unsaved
258266
// let fff = "hello"; fff
259267
// ^hov

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

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -301,12 +301,18 @@ Hover src/Hover.res 248:19
301301
{"contents": {"kind": "markdown", "value": "```rescript\nbool\n```\n---\n Mighty fine field here. "}}
302302

303303
Hover src/Hover.res 253:20
304-
{"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! "}}
304+
{"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! "}}
305305

306-
Hover src/Hover.res 257:23
306+
Hover src/Hover.res 258:22
307+
{"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"}}
308+
309+
Hover src/Hover.res 261:23
310+
{"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"}}
311+
312+
Hover src/Hover.res 265:23
307313
Nothing at that position. Now trying to use completion.
308-
posCursor:[257:23] posNoWhite:[257:22] Found expr:[257:22->257:25]
309-
Pexp_ident fff:[257:22->257:25]
314+
posCursor:[265:23] posNoWhite:[265:22] Found expr:[265:22->265:25]
315+
Pexp_ident fff:[265:22->265:25]
310316
Completable: Cpath Value[fff]
311317
Package opens Stdlib.place holder Pervasives.JsxModules.place holder
312318
Resolved opens 1 Stdlib
@@ -317,10 +323,10 @@ Resolved opens 1 Stdlib
317323
ContextPath string
318324
{"contents": {"kind": "markdown", "value": "```rescript\nstring\n```"}}
319325

320-
Hover src/Hover.res 260:33
326+
Hover src/Hover.res 268:33
321327
Nothing at that position. Now trying to use completion.
322-
posCursor:[260:33] posNoWhite:[260:32] Found expr:[260:31->260:40]
323-
Pexp_ident someField:[260:31->260:40]
328+
posCursor:[268:33] posNoWhite:[268:32] Found expr:[268:31->268:40]
329+
Pexp_ident someField:[268:31->268:40]
324330
Completable: Cpath Value[someField]
325331
Package opens Stdlib.place holder Pervasives.JsxModules.place holder
326332
Resolved opens 1 Stdlib
@@ -333,9 +339,9 @@ ContextPath Value[x]
333339
Path x
334340
{"contents": {"kind": "markdown", "value": "```rescript\nbool\n```"}}
335341

336-
Hover src/Hover.res 263:8
342+
Hover src/Hover.res 271:8
337343
{"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```"}}
338344

339-
Hover src/Hover.res 266:6
345+
Hover src/Hover.res 274:6
340346
{"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"}}
341347

0 commit comments

Comments
 (0)