@@ -87,6 +87,72 @@ let findRelevantTypesFromType ~file ~package typ =
87
87
let constructors = Shared. findTypeConstructors typesToSearch in
88
88
constructors |> List. filter_map (fromConstructorPath ~env: envToSearch)
89
89
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 ~print NameAsIs: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 ~pos Cursor:pos
122
+ ~current File ~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 ~for Hover
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
+ ~supports MarkdownLinks typ
152
+ in
153
+ Some (Protocol. stringifyHover typeString)
154
+ | None -> None ))))
155
+
90
156
let newHover ~full :{file; package} ~supportsMarkdownLinks locItem =
91
157
match locItem.locType with
92
158
| TypeDefinition (name , decl , _stamp ) ->
@@ -150,24 +216,8 @@ let newHover ~full:{file; package} ~supportsMarkdownLinks locItem =
150
216
| Const_nativeint _ -> " int" ))
151
217
| Typed (_ , t , locKind ) ->
152
218
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 ~print NameAsIs:true
167
- (SharedTypes. pathIdentToString path))
168
- ^ linkToTypeDefinitionStr ^ " \n " ^ Markdown. divider)
169
- in
170
- (typeString :: typeDefinitions |> String. concat " \n " , docstring)
219
+ hoverWithExpandedTypes ~docstring ~file ~package ~supports MarkdownLinks
220
+ typ
171
221
in
172
222
let parts =
173
223
match References. definedForLoc ~file ~package locKind with
0 commit comments