@@ -79,7 +79,7 @@ let completionForExporteds iterExported getDeclared ~prefix ~exact ~env
79
79
res :=
80
80
{
81
81
(Completion. create declared.name.txt ~env
82
- ~kind: (transformContents declared.item ))
82
+ ~kind: (transformContents declared))
83
83
with
84
84
deprecated = declared.deprecated;
85
85
docstring = declared.docstring;
@@ -90,18 +90,20 @@ let completionForExporteds iterExported getDeclared ~prefix ~exact ~env
90
90
91
91
let completionForExportedModules ~env ~prefix ~exact ~namesUsed =
92
92
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})
95
97
96
98
let completionForExportedValues ~env ~prefix ~exact ~namesUsed =
97
99
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 )
100
102
101
103
let completionForExportedTypes ~env ~prefix ~exact ~namesUsed =
102
104
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 )
105
107
106
108
let completionsForExportedConstructors ~(env : QueryEnv.t ) ~prefix ~exact
107
109
~namesUsed =
@@ -224,39 +226,109 @@ let getEnvWithOpens ~scope ~(env : QueryEnv.t) ~package
224
226
| None -> None
225
227
| Some env -> ResolvePath. resolvePath ~env ~package ~path ))
226
228
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
+
227
296
let kindToDetail name (kind : Completion.kind ) =
228
297
match kind with
229
- | Type {decl } -> decl |> Shared. declToString name
298
+ | Type {name } -> " type " ^ name
230
299
| Value typ -> typ |> Shared. typeToString
231
300
| ObjLabel typ -> typ |> Shared. typeToString
232
301
| 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} , _ ) ->
236
305
(* Handle optional fields. Checking for "?" is because sometimes optional
237
306
fields are prefixed with "?" when completing, and at that point we don't
238
307
need to _also_ add a "?" after the field name, as that looks weird. *)
239
308
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} , _ ) -> (
246
313
" #" ^ 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
+ ^ " ) " )
256
323
| Snippet s -> s
257
324
| FollowContextPath _ -> " "
258
325
| 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
260
332
261
333
let findAllCompletions ~(env : QueryEnv.t ) ~prefix ~exact ~namesUsed
262
334
~(completionContext : Completable.completionContext ) =
@@ -366,7 +438,9 @@ let processLocalModule name loc ~prefix ~exact ~env
366
438
localTables.resultRev < -
367
439
{
368
440
(Completion. create declared.name.txt ~env
369
- ~kind: (Module declared.item))
441
+ ~kind:
442
+ (Module
443
+ {docstring = declared.docstring; module_ = declared.item}))
370
444
with
371
445
deprecated = declared.deprecated;
372
446
docstring = declared.docstring;
@@ -579,7 +653,7 @@ let rec digToRecordFieldsForCompletion ~debug ~package ~opens ~full ~pos ~env
579
653
| {kind = Type {kind = Record fields } } :: _ -> Some fields
580
654
| _ -> None
581
655
582
- let mkItem ~ name ~kind ~detail ~deprecated ~docstring =
656
+ let mkItem ? data name ~kind ~detail ~deprecated ~docstring =
583
657
let docContent =
584
658
(match deprecated with
585
659
| None -> " "
@@ -607,6 +681,7 @@ let mkItem ~name ~kind ~detail ~deprecated ~docstring =
607
681
insertText = None ;
608
682
insertTextFormat = None ;
609
683
filterText = None ;
684
+ data;
610
685
}
611
686
612
687
let completionToItem
@@ -620,16 +695,23 @@ let completionToItem
620
695
insertTextFormat;
621
696
filterText;
622
697
detail;
623
- } =
698
+ env;
699
+ } ~full =
624
700
let item =
625
- mkItem ~name
701
+ mkItem name
702
+ ?data:(kindToData (full.file.uri |> Uri. toPath) kind)
626
703
~kind: (Completion. kindToInt kind)
627
704
~deprecated
628
705
~detail:
629
706
(match detail with
630
707
| None -> kindToDetail name kind
631
708
| Some detail -> detail)
632
- ~docstring
709
+ ~docstring:
710
+ (match
711
+ kindToDocumentation ~current Docstring:docstring ~full ~env name kind
712
+ with
713
+ | "" -> []
714
+ | docstring -> [docstring])
633
715
in
634
716
{item with sortText; insertText; insertTextFormat; filterText}
635
717
0 commit comments