Skip to content
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

Remove unneeded code and unify "utils" modules #1321

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
1 change: 0 additions & 1 deletion odoc-parser.opam
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ depends: [
"dune" {>= "3.7"}
"ocaml" {>= "4.08.0" & < "5.4"}
"astring"
"result"
"camlp-streams"
"ppx_expect" {with-test}
"sexplib0" {with-test}
Expand Down
1 change: 0 additions & 1 deletion odoc.opam
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ depends: [
"dune" {>= "3.7.0"}
"fpath"
"ocaml" {>= "4.08.0" & < "5.4"}
"result"
"tyxml" {>= "4.4.0"}
"fmt"
"crunch" {>= "1.4.1"}
Expand Down
25 changes: 23 additions & 2 deletions src/document/codefmt.ml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,27 @@ module State = struct
flush state)
end

let rec compute_length_source (t : Types.Source.t) : int =
let f (acc : int) = function
| Types.Source.Elt t -> acc + compute_length_inline t
| Types.Source.Tag (_, t) -> acc + compute_length_source t
in
List.fold_left f 0 t

and compute_length_inline (t : Types.Inline.t) : int =
let f (acc : int) { Types.Inline.desc; _ } =
match desc with
| Text s -> acc + String.length s
| Entity _e -> acc + 1
| Linebreak -> 0 (* TODO *)
| Styled (_, t) | Link { content = t; _ } -> acc + compute_length_inline t
| Source s -> acc + compute_length_source s
| Math _ -> assert false
| Raw_markup _ -> assert false
(* TODO *)
in
List.fold_left f 0 t

(** Modern implementation using semantic tags, Only for 4.08+ *)

(*
Expand Down Expand Up @@ -79,7 +100,7 @@ module Tag = struct

let elt ppf elt =
Format.pp_open_stag ppf (Elt elt);
Format.pp_print_as ppf (Utils.compute_length_inline elt) "";
Format.pp_print_as ppf (compute_length_inline elt) "";
Format.pp_close_stag ppf ()

let ignore ppf txt =
Expand Down Expand Up @@ -140,7 +161,7 @@ module Tag = struct

let elt ppf (elt : Inline.t) =
Format.fprintf ppf "@{<tag:%s>%t@}" (Marshal.to_string elt []) (fun fmt ->
Format.pp_print_as fmt (Utils.compute_length_inline elt) "")
Format.pp_print_as fmt (compute_length_inline elt) "")

let ignore ppf txt = Format.fprintf ppf "@{<ignore-tag>%t@}" txt
end
Expand Down
7 changes: 4 additions & 3 deletions src/document/comment.ml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*)

open Odoc_utils
open Types
module Comment = Odoc_model.Comment
open Odoc_model.Names
Expand Down Expand Up @@ -67,7 +68,7 @@ module Reference = struct
| `TAbsolutePath -> "/"
| `TCurrentPackage -> "//"
in
tag ^ String.concat "/" cs
tag ^ String.concat ~sep:"/" cs

let rec render_unresolved : Reference.t -> string =
let open Reference in
Expand Down Expand Up @@ -412,11 +413,11 @@ let synopsis ~decl_doc ~expansion_doc =
match Comment.synopsis docs with Some p -> [ paragraph p ] | None -> []

let standalone docs =
Utils.flatmap ~f:item_element
List.concat_map item_element
@@ List.map (fun x -> x.Odoc_model.Location_.value) docs

let to_ir (docs : Comment.elements) =
Utils.flatmap ~f:block_element
List.concat_map block_element
@@ List.map (fun x -> x.Odoc_model.Location_.value) docs

let has_doc docs = docs <> []
27 changes: 16 additions & 11 deletions src/document/doctree.ml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
open Odoc_utils
open Types

module Take = struct
Expand Down Expand Up @@ -97,19 +98,23 @@ module Subpages : sig
val compute : Page.t -> Subpage.t list
end = struct
let rec walk_documentedsrc (l : DocumentedSrc.t) =
Utils.flatmap l ~f:(function
| DocumentedSrc.Code _ -> []
| Documented _ -> []
| Nested { code; _ } -> walk_documentedsrc code
| Subpage p -> [ p ]
| Alternative (Expansion r) -> walk_documentedsrc r.expansion)
List.concat_map
(function
| DocumentedSrc.Code _ -> []
| Documented _ -> []
| Nested { code; _ } -> walk_documentedsrc code
| Subpage p -> [ p ]
| Alternative (Expansion r) -> walk_documentedsrc r.expansion)
l

let rec walk_items (l : Item.t list) =
Utils.flatmap l ~f:(function
| Item.Text _ -> []
| Heading _ -> []
| Declaration { content; _ } -> walk_documentedsrc content
| Include i -> walk_items i.content.content)
List.concat_map
(function
| Item.Text _ -> []
| Heading _ -> []
| Declaration { content; _ } -> walk_documentedsrc content
| Include i -> walk_items i.content.content)
l

let compute (p : Page.t) = walk_items (p.preamble @ p.items)
end
Expand Down
44 changes: 23 additions & 21 deletions src/document/generator.ml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*)

open Odoc_utils
open Odoc_model.Names
module Location = Odoc_model.Location_
module Paths = Odoc_model.Paths
Expand Down Expand Up @@ -76,7 +77,7 @@ let mk_heading ?(level = 1) ?label text =
rest is inserted into [items]. *)
let prepare_preamble comment items =
let preamble, first_comment =
Utils.split_at
List.split_at
~f:(function
| { Odoc_model.Location_.value = `Heading _; _ } -> true | _ -> false)
comment
Expand Down Expand Up @@ -213,7 +214,7 @@ module Make (Syntax : SYNTAX) = struct
let in_bound x = min (max x 0) (String.length src) in
let a = in_bound a and b = in_bound b in
let a, b = (min a b, max a b) in
String.sub src a (b - a)
String.with_range src ~first:a ~len:(b - a)
in
let plain_code = function
| "" -> []
Expand Down Expand Up @@ -358,7 +359,7 @@ module Make (Syntax : SYNTAX) = struct
| Open -> O.txt "[> " ++ elements ++ O.txt " ]"
| Closed [] -> O.txt "[< " ++ elements ++ O.txt " ]"
| Closed lst ->
let constrs = String.concat " " lst in
let constrs = String.concat ~sep:" " lst in
O.txt "[< " ++ elements ++ O.txt (" " ^ constrs ^ " ]"))

and te_object (t : Odoc_model.Lang.TypeExpr.Object.t) =
Expand Down Expand Up @@ -461,7 +462,7 @@ module Make (Syntax : SYNTAX) = struct
format_type_path ~delim:`brackets args
(Link.from_path (path :> Paths.Path.t))
| Poly (polyvars, t) ->
O.txt ("'" ^ String.concat " '" polyvars ^ ". ") ++ type_expr t
O.txt ("'" ^ String.concat ~sep:" '" polyvars ^ ". ") ++ type_expr t
| Package pkg ->
enclose ~l:"(" ~r:")"
(O.keyword "module" ++ O.txt " "
Expand Down Expand Up @@ -747,7 +748,7 @@ module Make (Syntax : SYNTAX) = struct
| Closed [] ->
(O.documentedSrc (O.txt "[< "), O.documentedSrc (O.txt " ]"))
| Closed lst ->
let constrs = String.concat " " lst in
let constrs = String.concat ~sep:" " lst in
( O.documentedSrc (O.txt "[< "),
O.documentedSrc (O.txt (" " ^ constrs ^ " ]")) )
in
Expand All @@ -773,14 +774,14 @@ module Make (Syntax : SYNTAX) = struct
| Some Odoc_model.Lang.TypeDecl.Neg -> "-" :: desc
in
let final = if injectivity then "!" :: var_desc else var_desc in
String.concat "" final
String.concat ~sep:"" final
in
O.txt
(match params with
| [] -> ""
| [ x ] -> format_param x |> Syntax.Type.handle_format_params
| lst -> (
let params = String.concat ", " (List.map format_param lst) in
let params = String.concat ~sep:", " (List.map format_param lst) in
(match delim with `parens -> "(" | `brackets -> "[")
^ params
^ match delim with `parens -> ")" | `brackets -> "]"))
Expand Down Expand Up @@ -1077,7 +1078,7 @@ module Make (Syntax : SYNTAX) = struct
| Constraint cst -> continue @@ constraint_ cst
| Comment `Stop ->
let rest =
Utils.skip_until rest ~p:(function
List.skip_until rest ~p:(function
| Lang.ClassSignature.Comment `Stop -> true
| _ -> false)
in
Expand Down Expand Up @@ -1268,7 +1269,7 @@ module Make (Syntax : SYNTAX) = struct
loop rest (List.rev_append items acc_items)
| Comment `Stop ->
let rest =
Utils.skip_until rest ~p:(function
List.skip_until rest ~p:(function
| Lang.Signature.Comment `Stop -> true
| _ -> false)
in
Expand Down Expand Up @@ -1376,18 +1377,19 @@ module Make (Syntax : SYNTAX) = struct
| Some params, sg ->
let sg_doc, content = signature sg in
let params =
Utils.flatmap params ~f:(fun arg ->
let content = functor_parameter arg in
let attr = [ "parameter" ] in
let anchor =
Some
(Url.Anchor.from_identifier (arg.id :> Paths.Identifier.t))
in
let doc = [] in
[
Item.Declaration
{ content; anchor; attr; doc; source_anchor = None };
])
let decl_of_arg arg =
let content = functor_parameter arg in
let attr = [ "parameter" ] in
let anchor =
Some (Url.Anchor.from_identifier (arg.id :> Paths.Identifier.t))
in
let doc = [] in
[
Item.Declaration
{ content; anchor; attr; doc; source_anchor = None };
]
in
List.concat_map decl_of_arg params
in
let prelude = mk_heading ~label:"parameters" "Parameters" :: params
and content = mk_heading ~label:"signature" "Signature" :: content in
Expand Down
42 changes: 0 additions & 42 deletions src/document/utils.ml

This file was deleted.

6 changes: 0 additions & 6 deletions src/document/utils.mli

This file was deleted.

3 changes: 1 addition & 2 deletions src/driver/dune
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,4 @@
logs
logs.fmt
eio_main
sexplib
odoc_utils))
sexplib))
5 changes: 2 additions & 3 deletions src/driver/util.ml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
open Odoc_utils
open Bos

let ( >>= ) = Result.bind

module StringSet = Set.Make (String)
module StringMap = Map.Make (String)

Expand All @@ -26,7 +27,6 @@ let lines_of_process cmd =
(** Opens a file for writing and calls [f]. The destination directory is created
if needed. *)
let with_out_to filename f =
let open ResultMonad in
let filename = Fpath.normalize filename in
OS.Dir.create (Fpath.parent filename) >>= fun _ ->
OS.File.with_oc filename
Expand All @@ -35,6 +35,5 @@ let with_out_to filename f =
Ok ())
()
|> Result.join
>>= fun () -> Ok ()

let cp src dst = assert (lines_of_process Cmd.(v "cp" % src % dst) = [])
1 change: 0 additions & 1 deletion src/loader/odoc_loader.mli
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
open Result
open Odoc_model
open Odoc_model.Paths

Expand Down
11 changes: 6 additions & 5 deletions src/manpage/generator.ml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
open Odoc_utils
module ManLink = Link
open Odoc_document
open Types
Expand Down Expand Up @@ -299,7 +300,7 @@ and inline (l : Inline.t) =
| { Inline.desc = Text s; _ } -> Accum [ s ]
| _ -> Stop_and_keep)
in
str {|%s|} (String.concat "" l) ++ inline rest
str {|%s|} (String.concat ~sep:"" l) ++ inline rest
| Entity e ->
let x = entity e in
x ++ inline rest
Expand Down Expand Up @@ -343,7 +344,7 @@ let table pp { Table.data; align } =
| Default -> "l")
align
in
Align_line (String.concat "" alignment)
Align_line (String.concat ~sep:"" alignment)
in
env "TS" "TE" ""
(str "allbox;" ++ alignment
Expand Down Expand Up @@ -408,7 +409,7 @@ let next_heading, reset_heading =
| 1, n :: _ -> [ n + 1 ]
| i, n :: t -> n :: succ_heading (i - 1) t
in
let print_heading l = String.concat "." @@ List.map string_of_int l in
let print_heading l = String.concat ~sep:"." @@ List.map string_of_int l in
let next level =
let new_heading = succ_heading level !heading_stack in
heading_stack := new_heading;
Expand Down Expand Up @@ -547,7 +548,7 @@ let page p =
let i = Shift.compute ~on_sub p.items in
macro "TH" {|%s 3 "" "Odoc" "OCaml Library"|} p.url.name
++ macro "SH" "Name"
++ str "%s" (String.concat "." @@ Link.for_printing p.url)
++ str "%s" (String.concat ~sep:"." @@ Link.for_printing p.url)
++ macro "SH" "Synopsis" ++ vspace ++ item ~nested:false header
++ macro "SH" "Documentation" ++ vspace ++ macro "nf" ""
++ item ~nested:false i
Expand All @@ -558,7 +559,7 @@ let rec subpage subp =

and render_page (p : Page.t) =
let p = Doctree.Labels.disambiguate_page ~enter_subpages:true p
and children = Utils.flatmap ~f:subpage @@ Subpages.compute p in
and children = List.concat_map subpage (Subpages.compute p) in
let content ppf = Format.fprintf ppf "%a@." Roff.pp (page p) in
let filename = Link.as_filename p.url in
{ Renderer.filename; content; children; path = p.url }
Expand Down
2 changes: 1 addition & 1 deletion src/model/dune
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@
(backend landmarks --auto))
(instrumentation
(backend bisect_ppx))
(libraries result compiler-libs.common odoc-parser odoc_utils))
(libraries compiler-libs.common odoc-parser odoc_utils))
Loading
Loading