Skip to content

Commit d9091b9

Browse files
committed
make hovers a bit nicer
1 parent a4ef0aa commit d9091b9

File tree

7 files changed

+88
-25
lines changed

7 files changed

+88
-25
lines changed

analysis/src/Hover.ml

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -101,26 +101,48 @@ let findRelevantTypesFromType ~file ~package typ =
101101
constructors |> List.filter_map (fromConstructorPath ~env:envToSearch)
102102

103103
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")
104+
match findRelevantTypesFromType typ ~file ~package with
105+
| {decl; path} :: _
106+
when Res_parsetree_viewer.has_inline_record_definition_attribute
107+
decl.type_attributes ->
108+
( [
109+
Markdown.codeBlock
110+
(decl
111+
|> Shared.declToString ~printNameAsIs:true
112+
(SharedTypes.pathIdentToString path));
113+
],
114+
`InlineType )
115+
| all ->
116+
( all
117+
|> List.map (fun {decl; env; loc; path} ->
118+
let linkToTypeDefinitionStr =
119+
if
120+
supportsMarkdownLinks
121+
&& not
122+
(Res_parsetree_viewer
123+
.has_inline_record_definition_attribute
124+
decl.type_attributes)
125+
then Markdown.goToDefinitionText ~env ~pos:loc.Warnings.loc_start
126+
else ""
127+
in
128+
Markdown.divider
129+
^ (if supportsMarkdownLinks then Markdown.spacing else "")
130+
^ Markdown.codeBlock
131+
(decl
132+
|> Shared.declToString ~printNameAsIs:true
133+
(SharedTypes.pathIdentToString path))
134+
^ linkToTypeDefinitionStr ^ "\n"),
135+
`Default )
118136

119137
(* Produces a hover with relevant types expanded in the main type being hovered. *)
120138
let hoverWithExpandedTypes ~file ~package ~supportsMarkdownLinks typ =
121139
let typeString = Markdown.codeBlock (typ |> Shared.typeToString) in
122-
typeString :: expandTypes ~file ~package ~supportsMarkdownLinks typ
123-
|> String.concat "\n"
140+
let expandedTypes, expansionType =
141+
expandTypes ~file ~package ~supportsMarkdownLinks typ
142+
in
143+
match expansionType with
144+
| `Default -> typeString :: expandedTypes |> String.concat "\n"
145+
| `InlineType -> expandedTypes |> String.concat "\n"
124146

125147
(* Leverages autocomplete functionality to produce a hover for a position. This
126148
makes it (most often) work with unsaved content. *)
@@ -171,10 +193,13 @@ let newHover ~full:{file; package} ~supportsMarkdownLinks locItem =
171193
let typeDef = Markdown.codeBlock (Shared.declToString name decl) in
172194
match decl.type_manifest with
173195
| None -> Some typeDef
174-
| Some typ ->
175-
Some
176-
(typeDef :: expandTypes ~file ~package ~supportsMarkdownLinks typ
177-
|> String.concat "\n"))
196+
| Some typ -> (
197+
let expandedTypes, expansionType =
198+
expandTypes ~file ~package ~supportsMarkdownLinks typ
199+
in
200+
match expansionType with
201+
| `Default -> Some (typeDef :: expandedTypes |> String.concat "\n")
202+
| `InlineType -> Some (expandedTypes |> String.concat "\n")))
178203
| LModule (Definition (stamp, _tip)) | LModule (LocalReference (stamp, _tip))
179204
-> (
180205
match Stamps.findModule file.stamps stamp with

compiler/syntax/src/res_parsetree_viewer.ml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,20 @@ let has_await_attribute attrs =
7272
| _ -> false)
7373
attrs
7474

75+
let has_inline_record_definition_attribute attrs =
76+
List.exists
77+
(function
78+
| {Location.txt = "res.inlineRecordDefinition"}, _ -> true
79+
| _ -> false)
80+
attrs
81+
82+
let has_inline_record_reference_attribute attrs =
83+
List.exists
84+
(function
85+
| {Location.txt = "res.inlineRecordReference"}, _ -> true
86+
| _ -> false)
87+
attrs
88+
7589
let has_res_pat_variant_spread_attribute attrs =
7690
List.exists
7791
(function

compiler/syntax/src/res_parsetree_viewer.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ val functor_type :
1515
* Parsetree.module_type
1616

1717
val has_await_attribute : Parsetree.attributes -> bool
18+
val has_inline_record_definition_attribute : Parsetree.attributes -> bool
19+
val has_inline_record_reference_attribute : Parsetree.attributes -> bool
1820
val has_res_pat_variant_spread_attribute : Parsetree.attributes -> bool
1921
val has_dict_pattern_attribute : Parsetree.attributes -> bool
2022

tests/analysis_tests/tests-reanalyze/termination/package-lock.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/analysis_tests/tests/src/NestedRecordsHover.res

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,18 @@ let options = {
1010
// ^hov
1111
extra: {
1212
name: "test",
13+
//^hov
1314
superExtra: {
1415
age: 2222,
16+
//^hov
1517
},
16-
otherExtra: Some({test: true, anotherInlined: {record: true}}),
18+
otherExtra: Some({
19+
test: true,
20+
// ^hov
21+
anotherInlined: {
22+
record: true,
23+
// ^hov
24+
},
25+
}),
1726
},
1827
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ ContextPath Value[options]
99
Path options
1010
Package opens Stdlib.place holder Pervasives.JsxModules.place holder
1111
Resolved opens 1 Stdlib
12-
{"contents": {"kind": "markdown", "value": "```rescript\noptions\n```\n\n---\n\n```\n \n```\n```rescript\ntype options = {\n extra: {name: string, superExtra: {age: int}},\n}\n```\nGo to: [Type definition](command:rescript-vscode.go_to_location?%5B%22NestedRecords.res%22%2C1%2C0%5D)\n\n\n---\n\n```\n \n```\n```rescript\ntype options.extra = {name: string, superExtra: {age: int}}\n```\nGo to: [Type definition](command:rescript-vscode.go_to_location?%5B%22NestedRecords.res%22%2C2%2C9%5D)\n"}}
12+
{"contents": {"kind": "markdown", "value": "```rescript\noptions\n```\n\n---\n\n```\n \n```\n```rescript\ntype options = {\n extra: {name: string, superExtra: {age: int}},\n}\n```\nGo to: [Type definition](command:rescript-vscode.go_to_location?%5B%22NestedRecords.res%22%2C1%2C0%5D)\n\n\n---\n\n```\n \n```\n```rescript\ntype options.extra = {name: string, superExtra: {age: int}}\n```\n"}}
1313

1414
Hover src/NestedRecords.res 20:13
1515
Nothing at that position. Now trying to use completion.
@@ -28,7 +28,7 @@ CPPipe pathFromEnv: found:true
2828
Path NestedRecords.extra
2929
Package opens Stdlib.place holder Pervasives.JsxModules.place holder
3030
Resolved opens 1 Stdlib
31-
{"contents": {"kind": "markdown", "value": "```rescript\n\\\"options.extra\"\n```\n\n---\n\n```\n \n```\n```rescript\ntype options.extra = {name: string, superExtra: {age: int}}\n```\nGo to: [Type definition](command:rescript-vscode.go_to_location?%5B%22NestedRecords.res%22%2C2%2C9%5D)\n\n\n---\n\n```\n \n```\n```rescript\ntype options.extra.superExtra = {age: int}\n```\nGo to: [Type definition](command:rescript-vscode.go_to_location?%5B%22NestedRecords.res%22%2C4%2C16%5D)\n"}}
31+
{"contents": {"kind": "markdown", "value": "```rescript\ntype options.extra = {name: string, superExtra: {age: int}}\n```"}}
3232

3333
Hover src/NestedRecords.res 23:26
3434
Nothing at that position. Now trying to use completion.
@@ -59,7 +59,7 @@ CPPipe pathFromEnv: found:true
5959
Path NestedRecords.superExtra
6060
Package opens Stdlib.place holder Pervasives.JsxModules.place holder
6161
Resolved opens 1 Stdlib
62-
{"contents": {"kind": "markdown", "value": "```rescript\n\\\"options.extra.superExtra\"\n```\n\n---\n\n```\n \n```\n```rescript\ntype options.extra.superExtra = {age: int}\n```\nGo to: [Type definition](command:rescript-vscode.go_to_location?%5B%22NestedRecords.res%22%2C4%2C16%5D)\n"}}
62+
{"contents": {"kind": "markdown", "value": "```rescript\ntype options.extra.superExtra = {age: int}\n```"}}
6363

6464
Hover src/NestedRecords.res 26:29
6565
Nothing at that position. Now trying to use completion.
Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
11
Hover src/NestedRecordsHover.res 8:7
2-
{"contents": {"kind": "markdown", "value": "```rescript\noptions\n```\n\n---\n\n```\n \n```\n```rescript\ntype options = {\n extra?: {\n name: string,\n superExtra?: {age: int},\n otherExtra: option<\n {\n test: bool,\n anotherInlined: {record: bool},\n },\n >,\n },\n}\n```\nGo to: [Type definition](command:rescript-vscode.go_to_location?%5B%22NestedRecordsHover.res%22%2C0%2C0%5D)\n\n\n---\n\n```\n \n```\n```rescript\ntype options.extra = {\n name: string,\n superExtra?: {age: int},\n otherExtra: option<\n {\n test: bool,\n anotherInlined: {record: bool},\n },\n >,\n}\n```\nGo to: [Type definition](command:rescript-vscode.go_to_location?%5B%22NestedRecordsHover.res%22%2C1%2C10%5D)\n"}}
2+
{"contents": {"kind": "markdown", "value": "```rescript\noptions\n```\n\n---\n\n```\n \n```\n```rescript\ntype options = {\n extra?: {\n name: string,\n superExtra?: {age: int},\n otherExtra: option<\n {\n test: bool,\n anotherInlined: {record: bool},\n },\n >,\n },\n}\n```\nGo to: [Type definition](command:rescript-vscode.go_to_location?%5B%22NestedRecordsHover.res%22%2C0%2C0%5D)\n\n\n---\n\n```\n \n```\n```rescript\ntype options.extra = {\n name: string,\n superExtra?: {age: int},\n otherExtra: option<\n {\n test: bool,\n anotherInlined: {record: bool},\n },\n >,\n}\n```\n"}}
3+
4+
Hover src/NestedRecordsHover.res 11:6
5+
{"contents": {"kind": "markdown", "value": "```rescript\ntype options.extra = {\n name: string,\n superExtra?: {age: int},\n otherExtra: option<\n {\n test: bool,\n anotherInlined: {record: bool},\n },\n >,\n}\n```"}}
6+
7+
Hover src/NestedRecordsHover.res 14:8
8+
{"contents": {"kind": "markdown", "value": "```rescript\ntype options.extra.superExtra = {age: int}\n```"}}
9+
10+
Hover src/NestedRecordsHover.res 18:9
11+
{"contents": {"kind": "markdown", "value": "```rescript\ntype options.extra.otherExtra = {\n test: bool,\n anotherInlined: {record: bool},\n}\n```"}}
12+
13+
Hover src/NestedRecordsHover.res 21:11
14+
{"contents": {"kind": "markdown", "value": "```rescript\ntype options.extra.otherExtra.anotherInlined = {\n record: bool,\n}\n```"}}
315

0 commit comments

Comments
 (0)