Skip to content

[PoC] Doc extraction #732

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 21 commits into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
first tests
  • Loading branch information
aspeddro authored and zth committed Oct 9, 2023
commit 8d59cd0bef2511ee2c85f172db9efa2d52f2d44a
71 changes: 68 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,16 @@ type docItem =
docstring: string list;
signature: string;
name: string;
loc: Warnings.loc;
deprecated: string option;
}
| Type of {
id: string;
docstring: string list;
signature: string;
name: string;
loc: Warnings.loc;
deprecated: string option;
detail: docItemDetail option;
(** Additional documentation for constructors and record fields, if available. *)
}
Expand All @@ -36,6 +47,7 @@ type docItem =
and docsForModule = {
id: string;
docstring: string list;
deprecated: string option;
name: string;
items: docItem list;
}
Expand Down Expand Up @@ -69,6 +81,15 @@ let stringifyDetail ?(indentation = 0) (detail : docItemDetail) =
stringifyObject ~indentation:(indentation + 1)
[
("fieldName", Some (wrapInQuotes fieldDoc.fieldName));
( "deprecated",
Some
(string_of_bool (Option.is_some fieldDoc.deprecated))
);
( "deprecatedMessage",
match fieldDoc.deprecated with
| Some msg -> Some (wrapInQuotes msg)
| None -> None );
("optional", Some (string_of_bool fieldDoc.optional));
( "docstrings",
Some (stringifyDocstrings fieldDoc.docstrings) );
("signature", Some (wrapInQuotes fieldDoc.signature));
Expand All @@ -88,6 +109,14 @@ let stringifyDetail ?(indentation = 0) (detail : docItemDetail) =
[
( "constructorName",
Some (wrapInQuotes constructorDoc.constructorName) );
( "deprecated",
Some
(string_of_bool
(Option.is_some constructorDoc.deprecated)) );
( "deprecatedMessage",
match constructorDoc.deprecated with
| Some msg -> Some (wrapInQuotes msg)
| None -> None );
( "docstrings",
Some (stringifyDocstrings constructorDoc.docstrings) );
( "signature",
Expand All @@ -96,25 +125,48 @@ let stringifyDetail ?(indentation = 0) (detail : docItemDetail) =
|> array) );
]

let stringifyLoc loc ~indentation =
let open Protocol in
let line, col = Loc.start loc in
let path = loc.loc_start.pos_fname in
stringifyObject ~startOnNewline:false ~indentation:(indentation + 1)
[
("path", Some (wrapInQuotes path));
("line", Some (string_of_int (line + 1)));
("col", Some (string_of_int (col + 1)));
]

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; loc; deprecated} ->
stringifyObject ~startOnNewline:true ~indentation
[
("id", Some (wrapInQuotes id));
("kind", Some (wrapInQuotes "value"));
("name", Some (name |> Json.escape |> wrapInQuotes));
("deprecated", Some (string_of_bool (Option.is_some deprecated)));
( "deprecatedMessage",
match deprecated with
| Some msg -> Some (wrapInQuotes msg)
| None -> None );
("location", Some (stringifyLoc loc ~indentation));
( "signature",
Some (signature |> String.trim |> Json.escape |> wrapInQuotes) );
("docstrings", Some (stringifyDocstrings docstring));
]
| Type {id; docstring; signature; name; detail} ->
| Type {id; docstring; signature; name; loc; deprecated; detail} ->
stringifyObject ~startOnNewline:true ~indentation
[
("id", Some (wrapInQuotes id));
("kind", Some (wrapInQuotes "type"));
("name", Some (name |> Json.escape |> wrapInQuotes));
("deprecated", Some (string_of_bool (Option.is_some deprecated)));
( "deprecatedMessage",
match deprecated with
| Some msg -> Some (wrapInQuotes msg)
| None -> None );
("location", Some (stringifyLoc loc ~indentation));
("signature", Some (signature |> Json.escape |> wrapInQuotes));
("docstrings", Some (stringifyDocstrings docstring));
( "detail",
Expand Down Expand Up @@ -147,6 +199,11 @@ and stringifyDocsForModule ?(indentation = 0) ~originalEnv (d : docsForModule) =
stringifyObject ~startOnNewline:true ~indentation
[
("name", Some (wrapInQuotes d.name));
("deprecated", Some (string_of_bool (Option.is_some d.deprecated)));
( "deprecatedMessage",
match d.deprecated with
| Some msg -> Some (wrapInQuotes msg)
| None -> None );
("docstrings", Some (stringifyDocstrings d.docstring));
( "items",
Some
Expand All @@ -169,7 +226,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 +242,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 +285,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 +300,8 @@ let extractDocs ~path ~debug =
"let " ^ item.name ^ ": " ^ Shared.typeToString typ
|> formatCode;
name = item.name;
loc = item.loc;
deprecated = item.deprecated;
})
| Type (typ, _) ->
Some
Expand All @@ -251,6 +314,8 @@ let extractDocs ~path ~debug =
|> Shared.declToString item.name
|> formatCode;
name = item.name;
loc = item.loc;
deprecated = item.deprecated;
detail = typeDetail typ ~full ~env;
})
| Module (Ident p) ->
Expand Down
28 changes: 25 additions & 3 deletions analysis/src/ProcessCmt.ml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ let rec forTypeSignatureItem ~(env : SharedTypes.Env.t) ~(exported : Exported.t)
Module.kind = Module.Value declared.item;
name = declared.name.txt;
docstring = declared.docstring;
loc;
deprecated = declared.deprecated;
};
]
| Sig_type
Expand Down Expand Up @@ -132,6 +134,8 @@ let rec forTypeSignatureItem ~(env : SharedTypes.Env.t) ~(exported : Exported.t)
Module.kind = Type (declared.item, recStatus);
name = declared.name.txt;
docstring = declared.docstring;
loc = type_loc;
deprecated = declared.deprecated;
};
]
| Sig_module (ident, {md_type; md_attributes; md_loc}, _) ->
Expand All @@ -149,6 +153,8 @@ let rec forTypeSignatureItem ~(env : SharedTypes.Env.t) ~(exported : Exported.t)
Module.kind = Module declared.item;
name = declared.name.txt;
docstring = declared.docstring;
loc = md_loc;
deprecated = declared.deprecated;
};
]
| _ -> []
Expand All @@ -160,7 +166,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 +318,8 @@ let forTypeDeclaration ~env ~(exported : Exported.t)
Module.kind = Module.Type (declared.item, recStatus);
name = declared.name.txt;
docstring = declared.docstring;
loc = typ_loc;
deprecated = declared.deprecated;
}

let rec forSignatureItem ~env ~(exported : Exported.t)
Expand All @@ -330,6 +338,8 @@ let rec forSignatureItem ~env ~(exported : Exported.t)
Module.kind = Module.Value declared.item;
name = declared.name.txt;
docstring = declared.docstring;
loc = name.loc;
deprecated = declared.deprecated;
};
]
| Tsig_type (recFlag, decls) ->
Expand Down Expand Up @@ -360,6 +370,8 @@ let rec forSignatureItem ~env ~(exported : Exported.t)
Module.kind = Module declared.item;
name = declared.name.txt;
docstring = declared.docstring;
loc = name.loc;
deprecated = declared.deprecated;
};
]
| Tsig_recmodule modDecls ->
Expand Down Expand Up @@ -395,7 +407,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 +448,8 @@ let rec forStructureItem ~env ~(exported : Exported.t) item =
Module.kind = Module.Value declared.item;
name = declared.name.txt;
docstring = declared.docstring;
loc = pat.pat_loc;
deprecated = declared.deprecated;
}
:: !items
| Tpat_tuple pats | Tpat_array pats | Tpat_construct (_, _, pats) ->
Expand Down Expand Up @@ -469,6 +484,8 @@ let rec forStructureItem ~env ~(exported : Exported.t) item =
Module.kind = Module declared.item;
name = declared.name.txt;
docstring = declared.docstring;
loc = name.loc;
deprecated = declared.deprecated;
};
]
| Tstr_recmodule modDecls ->
Expand Down Expand Up @@ -499,6 +516,8 @@ let rec forStructureItem ~env ~(exported : Exported.t) item =
Module.kind = Module modTypeItem;
name = declared.name.txt;
docstring = declared.docstring;
loc = name.loc;
deprecated = declared.deprecated;
};
]
| Tstr_include {incl_mod; incl_type} ->
Expand Down Expand Up @@ -527,6 +546,8 @@ let rec forStructureItem ~env ~(exported : Exported.t) item =
Module.kind = Value declared.item;
name = declared.name.txt;
docstring = declared.docstring;
loc = vd.val_loc;
deprecated = declared.deprecated;
};
]
| Tstr_type (recFlag, decls) ->
Expand Down Expand Up @@ -587,7 +608,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
10 changes: 9 additions & 1 deletion analysis/src/SharedTypes.ml
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,20 @@ 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;
loc: Warnings.loc;
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 +260,7 @@ module File = struct
docstring = [];
exported = Exported.init ();
items = [];
deprecated = None
};
}
end
Expand Down