Skip to content

[Poc]: Doc extraction continuation (PR 732) #780

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 40 additions & 3 deletions analysis/src/DocExtraction.ml
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
type fieldDoc = {fieldName: string; docstrings: string list; signature: string}
type fieldDoc = {
fieldName: string;
docstrings: string list;
signature: string;
optional: bool;
deprecated: string option;
}

type constructorDoc = {
constructorName: string;
docstrings: string list;
signature: string;
deprecated: string option;
}

type docsForModuleAlias = {
Expand All @@ -22,12 +29,14 @@ type docItem =
docstring: string list;
signature: string;
name: string;
deprecated: string option;
}
| Type of {
id: string;
docstring: string list;
signature: string;
name: string;
deprecated: string option;
detail: docItemDetail option;
(** Additional documentation for constructors and record fields, if available. *)
}
Expand All @@ -36,6 +45,7 @@ type docItem =
and docsForModule = {
id: string;
docstring: string list;
deprecated: string option;
name: string;
items: docItem list;
}
Expand Down Expand Up @@ -69,6 +79,11 @@ let stringifyDetail ?(indentation = 0) (detail : docItemDetail) =
stringifyObject ~indentation:(indentation + 1)
[
("fieldName", Some (wrapInQuotes fieldDoc.fieldName));
( "deprecated",
match fieldDoc.deprecated with
| Some d -> Some (wrapInQuotes d)
| None -> None );
("optional", Some (string_of_bool fieldDoc.optional));
( "docstrings",
Some (stringifyDocstrings fieldDoc.docstrings) );
("signature", Some (wrapInQuotes fieldDoc.signature));
Expand All @@ -88,6 +103,10 @@ let stringifyDetail ?(indentation = 0) (detail : docItemDetail) =
[
( "constructorName",
Some (wrapInQuotes constructorDoc.constructorName) );
( "deprecated",
match constructorDoc.deprecated with
| Some d -> Some (wrapInQuotes d)
| None -> None );
( "docstrings",
Some (stringifyDocstrings constructorDoc.docstrings) );
( "signature",
Expand All @@ -99,22 +118,30 @@ let stringifyDetail ?(indentation = 0) (detail : docItemDetail) =
let rec stringifyDocItem ?(indentation = 0) ~originalEnv (item : docItem) =
let open Protocol in
match item with
| Value {id; docstring; signature; name} ->
| Value {id; docstring; signature; name; deprecated} ->
stringifyObject ~startOnNewline:true ~indentation
[
("id", Some (wrapInQuotes id));
("kind", Some (wrapInQuotes "value"));
("name", Some (name |> Json.escape |> wrapInQuotes));
( "deprecated",
match deprecated with
| Some d -> Some (wrapInQuotes d)
| None -> None );
( "signature",
Some (signature |> String.trim |> Json.escape |> wrapInQuotes) );
("docstrings", Some (stringifyDocstrings docstring));
]
| Type {id; docstring; signature; name; detail} ->
| Type {id; docstring; signature; name; deprecated; detail} ->
stringifyObject ~startOnNewline:true ~indentation
[
("id", Some (wrapInQuotes id));
("kind", Some (wrapInQuotes "type"));
("name", Some (name |> Json.escape |> wrapInQuotes));
( "deprecated",
match deprecated with
| Some d -> Some (wrapInQuotes d)
| None -> None );
("signature", Some (signature |> Json.escape |> wrapInQuotes));
("docstrings", Some (stringifyDocstrings docstring));
( "detail",
Expand Down Expand Up @@ -147,6 +174,10 @@ and stringifyDocsForModule ?(indentation = 0) ~originalEnv (d : docsForModule) =
stringifyObject ~startOnNewline:true ~indentation
[
("name", Some (wrapInQuotes d.name));
( "deprecated",
match d.deprecated with
| Some d -> Some (wrapInQuotes d)
| None -> None );
("docstrings", Some (stringifyDocstrings d.docstring));
( "items",
Some
Expand All @@ -169,7 +200,9 @@ let typeDetail typ ~env ~full =
{
fieldName = field.fname.txt;
docstrings = field.docstring;
optional = field.optional;
signature = Shared.typeToString field.typ;
deprecated = field.deprecated;
});
})
| Some (Tvariant {constructors}) ->
Expand All @@ -183,6 +216,7 @@ let typeDetail typ ~env ~full =
constructorName = c.cname.txt;
docstrings = c.docstring;
signature = CompletionBackEnd.showConstructor c;
deprecated = c.deprecated;
});
})
| _ -> None
Expand Down Expand Up @@ -225,6 +259,7 @@ let extractDocs ~path ~debug =
id = modulePath |> ident;
docstring = structure.docstring |> List.map String.trim;
name = structure.name;
deprecated = structure.deprecated;
items =
structure.items
|> List.filter_map (fun (item : Module.item) ->
Expand All @@ -239,6 +274,7 @@ let extractDocs ~path ~debug =
"let " ^ item.name ^ ": " ^ Shared.typeToString typ
|> formatCode;
name = item.name;
deprecated = item.deprecated;
})
| Type (typ, _) ->
Some
Expand All @@ -251,6 +287,7 @@ let extractDocs ~path ~debug =
|> Shared.declToString item.name
|> formatCode;
name = item.name;
deprecated = item.deprecated;
detail = typeDetail typ ~full ~env;
})
| Module (Ident p) ->
Expand Down
18 changes: 15 additions & 3 deletions analysis/src/ProcessCmt.ml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ let rec forTypeSignatureItem ~(env : SharedTypes.Env.t) ~(exported : Exported.t)
Module.kind = Module.Value declared.item;
name = declared.name.txt;
docstring = declared.docstring;
deprecated = declared.deprecated;
};
]
| Sig_type
Expand Down Expand Up @@ -132,6 +133,7 @@ let rec forTypeSignatureItem ~(env : SharedTypes.Env.t) ~(exported : Exported.t)
Module.kind = Type (declared.item, recStatus);
name = declared.name.txt;
docstring = declared.docstring;
deprecated = declared.deprecated;
};
]
| Sig_module (ident, {md_type; md_attributes; md_loc}, _) ->
Expand All @@ -149,6 +151,7 @@ let rec forTypeSignatureItem ~(env : SharedTypes.Env.t) ~(exported : Exported.t)
Module.kind = Module declared.item;
name = declared.name.txt;
docstring = declared.docstring;
deprecated = declared.deprecated;
};
]
| _ -> []
Expand All @@ -160,7 +163,7 @@ and forTypeSignature ~name ~env signature =
(fun item items -> forTypeSignatureItem ~env ~exported item @ items)
signature []
in
{Module.name; docstring = []; exported; items}
{Module.name; docstring = []; exported; items; deprecated = None}

and forTypeModule ~name ~env moduleType =
match moduleType with
Expand Down Expand Up @@ -312,6 +315,7 @@ let forTypeDeclaration ~env ~(exported : Exported.t)
Module.kind = Module.Type (declared.item, recStatus);
name = declared.name.txt;
docstring = declared.docstring;
deprecated = declared.deprecated;
}

let rec forSignatureItem ~env ~(exported : Exported.t)
Expand All @@ -330,6 +334,7 @@ let rec forSignatureItem ~env ~(exported : Exported.t)
Module.kind = Module.Value declared.item;
name = declared.name.txt;
docstring = declared.docstring;
deprecated = declared.deprecated;
};
]
| Tsig_type (recFlag, decls) ->
Expand Down Expand Up @@ -360,6 +365,7 @@ let rec forSignatureItem ~env ~(exported : Exported.t)
Module.kind = Module declared.item;
name = declared.name.txt;
docstring = declared.docstring;
deprecated = declared.deprecated;
};
]
| Tsig_recmodule modDecls ->
Expand Down Expand Up @@ -395,7 +401,8 @@ let forSignature ~name ~env sigItems =
| _ -> []
in
let docstring = attrsToDocstring attributes in
{Module.name; docstring; exported; items}
let deprecated = ProcessAttributes.findDeprecatedAttribute attributes in
{Module.name; docstring; exported; items; deprecated}

let forTreeModuleType ~name ~env {Typedtree.mty_desc} =
match mty_desc with
Expand Down Expand Up @@ -435,6 +442,7 @@ let rec forStructureItem ~env ~(exported : Exported.t) item =
Module.kind = Module.Value declared.item;
name = declared.name.txt;
docstring = declared.docstring;
deprecated = declared.deprecated;
}
:: !items
| Tpat_tuple pats | Tpat_array pats | Tpat_construct (_, _, pats) ->
Expand Down Expand Up @@ -469,6 +477,7 @@ let rec forStructureItem ~env ~(exported : Exported.t) item =
Module.kind = Module declared.item;
name = declared.name.txt;
docstring = declared.docstring;
deprecated = declared.deprecated;
};
]
| Tstr_recmodule modDecls ->
Expand Down Expand Up @@ -499,6 +508,7 @@ let rec forStructureItem ~env ~(exported : Exported.t) item =
Module.kind = Module modTypeItem;
name = declared.name.txt;
docstring = declared.docstring;
deprecated = declared.deprecated;
};
]
| Tstr_include {incl_mod; incl_type} ->
Expand Down Expand Up @@ -527,6 +537,7 @@ let rec forStructureItem ~env ~(exported : Exported.t) item =
Module.kind = Value declared.item;
name = declared.name.txt;
docstring = declared.docstring;
deprecated = declared.deprecated;
};
]
| Tstr_type (recFlag, decls) ->
Expand Down Expand Up @@ -587,7 +598,8 @@ and forStructure ~name ~env strItems =
| _ -> []
in
let docstring = attrsToDocstring attributes in
{Module.name; docstring; exported; items}
let deprecated = ProcessAttributes.findDeprecatedAttribute attributes in
{Module.name; docstring; exported; items; deprecated}

let fileForCmtInfos ~moduleName ~uri
({cmt_modname; cmt_annots} : Cmt_format.cmt_infos) =
Expand Down
9 changes: 8 additions & 1 deletion analysis/src/SharedTypes.ml
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,19 @@ module Module = struct
| Type of Type.t * Types.rec_status
| Module of t

and item = {kind: kind; name: string; docstring: string list}
and item = {
kind: kind;
name: string;
docstring: string list;
deprecated: string option;
}

and structure = {
name: string;
docstring: string list;
exported: Exported.t;
items: item list;
deprecated: string option
}

and t = Ident of Path.t | Structure of structure | Constraint of t * t
Expand Down Expand Up @@ -253,6 +259,7 @@ module File = struct
docstring = [];
exported = Exported.init ();
items = [];
deprecated = None
};
}
end
Expand Down
2 changes: 2 additions & 0 deletions analysis/tests/src/expected/DocExtractionRes.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ extracting docs for src/DocExtractionRes.res
"kind": "record",
"fieldDocs": [{
"fieldName": "name",
"optional": false,
"docstrings": ["The name of the stuff."],
"signature": "string"
}, {
"fieldName": "online",
"optional": false,
"docstrings": ["Whether stuff is online."],
"signature": "bool"
}]
Expand Down