Skip to content

Commit 76913b3

Browse files
Julowjonludlam
authored andcommitted
Remove document/Utils in favor of Odoc_utils
Remove list operations that were not needed and move skip_until and split_at into Odoc_utils. Document related utils could be moved to Codefmt. This makes the code more consistent regarding List and String manipulations.
1 parent c032c44 commit 76913b3

File tree

8 files changed

+84
-90
lines changed

8 files changed

+84
-90
lines changed

src/document/codefmt.ml

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,27 @@ module State = struct
4242
flush state)
4343
end
4444

45+
let rec compute_length_source (t : Types.Source.t) : int =
46+
let f (acc : int) = function
47+
| Types.Source.Elt t -> acc + compute_length_inline t
48+
| Types.Source.Tag (_, t) -> acc + compute_length_source t
49+
in
50+
List.fold_left f 0 t
51+
52+
and compute_length_inline (t : Types.Inline.t) : int =
53+
let f (acc : int) { Types.Inline.desc; _ } =
54+
match desc with
55+
| Text s -> acc + String.length s
56+
| Entity _e -> acc + 1
57+
| Linebreak -> 0 (* TODO *)
58+
| Styled (_, t) | Link { content = t; _ } -> acc + compute_length_inline t
59+
| Source s -> acc + compute_length_source s
60+
| Math _ -> assert false
61+
| Raw_markup _ -> assert false
62+
(* TODO *)
63+
in
64+
List.fold_left f 0 t
65+
4566
(** Modern implementation using semantic tags, Only for 4.08+ *)
4667

4768
(*
@@ -79,7 +100,7 @@ module Tag = struct
79100
80101
let elt ppf elt =
81102
Format.pp_open_stag ppf (Elt elt);
82-
Format.pp_print_as ppf (Utils.compute_length_inline elt) "";
103+
Format.pp_print_as ppf (compute_length_inline elt) "";
83104
Format.pp_close_stag ppf ()
84105
85106
let ignore ppf txt =
@@ -140,7 +161,7 @@ module Tag = struct
140161

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

145166
let ignore ppf txt = Format.fprintf ppf "@{<ignore-tag>%t@}" txt
146167
end

src/document/comment.ml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1515
*)
1616

17+
open Odoc_utils
1718
open Types
1819
module Comment = Odoc_model.Comment
1920
open Odoc_model.Names
@@ -67,7 +68,7 @@ module Reference = struct
6768
| `TAbsolutePath -> "/"
6869
| `TCurrentPackage -> "//"
6970
in
70-
tag ^ String.concat "/" cs
71+
tag ^ String.concat ~sep:"/" cs
7172

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

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

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

422423
let has_doc docs = docs <> []

src/document/doctree.ml

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
open Odoc_utils
12
open Types
23

34
module Take = struct
@@ -97,19 +98,23 @@ module Subpages : sig
9798
val compute : Page.t -> Subpage.t list
9899
end = struct
99100
let rec walk_documentedsrc (l : DocumentedSrc.t) =
100-
Utils.flatmap l ~f:(function
101-
| DocumentedSrc.Code _ -> []
102-
| Documented _ -> []
103-
| Nested { code; _ } -> walk_documentedsrc code
104-
| Subpage p -> [ p ]
105-
| Alternative (Expansion r) -> walk_documentedsrc r.expansion)
101+
List.concat_map
102+
(function
103+
| DocumentedSrc.Code _ -> []
104+
| Documented _ -> []
105+
| Nested { code; _ } -> walk_documentedsrc code
106+
| Subpage p -> [ p ]
107+
| Alternative (Expansion r) -> walk_documentedsrc r.expansion)
108+
l
106109

107110
let rec walk_items (l : Item.t list) =
108-
Utils.flatmap l ~f:(function
109-
| Item.Text _ -> []
110-
| Heading _ -> []
111-
| Declaration { content; _ } -> walk_documentedsrc content
112-
| Include i -> walk_items i.content.content)
111+
List.concat_map
112+
(function
113+
| Item.Text _ -> []
114+
| Heading _ -> []
115+
| Declaration { content; _ } -> walk_documentedsrc content
116+
| Include i -> walk_items i.content.content)
117+
l
113118

114119
let compute (p : Page.t) = walk_items (p.preamble @ p.items)
115120
end

src/document/generator.ml

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1515
*)
1616

17+
open Odoc_utils
1718
open Odoc_model.Names
1819
module Location = Odoc_model.Location_
1920
module Paths = Odoc_model.Paths
@@ -76,7 +77,7 @@ let mk_heading ?(level = 1) ?label text =
7677
rest is inserted into [items]. *)
7778
let prepare_preamble comment items =
7879
let preamble, first_comment =
79-
Utils.split_at
80+
List.split_at
8081
~f:(function
8182
| { Odoc_model.Location_.value = `Heading _; _ } -> true | _ -> false)
8283
comment
@@ -213,7 +214,7 @@ module Make (Syntax : SYNTAX) = struct
213214
let in_bound x = min (max x 0) (String.length src) in
214215
let a = in_bound a and b = in_bound b in
215216
let a, b = (min a b, max a b) in
216-
String.sub src a (b - a)
217+
String.with_range src ~first:a ~len:(b - a)
217218
in
218219
let plain_code = function
219220
| "" -> []
@@ -358,7 +359,7 @@ module Make (Syntax : SYNTAX) = struct
358359
| Open -> O.txt "[> " ++ elements ++ O.txt " ]"
359360
| Closed [] -> O.txt "[< " ++ elements ++ O.txt " ]"
360361
| Closed lst ->
361-
let constrs = String.concat " " lst in
362+
let constrs = String.concat ~sep:" " lst in
362363
O.txt "[< " ++ elements ++ O.txt (" " ^ constrs ^ " ]"))
363364

364365
and te_object (t : Odoc_model.Lang.TypeExpr.Object.t) =
@@ -461,7 +462,7 @@ module Make (Syntax : SYNTAX) = struct
461462
format_type_path ~delim:`brackets args
462463
(Link.from_path (path :> Paths.Path.t))
463464
| Poly (polyvars, t) ->
464-
O.txt ("'" ^ String.concat " '" polyvars ^ ". ") ++ type_expr t
465+
O.txt ("'" ^ String.concat ~sep:" '" polyvars ^ ". ") ++ type_expr t
465466
| Package pkg ->
466467
enclose ~l:"(" ~r:")"
467468
(O.keyword "module" ++ O.txt " "
@@ -747,7 +748,7 @@ module Make (Syntax : SYNTAX) = struct
747748
| Closed [] ->
748749
(O.documentedSrc (O.txt "[< "), O.documentedSrc (O.txt " ]"))
749750
| Closed lst ->
750-
let constrs = String.concat " " lst in
751+
let constrs = String.concat ~sep:" " lst in
751752
( O.documentedSrc (O.txt "[< "),
752753
O.documentedSrc (O.txt (" " ^ constrs ^ " ]")) )
753754
in
@@ -773,14 +774,14 @@ module Make (Syntax : SYNTAX) = struct
773774
| Some Odoc_model.Lang.TypeDecl.Neg -> "-" :: desc
774775
in
775776
let final = if injectivity then "!" :: var_desc else var_desc in
776-
String.concat "" final
777+
String.concat ~sep:"" final
777778
in
778779
O.txt
779780
(match params with
780781
| [] -> ""
781782
| [ x ] -> format_param x |> Syntax.Type.handle_format_params
782783
| lst -> (
783-
let params = String.concat ", " (List.map format_param lst) in
784+
let params = String.concat ~sep:", " (List.map format_param lst) in
784785
(match delim with `parens -> "(" | `brackets -> "[")
785786
^ params
786787
^ match delim with `parens -> ")" | `brackets -> "]"))
@@ -1077,7 +1078,7 @@ module Make (Syntax : SYNTAX) = struct
10771078
| Constraint cst -> continue @@ constraint_ cst
10781079
| Comment `Stop ->
10791080
let rest =
1080-
Utils.skip_until rest ~p:(function
1081+
List.skip_until rest ~p:(function
10811082
| Lang.ClassSignature.Comment `Stop -> true
10821083
| _ -> false)
10831084
in
@@ -1268,7 +1269,7 @@ module Make (Syntax : SYNTAX) = struct
12681269
loop rest (List.rev_append items acc_items)
12691270
| Comment `Stop ->
12701271
let rest =
1271-
Utils.skip_until rest ~p:(function
1272+
List.skip_until rest ~p:(function
12721273
| Lang.Signature.Comment `Stop -> true
12731274
| _ -> false)
12741275
in
@@ -1376,18 +1377,19 @@ module Make (Syntax : SYNTAX) = struct
13761377
| Some params, sg ->
13771378
let sg_doc, content = signature sg in
13781379
let params =
1379-
Utils.flatmap params ~f:(fun arg ->
1380-
let content = functor_parameter arg in
1381-
let attr = [ "parameter" ] in
1382-
let anchor =
1383-
Some
1384-
(Url.Anchor.from_identifier (arg.id :> Paths.Identifier.t))
1385-
in
1386-
let doc = [] in
1387-
[
1388-
Item.Declaration
1389-
{ content; anchor; attr; doc; source_anchor = None };
1390-
])
1380+
let decl_of_arg arg =
1381+
let content = functor_parameter arg in
1382+
let attr = [ "parameter" ] in
1383+
let anchor =
1384+
Some (Url.Anchor.from_identifier (arg.id :> Paths.Identifier.t))
1385+
in
1386+
let doc = [] in
1387+
[
1388+
Item.Declaration
1389+
{ content; anchor; attr; doc; source_anchor = None };
1390+
]
1391+
in
1392+
List.concat_map decl_of_arg params
13911393
in
13921394
let prelude = mk_heading ~label:"parameters" "Parameters" :: params
13931395
and content = mk_heading ~label:"signature" "Signature" :: content in

src/document/utils.ml

Lines changed: 0 additions & 42 deletions
This file was deleted.

src/document/utils.mli

Lines changed: 0 additions & 6 deletions
This file was deleted.

src/manpage/generator.ml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
open Odoc_utils
12
module ManLink = Link
23
open Odoc_document
34
open Types
@@ -299,7 +300,7 @@ and inline (l : Inline.t) =
299300
| { Inline.desc = Text s; _ } -> Accum [ s ]
300301
| _ -> Stop_and_keep)
301302
in
302-
str {|%s|} (String.concat "" l) ++ inline rest
303+
str {|%s|} (String.concat ~sep:"" l) ++ inline rest
303304
| Entity e ->
304305
let x = entity e in
305306
x ++ inline rest
@@ -343,7 +344,7 @@ let table pp { Table.data; align } =
343344
| Default -> "l")
344345
align
345346
in
346-
Align_line (String.concat "" alignment)
347+
Align_line (String.concat ~sep:"" alignment)
347348
in
348349
env "TS" "TE" ""
349350
(str "allbox;" ++ alignment
@@ -408,7 +409,7 @@ let next_heading, reset_heading =
408409
| 1, n :: _ -> [ n + 1 ]
409410
| i, n :: t -> n :: succ_heading (i - 1) t
410411
in
411-
let print_heading l = String.concat "." @@ List.map string_of_int l in
412+
let print_heading l = String.concat ~sep:"." @@ List.map string_of_int l in
412413
let next level =
413414
let new_heading = succ_heading level !heading_stack in
414415
heading_stack := new_heading;
@@ -547,7 +548,7 @@ let page p =
547548
let i = Shift.compute ~on_sub p.items in
548549
macro "TH" {|%s 3 "" "Odoc" "OCaml Library"|} p.url.name
549550
++ macro "SH" "Name"
550-
++ str "%s" (String.concat "." @@ Link.for_printing p.url)
551+
++ str "%s" (String.concat ~sep:"." @@ Link.for_printing p.url)
551552
++ macro "SH" "Synopsis" ++ vspace ++ item ~nested:false header
552553
++ macro "SH" "Documentation" ++ vspace ++ macro "nf" ""
553554
++ item ~nested:false i
@@ -558,7 +559,7 @@ let rec subpage subp =
558559

559560
and render_page (p : Page.t) =
560561
let p = Doctree.Labels.disambiguate_page ~enter_subpages:true p
561-
and children = Utils.flatmap ~f:subpage @@ Subpages.compute p in
562+
and children = List.concat_map subpage (Subpages.compute p) in
562563
let content ppf = Format.fprintf ppf "%a@." Roff.pp (page p) in
563564
let filename = Link.as_filename p.url in
564565
{ Renderer.filename; content; children; path = p.url }

src/utils/odoc_list.ml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,15 @@ let rec find_map f = function
3232

3333
(* Since 5.1 *)
3434
let is_empty = function [] -> true | _ :: _ -> false
35+
36+
let rec skip_until ~p = function
37+
| [] -> []
38+
| h :: t -> if p h then t else skip_until ~p t
39+
40+
let split_at ~f lst =
41+
let rec loop acc = function
42+
| hd :: _ as rest when f hd -> (List.rev acc, rest)
43+
| [] -> (List.rev acc, [])
44+
| hd :: tl -> loop (hd :: acc) tl
45+
in
46+
loop [] lst

0 commit comments

Comments
 (0)