Skip to content

Commit fb30b2d

Browse files
committed
Show docstrings before type expansions
1 parent 4276895 commit fb30b2d

File tree

3 files changed

+25
-24
lines changed

3 files changed

+25
-24
lines changed

analysis/src/Hover.ml

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,8 @@ let expandTypes ~file ~package ~supportsMarkdownLinks typ =
150150
`Default )
151151

152152
(* Produces a hover with relevant types expanded in the main type being hovered. *)
153-
let hoverWithExpandedTypes ~file ~package ~supportsMarkdownLinks ?constructor
154-
typ =
153+
let hoverWithExpandedTypes ~file ~package ~supportsMarkdownLinks ?docstring
154+
?constructor typ =
155155
let expandedTypes, expansionType =
156156
expandTypes ~file ~package ~supportsMarkdownLinks typ
157157
in
@@ -164,7 +164,15 @@ let hoverWithExpandedTypes ~file ~package ~supportsMarkdownLinks ?constructor
164164
typeString ^ "\n" ^ CompletionBackEnd.showConstructor constructor
165165
| None -> typeString
166166
in
167-
Markdown.codeBlock typeString :: expandedTypes |> String.concat "\n"
167+
let typeString =
168+
match docstring with
169+
| Some [] | None -> Markdown.codeBlock typeString
170+
| Some docstring ->
171+
Markdown.codeBlock typeString
172+
^ Markdown.divider
173+
^ (docstring |> String.concat "\n")
174+
in
175+
typeString :: expandedTypes |> String.concat "\n"
168176
| `InlineType -> expandedTypes |> String.concat "\n"
169177

170178
(* Leverages autocomplete functionality to produce a hover for a position. This
@@ -191,10 +199,10 @@ let getHoverViaCompletions ~debug ~path ~pos ~currentFile ~forHover
191199
with
192200
| Some (typ, _env) ->
193201
let typeString =
194-
hoverWithExpandedTypes ~file ~package ~supportsMarkdownLinks typ
202+
hoverWithExpandedTypes ~file ~package ~docstring
203+
~supportsMarkdownLinks typ
195204
in
196-
let parts = docstring @ [typeString] in
197-
Some (Protocol.stringifyHover (String.concat "\n\n" parts))
205+
Some (Protocol.stringifyHover typeString)
198206
| None -> None)
199207
| {env} :: _ -> (
200208
let opens = CompletionBackEnd.getOpens ~debug ~rawOpens ~package ~env in
@@ -277,22 +285,15 @@ let newHover ~full:{file; package} ~supportsMarkdownLinks locItem =
277285
| Const_int64 _ -> "int64"
278286
| Const_bigint _ -> "bigint"))
279287
| Typed (_, t, locKind) ->
280-
let fromType ?constructor typ =
281-
hoverWithExpandedTypes ~file ~package ~supportsMarkdownLinks ?constructor
282-
typ
288+
let fromType ?docstring ?constructor typ =
289+
hoverWithExpandedTypes ~file ~package ~supportsMarkdownLinks ?docstring
290+
?constructor typ
283291
in
284-
let parts =
285-
match References.definedForLoc ~file ~package locKind with
286-
| None ->
287-
let typeString = t |> fromType in
288-
[typeString]
292+
Some
293+
(match References.definedForLoc ~file ~package locKind with
294+
| None -> t |> fromType
289295
| Some (docstring, res) -> (
290296
match res with
291-
| `Declared | `Field ->
292-
let typeString = t |> fromType in
293-
typeString :: docstring
297+
| `Declared | `Field -> t |> fromType ~docstring
294298
| `Constructor constructor ->
295-
let typeString = t |> fromType ~constructor in
296-
typeString :: constructor.docstring)
297-
in
298-
Some (String.concat Markdown.divider parts)
299+
t |> fromType ~docstring:constructor.docstring ~constructor))

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,13 +295,13 @@ Path Hover.someField
295295
Path someField
296296
Package opens Stdlib.place holder Pervasives.JsxModules.place holder
297297
Resolved opens 1 Stdlib
298-
{"contents": {"kind": "markdown", "value": " Mighty fine field here. \n\n```rescript\nbool\n```"}}
298+
{"contents": {"kind": "markdown", "value": "```rescript\nbool\n```\n---\n Mighty fine field here. "}}
299299

300300
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\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! "}}
304+
{"contents": {"kind": "markdown", "value": "```rescript\nvariant\nCoolVariant\n```\n---\n Cool variant! \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"}}
305305

306306
Hover src/Hover.res 258:22
307307
{"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"}}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Path M4.make
1717
}]
1818

1919
Hover src/JsxV4.res 14:9
20-
{"contents": {"kind": "markdown", "value": "```rescript\nReact.component<M4.props<string, string, string>>\n```\n\n---\n\n```\n \n```\n```rescript\ntype React.component<'props> = Jsx.component<'props>\n```\nGo to: [Type definition](command:rescript-vscode.go_to_location?%5B%22React.res%22%2C12%2C0%5D)\n\n\n---\n\n```\n \n```\n```rescript\ntype M4.props<'first, 'fun, 'second> = {\n first: 'first,\n fun?: 'fun,\n second?: 'second,\n}\n```\nGo to: [Type definition](command:rescript-vscode.go_to_location?%5B%22JsxV4.res%22%2C3%2C2%5D)\n\n---\n Doc Comment For M4 "}}
20+
{"contents": {"kind": "markdown", "value": "```rescript\nReact.component<M4.props<string, string, string>>\n```\n---\n Doc Comment For M4 \n\n---\n\n```\n \n```\n```rescript\ntype React.component<'props> = Jsx.component<'props>\n```\nGo to: [Type definition](command:rescript-vscode.go_to_location?%5B%22React.res%22%2C12%2C0%5D)\n\n\n---\n\n```\n \n```\n```rescript\ntype M4.props<'first, 'fun, 'second> = {\n first: 'first,\n fun?: 'fun,\n second?: 'second,\n}\n```\nGo to: [Type definition](command:rescript-vscode.go_to_location?%5B%22JsxV4.res%22%2C3%2C2%5D)\n"}}
2121

2222
Create Interface src/JsxV4.res
2323
module M4: {

0 commit comments

Comments
 (0)