Skip to content

Commit 502c1df

Browse files
committed
docstrings for record fields
1 parent 900a7e3 commit 502c1df

File tree

7 files changed

+107
-39
lines changed

7 files changed

+107
-39
lines changed

analysis/src/CompletionBackEnd.ml

Lines changed: 46 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,8 @@ let completionForExporteds iterExported getDeclared ~prefix ~exact ~env
558558
res :=
559559
{
560560
(Completion.create ~name:declared.name.txt ~env
561-
~kind:(transformContents declared.item))
561+
~kind:(transformContents declared.item)
562+
())
562563
with
563564
deprecated = declared.deprecated;
564565
docstring = declared.docstring;
@@ -600,7 +601,8 @@ let completionsForExportedConstructors ~(env : QueryEnv.t) ~prefix ~exact
600601
(Completion.create ~name ~env
601602
~kind:
602603
(Completion.Constructor
603-
(c, t.item.decl |> Shared.declToString t.name.txt)))
604+
(c, t.item.decl |> Shared.declToString t.name.txt))
605+
())
604606
else None))
605607
@ !res
606608
| _ -> ());
@@ -619,10 +621,11 @@ let completionForExportedFields ~(env : QueryEnv.t) ~prefix ~exact ~namesUsed =
619621
if not (Hashtbl.mem namesUsed name) then
620622
let () = Hashtbl.add namesUsed name () in
621623
Some
622-
(Completion.create ~name ~env
624+
(Completion.create ~name ~env ~docstring:f.docstring
623625
~kind:
624626
(Completion.Field
625-
(f, t.item.decl |> Shared.declToString t.name.txt)))
627+
(f, t.item.decl |> Shared.declToString t.name.txt))
628+
())
626629
else None))
627630
@ !res
628631
| _ -> ());
@@ -801,7 +804,7 @@ let processLocalValue name loc ~prefix ~exact ~env
801804
localTables.resultRev <-
802805
{
803806
(Completion.create ~name:declared.name.txt ~env
804-
~kind:(Value declared.item))
807+
~kind:(Value declared.item) ())
805808
with
806809
deprecated = declared.deprecated;
807810
docstring = declared.docstring;
@@ -818,6 +821,7 @@ let processLocalValue name loc ~prefix ~exact ~env
818821
(Ctype.newconstr
819822
(Path.Pident (Ident.create "Type Not Known"))
820823
[]))
824+
()
821825
:: localTables.resultRev
822826

823827
let processLocalConstructor name loc ~prefix ~exact ~env
@@ -836,7 +840,8 @@ let processLocalConstructor name loc ~prefix ~exact ~env
836840
(Constructor
837841
( declared.item,
838842
snd declared.item.typeDecl
839-
|> Shared.declToString (fst declared.item.typeDecl) )))
843+
|> Shared.declToString (fst declared.item.typeDecl) ))
844+
())
840845
with
841846
deprecated = declared.deprecated;
842847
docstring = declared.docstring;
@@ -857,7 +862,7 @@ let processLocalType name loc ~prefix ~exact ~env ~(localTables : LocalTables.t)
857862
localTables.resultRev <-
858863
{
859864
(Completion.create ~name:declared.name.txt ~env
860-
~kind:(Type declared.item))
865+
~kind:(Type declared.item) ())
861866
with
862867
deprecated = declared.deprecated;
863868
docstring = declared.docstring;
@@ -878,7 +883,7 @@ let processLocalModule name loc ~prefix ~exact ~env
878883
localTables.resultRev <-
879884
{
880885
(Completion.create ~name:declared.name.txt ~env
881-
~kind:(Module declared.item))
886+
~kind:(Module declared.item) ())
882887
with
883888
deprecated = declared.deprecated;
884889
docstring = declared.docstring;
@@ -1139,7 +1144,8 @@ let getComplementaryCompletionsForTypedValue ~opens ~allFiles ~scope ~env prefix
11391144
(String.contains name '-')
11401145
then
11411146
Some
1142-
(Completion.create ~name ~env ~kind:(Completion.FileModule name))
1147+
(Completion.create ~name ~env ~kind:(Completion.FileModule name)
1148+
())
11431149
else None)
11441150
in
11451151
localCompletionsWithOpens @ fileModules
@@ -1164,7 +1170,7 @@ let getCompletionsForPath ~package ~opens ~allFiles ~pos ~exact ~scope
11641170
then
11651171
Some
11661172
(Completion.create ~name ~env
1167-
~kind:(Completion.FileModule name))
1173+
~kind:(Completion.FileModule name) ())
11681174
else None)
11691175
in
11701176
localCompletionsWithOpens @ fileModules
@@ -1353,28 +1359,32 @@ let rec getCompletionsForContextPath ~full ~opens ~rawOpens ~allFiles ~pos ~env
13531359
Completion.create ~name:"string" ~env
13541360
~kind:
13551361
(Completion.Value
1356-
(Ctype.newconstr (Path.Pident (Ident.create "string")) []));
1362+
(Ctype.newconstr (Path.Pident (Ident.create "string")) []))
1363+
();
13571364
]
13581365
| CPInt ->
13591366
[
13601367
Completion.create ~name:"int" ~env
13611368
~kind:
13621369
(Completion.Value
1363-
(Ctype.newconstr (Path.Pident (Ident.create "int")) []));
1370+
(Ctype.newconstr (Path.Pident (Ident.create "int")) []))
1371+
();
13641372
]
13651373
| CPFloat ->
13661374
[
13671375
Completion.create ~name:"float" ~env
13681376
~kind:
13691377
(Completion.Value
1370-
(Ctype.newconstr (Path.Pident (Ident.create "float")) []));
1378+
(Ctype.newconstr (Path.Pident (Ident.create "float")) []))
1379+
();
13711380
]
13721381
| CPArray ->
13731382
[
13741383
Completion.create ~name:"array" ~env
13751384
~kind:
13761385
(Completion.Value
1377-
(Ctype.newconstr (Path.Pident (Ident.create "array")) []));
1386+
(Ctype.newconstr (Path.Pident (Ident.create "array")) []))
1387+
();
13781388
]
13791389
| CPId (path, completionContext) ->
13801390
path
@@ -1418,7 +1428,10 @@ let rec getCompletionsForContextPath ~full ~opens ~rawOpens ~allFiles ~pos ~env
14181428
| args, tRet when args <> [] ->
14191429
let args = processApply args labels in
14201430
let retType = reconstructFunctionType args tRet in
1421-
[Completion.create ~name:"dummy" ~env ~kind:(Completion.Value retType)]
1431+
[
1432+
Completion.create ~name:"dummy" ~env ~kind:(Completion.Value retType)
1433+
();
1434+
]
14221435
| _ -> [])
14231436
| None -> [])
14241437
| CPField (CPId (path, Module), fieldName) ->
@@ -1441,11 +1454,13 @@ let rec getCompletionsForContextPath ~full ~opens ~rawOpens ~allFiles ~pos ~env
14411454
if checkName field.fname.txt ~prefix:fieldName ~exact then
14421455
Some
14431456
(Completion.create ~name:field.fname.txt ~env
1457+
~docstring:field.docstring
14441458
~kind:
14451459
(Completion.Field
14461460
( field,
14471461
typDecl.item.decl
1448-
|> Shared.declToString typDecl.name.txt )))
1462+
|> Shared.declToString typDecl.name.txt ))
1463+
())
14491464
else None)
14501465
| None -> [])
14511466
| None -> [])
@@ -1473,7 +1488,7 @@ let rec getCompletionsForContextPath ~full ~opens ~rawOpens ~allFiles ~pos ~env
14731488
if checkName field ~prefix:label ~exact then
14741489
Some
14751490
(Completion.create ~name:field ~env
1476-
~kind:(Completion.ObjLabel typ))
1491+
~kind:(Completion.ObjLabel typ) ())
14771492
else None)
14781493
| None -> [])
14791494
| None -> [])
@@ -1656,7 +1671,8 @@ let rec getCompletionsForContextPath ~full ~opens ~rawOpens ~allFiles ~pos ~env
16561671
if List.length ctxPaths = List.length typeExrps then
16571672
[
16581673
Completion.create ~name:"dummy" ~env
1659-
~kind:(Completion.Value (Ctype.newty (Ttuple typeExrps)));
1674+
~kind:(Completion.Value (Ctype.newty (Ttuple typeExrps)))
1675+
();
16601676
]
16611677
else []
16621678
| CJsxPropValue {pathToComponent; propName} -> (
@@ -1675,7 +1691,8 @@ let rec getCompletionsForContextPath ~full ~opens ~rawOpens ~allFiles ~pos ~env
16751691
| Some (_, typ, env) ->
16761692
[
16771693
Completion.create ~name:"dummy" ~env
1678-
~kind:(Completion.Value (Utils.unwrapIfOption typ));
1694+
~kind:(Completion.Value (Utils.unwrapIfOption typ))
1695+
();
16791696
])
16801697
| CArgument {functionContextPath; argumentLabel} -> (
16811698
let labels, env =
@@ -1710,7 +1727,8 @@ let rec getCompletionsForContextPath ~full ~opens ~rawOpens ~allFiles ~pos ~env
17101727
Completion.create ~name:"dummy" ~env
17111728
~kind:
17121729
(Completion.Value
1713-
(if expandOption then Utils.unwrapIfOption typ else typ));
1730+
(if expandOption then Utils.unwrapIfOption typ else typ))
1731+
();
17141732
])
17151733

17161734
let getOpens ~debug ~rawOpens ~package ~env =
@@ -1803,10 +1821,10 @@ let rec completeTypedValue (t : Types.type_expr) ~env ~full ~prefix
18031821
[
18041822
Completion.create ~name:"true"
18051823
~kind:(Label (t |> Shared.typeToString))
1806-
~env;
1824+
~env ();
18071825
Completion.create ~name:"false"
18081826
~kind:(Label (t |> Shared.typeToString))
1809-
~env;
1827+
~env ();
18101828
]
18111829
|> filterItems ~prefix
18121830
| Some (Tvariant {env; constructors; variantDecl; variantName}) ->
@@ -1867,7 +1885,7 @@ let rec completeTypedValue (t : Types.type_expr) ~env ~full ~prefix
18671885
[
18681886
Completion.create ~name:"None"
18691887
~kind:(Label (t |> Shared.typeToString))
1870-
~env;
1888+
~env ();
18711889
Completion.createWithSnippet ~name:"Some(_)"
18721890
~kind:(Label (t |> Shared.typeToString))
18731891
~env ~insertText:"Some(${1:_})" ();
@@ -1894,7 +1912,7 @@ let rec completeTypedValue (t : Types.type_expr) ~env ~full ~prefix
18941912
|> List.map (fun (field : field) ->
18951913
Completion.create ~name:field.fname.txt
18961914
~kind:(Field (field, typeExpr |> Shared.typeToString))
1897-
~env)
1915+
~env ())
18981916
|> filterItems ~prefix
18991917
| None ->
19001918
if prefix = "" then
@@ -1998,7 +2016,7 @@ let rec processCompletable ~debug ~full ~scope ~env ~pos ~forHover
19982016
| Cjsx ([id], prefix, identsSeen) when String.uncapitalize_ascii id = id ->
19992017
(* Lowercase JSX tag means builtin *)
20002018
let mkLabel (name, typString) =
2001-
Completion.create ~name ~kind:(Label typString) ~env
2019+
Completion.create ~name ~kind:(Label typString) ~env ()
20022020
in
20032021
let keyLabels =
20042022
if Utils.startsWith "key" prefix then [mkLabel ("key", "string")] else []
@@ -2012,7 +2030,7 @@ let rec processCompletable ~debug ~full ~scope ~env ~pos ~forHover
20122030
| Cjsx (componentPath, prefix, identsSeen) ->
20132031
let labels = getJsxLabels ~componentPath ~findTypeOfValue ~package in
20142032
let mkLabel_ name typString =
2015-
Completion.create ~name ~kind:(Label typString) ~env
2033+
Completion.create ~name ~kind:(Label typString) ~env ()
20162034
in
20172035
let mkLabel (name, typ, _env) =
20182036
mkLabel_ name (typ |> Shared.typeToString)
@@ -2031,7 +2049,7 @@ let rec processCompletable ~debug ~full ~scope ~env ~pos ~forHover
20312049
@ keyLabels
20322050
| Cdecorator prefix ->
20332051
let mkDecorator (name, docstring) =
2034-
{(Completion.create ~name ~kind:(Label "") ~env) with docstring}
2052+
{(Completion.create ~name ~kind:(Label "") ~env ()) with docstring}
20352053
in
20362054
[
20372055
( "as",
@@ -2288,7 +2306,7 @@ Note: The `@react.component` decorator requires the react-jsx config to be set i
22882306
| None -> []
22892307
in
22902308
let mkLabel (name, typ) =
2291-
Completion.create ~name ~kind:(Label (typ |> Shared.typeToString)) ~env
2309+
Completion.create ~name ~kind:(Label (typ |> Shared.typeToString)) ~env ()
22922310
in
22932311
labels
22942312
|> List.filter (fun (name, _t) ->

analysis/src/Hover.ml

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,7 @@ let findRelevantTypesFromType ~file ~package typ =
9494
constructors |> List.filter_map (fromConstructorPath ~env:envToSearch)
9595

9696
(* Produces a hover with relevant types expanded in the main type being hovered. *)
97-
let hoverWithExpandedTypes ~docstring ~file ~package ~supportsMarkdownLinks typ
98-
=
97+
let hoverWithExpandedTypes ~file ~package ~supportsMarkdownLinks typ =
9998
let typeString = Markdown.codeBlock (typ |> Shared.typeToString) in
10099
let types = findRelevantTypesFromType typ ~file ~package in
101100
let typeDefinitions =
@@ -114,7 +113,7 @@ let hoverWithExpandedTypes ~docstring ~file ~package ~supportsMarkdownLinks typ
114113
(SharedTypes.pathIdentToString path))
115114
^ linkToTypeDefinitionStr ^ "\n")
116115
in
117-
(typeString :: typeDefinitions |> String.concat "\n", docstring)
116+
typeString :: typeDefinitions |> String.concat "\n"
118117

119118
(* Leverages autocomplete functionality to produce a hover for a position. This
120119
makes it (most often) work with unsaved content. *)
@@ -151,12 +150,20 @@ let getHoverViaCompletions ~debug ~path ~pos ~currentFile ~forHover
151150
@ docstring
152151
in
153152
Some (Protocol.stringifyHover (String.concat "\n\n" parts))
153+
| {kind = Field _; docstring} :: _ -> (
154+
match CompletionBackEnd.completionsGetTypeEnv completions with
155+
| Some (typ, _env) ->
156+
let typeString =
157+
hoverWithExpandedTypes ~file ~package ~supportsMarkdownLinks typ
158+
in
159+
let parts = [typeString] @ docstring in
160+
Some (Protocol.stringifyHover (String.concat "\n\n" parts))
161+
| None -> None)
154162
| _ -> (
155163
match CompletionBackEnd.completionsGetTypeEnv completions with
156164
| Some (typ, _env) ->
157-
let typeString, _docstring =
158-
hoverWithExpandedTypes ~docstring:"" ~file ~package
159-
~supportsMarkdownLinks typ
165+
let typeString =
166+
hoverWithExpandedTypes ~file ~package ~supportsMarkdownLinks typ
160167
in
161168
Some (Protocol.stringifyHover typeString)
162169
| None -> None))))
@@ -221,8 +228,8 @@ let newHover ~full:{file; package} ~supportsMarkdownLinks locItem =
221228
| Const_nativeint _ -> "int"))
222229
| Typed (_, t, locKind) ->
223230
let fromType ~docstring typ =
224-
hoverWithExpandedTypes ~docstring ~file ~package ~supportsMarkdownLinks
225-
typ
231+
( hoverWithExpandedTypes ~file ~package ~supportsMarkdownLinks typ,
232+
docstring )
226233
in
227234
let parts =
228235
match References.definedForLoc ~file ~package locKind with

analysis/src/ProcessCmt.ml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@ let rec forTypeSignatureItem ~(env : SharedTypes.Env.t) ~(exported : Exported.t)
9999
optional =
100100
Res_parsetree_viewer.hasOptionalAttribute
101101
ld_attributes;
102+
docstring =
103+
(match
104+
ProcessAttributes.findDocAttribute ld_attributes
105+
with
106+
| None -> []
107+
| Some docstring -> [docstring]);
102108
})));
103109
}
104110
~name ~stamp:(Ident.binding_time ident) ~env type_attributes
@@ -230,6 +236,12 @@ let forTypeDeclaration ~env ~(exported : Exported.t)
230236
optional =
231237
Res_parsetree_viewer.hasOptionalAttribute
232238
ld_attributes;
239+
docstring =
240+
(match
241+
ProcessAttributes.findDocAttribute ld_attributes
242+
with
243+
| None -> []
244+
| Some docstring -> [docstring]);
233245
})));
234246
}
235247
~name ~stamp ~env typ_attributes

analysis/src/References.ml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,12 @@ let definedForLoc ~file ~package locKind =
170170
match getConstructor file stamp name with
171171
| None -> None
172172
| Some constructor -> Some ([], `Constructor constructor))
173-
| Field _name -> Some ([], `Field)
173+
| Field name ->
174+
Some
175+
( (match getField file stamp name with
176+
| None -> []
177+
| Some field -> field.docstring),
178+
`Field )
174179
| _ -> (
175180
maybeLog
176181
("Trying for declared " ^ Tip.toString tip ^ " " ^ string_of_int stamp

analysis/src/SharedTypes.ml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ type field = {
2929
fname: string Location.loc;
3030
typ: Types.type_expr;
3131
optional: bool;
32+
docstring: string list;
3233
}
3334

3435
module Constructor = struct
@@ -309,12 +310,12 @@ module Completion = struct
309310
kind: kind;
310311
}
311312

312-
let create ~name ~kind ~env =
313+
let create ~name ~kind ~env ?(docstring = []) () =
313314
{
314315
name;
315316
env;
316317
deprecated = None;
317-
docstring = [];
318+
docstring;
318319
kind;
319320
sortText = None;
320321
insertText = None;

0 commit comments

Comments
 (0)