Skip to content

Commit 825afc2

Browse files
committed
refactor hover via completions to also expand types like the hover via loc does
1 parent 2f3a5fd commit 825afc2

File tree

3 files changed

+75
-35
lines changed

3 files changed

+75
-35
lines changed

analysis/src/Commands.ml

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,22 +46,12 @@ let hover ~path ~pos ~currentFile ~debug ~supportsMarkdownLinks =
4646
if debug then
4747
Printf.printf
4848
"Nothing at that position. Now trying to use completion.\n";
49-
let completions =
50-
getCompletions ~debug ~path ~pos ~currentFile ~forHover:true
51-
in
52-
match completions with
53-
| {kind = Label typString; docstring} :: _ ->
54-
let parts =
55-
(if typString = "" then [] else [Markdown.codeBlock typString])
56-
@ docstring
57-
in
58-
Protocol.stringifyHover (String.concat "\n\n" parts)
59-
| _ -> (
60-
match CompletionBackEnd.completionsGetTypeEnv completions with
61-
| Some (typ, _env) ->
62-
Protocol.stringifyHover
63-
(Markdown.codeBlock (Shared.typeToString typ))
64-
| None -> Protocol.null))
49+
match
50+
Hover.getHoverViaCompletions ~debug ~path ~pos ~currentFile
51+
~forHover:true ~supportsMarkdownLinks
52+
with
53+
| None -> Protocol.null
54+
| Some hover -> hover)
6555
| Some locItem -> (
6656
let isModule =
6757
match locItem.locType with

analysis/src/Hover.ml

Lines changed: 68 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,72 @@ let findRelevantTypesFromType ~file ~package typ =
8787
let constructors = Shared.findTypeConstructors typesToSearch in
8888
constructors |> List.filter_map (fromConstructorPath ~env:envToSearch)
8989

90+
(* Produces a hover with relevant types expanded in the main type being hovered. *)
91+
let hoverWithExpandedTypes ~docstring ~file ~package ~supportsMarkdownLinks typ
92+
=
93+
let typeString = Markdown.codeBlock (typ |> Shared.typeToString) in
94+
let types = findRelevantTypesFromType typ ~file ~package in
95+
let typeDefinitions =
96+
types
97+
|> List.map (fun {decl; env; loc; path} ->
98+
let linkToTypeDefinitionStr =
99+
if supportsMarkdownLinks then
100+
Markdown.goToDefinitionText ~env ~pos:loc.Warnings.loc_start
101+
else ""
102+
in
103+
"\n" ^ Markdown.spacing
104+
^ Markdown.codeBlock
105+
(decl
106+
|> Shared.declToString ~printNameAsIs:true
107+
(SharedTypes.pathIdentToString path))
108+
^ linkToTypeDefinitionStr ^ "\n" ^ Markdown.divider)
109+
in
110+
(typeString :: typeDefinitions |> String.concat "\n", docstring)
111+
112+
(* Leverages autocomplete functionality to produce a hover for a position. This
113+
makes it (most often) work with unsaved content. *)
114+
let getHoverViaCompletions ~debug ~path ~pos ~currentFile ~forHover
115+
~supportsMarkdownLinks =
116+
let textOpt = Files.readFile currentFile in
117+
match textOpt with
118+
| None | Some "" -> None
119+
| Some text -> (
120+
match
121+
CompletionFrontEnd.completionWithParser ~debug ~path ~posCursor:pos
122+
~currentFile ~text
123+
with
124+
| None -> None
125+
| Some (completable, scope) -> (
126+
if debug then
127+
Printf.printf "Completable: %s\n"
128+
(SharedTypes.Completable.toString completable);
129+
(* Only perform expensive ast operations if there are completables *)
130+
match Cmt.fullFromPath ~path with
131+
| None -> None
132+
| Some {file; package} -> (
133+
let env = SharedTypes.QueryEnv.fromFile file in
134+
let completions =
135+
completable
136+
|> CompletionBackEnd.processCompletable ~debug ~package ~pos ~scope
137+
~env ~forHover
138+
in
139+
match completions with
140+
| {kind = Label typString; docstring} :: _ ->
141+
let parts =
142+
(if typString = "" then [] else [Markdown.codeBlock typString])
143+
@ docstring
144+
in
145+
Some (Protocol.stringifyHover (String.concat "\n\n" parts))
146+
| _ -> (
147+
match CompletionBackEnd.completionsGetTypeEnv completions with
148+
| Some (typ, _env) ->
149+
let typeString, _docstring =
150+
hoverWithExpandedTypes ~docstring:"" ~file ~package
151+
~supportsMarkdownLinks typ
152+
in
153+
Some (Protocol.stringifyHover typeString)
154+
| None -> None))))
155+
90156
let newHover ~full:{file; package} ~supportsMarkdownLinks locItem =
91157
match locItem.locType with
92158
| TypeDefinition (name, decl, _stamp) ->
@@ -150,24 +216,8 @@ let newHover ~full:{file; package} ~supportsMarkdownLinks locItem =
150216
| Const_nativeint _ -> "int"))
151217
| Typed (_, t, locKind) ->
152218
let fromType ~docstring typ =
153-
let typeString = Markdown.codeBlock (typ |> Shared.typeToString) in
154-
let types = findRelevantTypesFromType typ ~file ~package in
155-
let typeDefinitions =
156-
types
157-
|> List.map (fun {decl; env; loc; path} ->
158-
let linkToTypeDefinitionStr =
159-
if supportsMarkdownLinks then
160-
Markdown.goToDefinitionText ~env ~pos:loc.Warnings.loc_start
161-
else ""
162-
in
163-
"\n" ^ Markdown.spacing
164-
^ Markdown.codeBlock
165-
(decl
166-
|> Shared.declToString ~printNameAsIs:true
167-
(SharedTypes.pathIdentToString path))
168-
^ linkToTypeDefinitionStr ^ "\n" ^ Markdown.divider)
169-
in
170-
(typeString :: typeDefinitions |> String.concat "\n", docstring)
219+
hoverWithExpandedTypes ~docstring ~file ~package ~supportsMarkdownLinks
220+
typ
171221
in
172222
let parts =
173223
match References.definedForLoc ~file ~package locKind with

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,5 +169,5 @@ Nothing at that position. Now trying to use completion.
169169
posCursor:[210:13] posNoWhite:[210:12] Found expr:[210:11->210:14]
170170
Pexp_ident usr:[210:11->210:14]
171171
Completable: Cpath Value[usr]
172-
{"contents": "```rescript\nuseR\n```"}
172+
{"contents": "```rescript\nuseR\n```\n\n\n```\n \n```\n```rescript\ntype useR = {x: int, y: list<option<r<float>>>}\n```\nGo to: [Type definition](command:rescript-vscode.go_to_location?%5B%22Hover.res%22%2C200%2C0%5D)\n\n---\n\n\n\n```\n \n```\n```rescript\ntype r<'a> = {i: 'a, f: float}\n```\nGo to: [Type definition](command:rescript-vscode.go_to_location?%5B%22Hover.res%22%2C101%2C0%5D)\n\n---\n"}
173173

0 commit comments

Comments
 (0)