Skip to content

Commit

Permalink
Add normalize_spaces parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
gpetiot committed Mar 20, 2023
1 parent 8fabe0d commit b896313
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
18 changes: 10 additions & 8 deletions lib/Fmt_odoc.ml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ open Fmt
open Odoc_parser.Ast
module Loc = Odoc_parser.Loc

type conf = {fmt_code: Fmt.code_formatter}
type conf = {fmt_code: Fmt.code_formatter; normalize_spaces: bool}

(** Escape characters if they are not already escaped. [escapeworthy] should
be [true] if the character should be escaped, [false] otherwise. *)
Expand Down Expand Up @@ -160,10 +160,10 @@ let list_block_elem elems f =

let space_elt : inline_element with_location = Loc.(at (span []) (`Space ""))

let rec fmt_inline_elements elements =
let rec fmt_inline_elements c elements =
let wrap_elements opn cls ~always_wrap hd = function
| [] -> wrap_if always_wrap opn cls hd
| tl -> wrap opn cls (hd $ fmt_inline_elements (space_elt :: tl))
| tl -> wrap opn cls (hd $ fmt_inline_elements c (space_elt :: tl))
in
let rec aux = function
| [] -> noop
Expand Down Expand Up @@ -210,7 +210,7 @@ let rec fmt_inline_elements elements =
aux (List.map elements ~f:(ign_loc ~f:Fn.id))

and fmt_nestable_block_element c = function
| `Paragraph elems -> fmt_inline_elements elems
| `Paragraph elems -> fmt_inline_elements c elems
| `Code_block (s1, s2) -> fmt_code_block c s1 s2
| `Math_block s -> fmt_math_block s
| `Verbatim s -> fmt_verbatim_block s
Expand Down Expand Up @@ -286,18 +286,20 @@ let fmt_block_element c = function
let elems =
if List.is_empty elems then elems else space_elt :: elems
in
hovbox 0 (wrap "{" "}" (str lvl $ lbl $ fmt_inline_elements elems))
hovbox 0 (wrap "{" "}" (str lvl $ lbl $ fmt_inline_elements c elems))
| #nestable_block_element as elm ->
hovbox 0 (fmt_nestable_block_element c elm)

let fmt_ast ~fmt_code (docs : t) =
vbox 0 (list_block_elem docs (fmt_block_element {fmt_code}))
let fmt_ast ?(normalize_spaces = true) ~fmt_code (docs : t) =
let conf = {fmt_code; normalize_spaces} in
vbox 0 (list_block_elem docs (fmt_block_element conf))

let fmt_parsed (conf : Conf.t) ~fmt_code ~input:str_cmt parsed =
let open Fmt in
let normalize_spaces = not conf.fmt_opts.ocp_indent_compat.v in
let fmt_parsed parsed =
fmt_if (String.starts_with_whitespace str_cmt) " "
$ fmt_ast ~fmt_code parsed
$ fmt_ast ~normalize_spaces ~fmt_code parsed
$ fmt_if
(String.length str_cmt > 1 && String.ends_with_whitespace str_cmt)
" "
Expand Down
6 changes: 5 additions & 1 deletion lib/Fmt_odoc.mli
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
(* *)
(**************************************************************************)

val fmt_ast : fmt_code:Fmt.code_formatter -> Odoc_parser.Ast.t -> Fmt.t
val fmt_ast :
?normalize_spaces:bool
-> fmt_code:Fmt.code_formatter
-> Odoc_parser.Ast.t
-> Fmt.t

val fmt_parsed :
Conf.t
Expand Down

0 comments on commit b896313

Please sign in to comment.