@@ -79,7 +79,7 @@ let completionForExporteds iterExported getDeclared ~prefix ~exact ~env
7979 res :=
8080 {
8181 (Completion. create declared.name.txt ~env
82- ~kind: (transformContents declared.item ))
82+ ~kind: (transformContents declared))
8383 with
8484 deprecated = declared.deprecated;
8585 docstring = declared.docstring;
@@ -90,18 +90,20 @@ let completionForExporteds iterExported getDeclared ~prefix ~exact ~env
9090
9191let completionForExportedModules ~env ~prefix ~exact ~namesUsed =
9292 completionForExporteds (Exported. iter env.QueryEnv. exported Exported. Module )
93- (Stamps. findModule env.file.stamps) ~prefix ~exact ~env ~names Used (fun m ->
94- Completion. Module m)
93+ (Stamps. findModule env.file.stamps) ~prefix ~exact ~env ~names Used
94+ (fun declared ->
95+ Completion. Module
96+ {docstring = declared.docstring; module_ = declared.item})
9597
9698let completionForExportedValues ~env ~prefix ~exact ~namesUsed =
9799 completionForExporteds (Exported. iter env.QueryEnv. exported Exported. Value )
98- (Stamps. findValue env.file.stamps) ~prefix ~exact ~env ~names Used ( fun v ->
99- Completion. Value v )
100+ (Stamps. findValue env.file.stamps) ~prefix ~exact ~env ~names Used
101+ ( fun declared -> Completion. Value declared.item )
100102
101103let completionForExportedTypes ~env ~prefix ~exact ~namesUsed =
102104 completionForExporteds (Exported. iter env.QueryEnv. exported Exported. Type )
103- (Stamps. findType env.file.stamps) ~prefix ~exact ~env ~names Used ( fun t ->
104- Completion. Type t )
105+ (Stamps. findType env.file.stamps) ~prefix ~exact ~env ~names Used
106+ ( fun declared -> Completion. Type declared.item )
105107
106108let completionsForExportedConstructors ~(env : QueryEnv.t ) ~prefix ~exact
107109 ~namesUsed =
@@ -224,39 +226,109 @@ let getEnvWithOpens ~scope ~(env : QueryEnv.t) ~package
224226 | None -> None
225227 | Some env -> ResolvePath. resolvePath ~env ~package ~path ))
226228
229+ let rec expandTypeExpr ~env ~package typeExpr =
230+ match typeExpr |> Shared. digConstructor with
231+ | Some path -> (
232+ match References. digConstructor ~env ~package path with
233+ | None -> None
234+ | Some (env , {item = {decl = {type_manifest = Some t } } } ) ->
235+ expandTypeExpr ~env ~package t
236+ | Some (_ , {docstring; item} ) -> Some (docstring, item))
237+ | None -> None
238+
239+ let kindToDocumentation ~env ~full ~currentDocstring name
240+ (kind : Completion.kind ) =
241+ let docsFromKind =
242+ match kind with
243+ | ObjLabel _ | Label _ | FileModule _ | Snippet _ | FollowContextPath _ ->
244+ []
245+ | Module {docstring} -> docstring
246+ | Type {decl; name} ->
247+ [decl |> Shared. declToString name |> Markdown. codeBlock]
248+ | Value typ -> (
249+ match expandTypeExpr ~env ~package: full.package typ with
250+ | None -> []
251+ | Some (docstrings , {decl; name; kind} ) ->
252+ docstrings
253+ @ [
254+ (match kind with
255+ | Record _ | Tuple _ | Variant _ ->
256+ Markdown. codeBlock (Shared. declToString name decl)
257+ | _ -> " " );
258+ ])
259+ | Field ({typ; optional; docstring} , s ) ->
260+ (* Handle optional fields. Checking for "?" is because sometimes optional
261+ fields are prefixed with "?" when completing, and at that point we don't
262+ need to _also_ add a "?" after the field name, as that looks weird. *)
263+ docstring
264+ @ [
265+ Markdown. codeBlock
266+ (if optional && Utils. startsWith name " ?" = false then
267+ name ^ " ?: "
268+ ^ (typ |> Utils. unwrapIfOption |> Shared. typeToString)
269+ else name ^ " : " ^ (typ |> Shared. typeToString));
270+ Markdown. codeBlock s;
271+ ]
272+ | Constructor (c , s ) ->
273+ [Markdown. codeBlock (showConstructor c); Markdown. codeBlock s]
274+ | PolyvariantConstructor ({displayName; args} , s ) ->
275+ [
276+ Markdown. codeBlock
277+ (" #" ^ displayName
278+ ^
279+ match args with
280+ | [] -> " "
281+ | typeExprs ->
282+ " ("
283+ ^ (typeExprs
284+ |> List. map (fun typeExpr -> typeExpr |> Shared. typeToString)
285+ |> String. concat " , " )
286+ ^ " )" );
287+ Markdown. codeBlock s;
288+ ]
289+ | ExtractedType (extractedType , _ ) ->
290+ [Markdown. codeBlock (TypeUtils. extractedTypeToString extractedType)]
291+ in
292+ currentDocstring @ docsFromKind
293+ |> List. filter (fun s -> s <> " " )
294+ |> String. concat " \n\n "
295+
227296let kindToDetail name (kind : Completion.kind ) =
228297 match kind with
229- | Type {decl } -> decl |> Shared. declToString name
298+ | Type {name } -> " type " ^ name
230299 | Value typ -> typ |> Shared. typeToString
231300 | ObjLabel typ -> typ |> Shared. typeToString
232301 | Label typString -> typString
233- | Module _ -> " module"
234- | FileModule _ -> " file module"
235- | Field ({typ; optional} , s ) ->
302+ | Module _ -> " module " ^ name
303+ | FileModule f -> " module " ^ f
304+ | Field ({typ; optional} , _ ) ->
236305 (* Handle optional fields. Checking for "?" is because sometimes optional
237306 fields are prefixed with "?" when completing, and at that point we don't
238307 need to _also_ add a "?" after the field name, as that looks weird. *)
239308 if optional && Utils. startsWith name " ?" = false then
240- name ^ " ?: "
241- ^ (typ |> Utils. unwrapIfOption |> Shared. typeToString)
242- ^ " \n\n " ^ s
243- else name ^ " : " ^ (typ |> Shared. typeToString) ^ " \n\n " ^ s
244- | Constructor (c , s ) -> showConstructor c ^ " \n\n " ^ s
245- | PolyvariantConstructor ({displayName; args} , s ) ->
309+ typ |> Utils. unwrapIfOption |> Shared. typeToString
310+ else typ |> Shared. typeToString
311+ | Constructor (c , _ ) -> showConstructor c
312+ | PolyvariantConstructor ({displayName; args} , _ ) -> (
246313 " #" ^ displayName
247- ^ ( match args with
248- | [] -> " "
249- | typeExprs ->
250- " ( "
251- ^ (typeExprs
252- |> List. map ( fun typeExpr -> typeExpr |> Shared. typeToString)
253- |> String. concat " , " )
254- ^ " ) " )
255- ^ " \n\n " ^ s
314+ ^
315+ match args with
316+ | [] -> " "
317+ | typeExprs ->
318+ " ( "
319+ ^ (typeExprs
320+ |> List. map ( fun typeExpr -> typeExpr |> Shared. typeToString )
321+ |> String. concat " , " )
322+ ^ " ) " )
256323 | Snippet s -> s
257324 | FollowContextPath _ -> " "
258325 | ExtractedType (extractedType , _ ) ->
259- TypeUtils. extractedTypeToString extractedType
326+ TypeUtils. extractedTypeToString ~name Only:true extractedType
327+
328+ let kindToData filePath (kind : Completion.kind ) =
329+ match kind with
330+ | FileModule f -> Some [(" modulePath" , f); (" filePath" , filePath)]
331+ | _ -> None
260332
261333let findAllCompletions ~(env : QueryEnv.t ) ~prefix ~exact ~namesUsed
262334 ~(completionContext : Completable.completionContext ) =
@@ -366,7 +438,9 @@ let processLocalModule name loc ~prefix ~exact ~env
366438 localTables.resultRev < -
367439 {
368440 (Completion. create declared.name.txt ~env
369- ~kind: (Module declared.item))
441+ ~kind:
442+ (Module
443+ {docstring = declared.docstring; module_ = declared.item}))
370444 with
371445 deprecated = declared.deprecated;
372446 docstring = declared.docstring;
@@ -579,7 +653,7 @@ let rec digToRecordFieldsForCompletion ~debug ~package ~opens ~full ~pos ~env
579653 | {kind = Type {kind = Record fields } } :: _ -> Some fields
580654 | _ -> None
581655
582- let mkItem ~ name ~kind ~detail ~deprecated ~docstring =
656+ let mkItem ? data name ~kind ~detail ~deprecated ~docstring =
583657 let docContent =
584658 (match deprecated with
585659 | None -> " "
@@ -607,6 +681,7 @@ let mkItem ~name ~kind ~detail ~deprecated ~docstring =
607681 insertText = None ;
608682 insertTextFormat = None ;
609683 filterText = None ;
684+ data;
610685 }
611686
612687let completionToItem
@@ -620,16 +695,23 @@ let completionToItem
620695 insertTextFormat;
621696 filterText;
622697 detail;
623- } =
698+ env;
699+ } ~full =
624700 let item =
625- mkItem ~name
701+ mkItem name
702+ ?data:(kindToData (full.file.uri |> Uri. toPath) kind)
626703 ~kind: (Completion. kindToInt kind)
627704 ~deprecated
628705 ~detail:
629706 (match detail with
630707 | None -> kindToDetail name kind
631708 | Some detail -> detail)
632- ~docstring
709+ ~docstring:
710+ (match
711+ kindToDocumentation ~current Docstring:docstring ~full ~env name kind
712+ with
713+ | "" -> []
714+ | docstring -> [docstring])
633715 in
634716 {item with sortText; insertText; insertTextFormat; filterText}
635717
0 commit comments