diff --git a/.ocamlformat b/.ocamlformat index 0fc5459..7a64b1e 100644 --- a/.ocamlformat +++ b/.ocamlformat @@ -1,4 +1,4 @@ -version=0.14.2 +version=0.17.0 break-separators=before dock-collection-brackets=false break-sequences=true diff --git a/src/pp.ml b/src/pp.ml index 91673e6..0227704 100644 --- a/src/pp.ml +++ b/src/pp.ml @@ -108,7 +108,7 @@ let rec filter_map_tags t ~f = let t = filter_map_tags t ~f in match f tag with | None -> t - | Some tag -> Tag (tag, t) ) + | Some tag -> Tag (tag, t)) | Format f -> Format f module Render = struct @@ -224,10 +224,10 @@ let chain l ~f = box ~indent:3 (seq (verbatim - ( if i = 0 then + (if i = 0 then " " else - "-> " )) + "-> ")) (f x))))) module O = struct diff --git a/src/pp.mli b/src/pp.mli index 4b64844..f625ad3 100644 --- a/src/pp.mli +++ b/src/pp.mli @@ -177,16 +177,15 @@ val to_fmt_with_tags : (** Inject a classic formatter in a document. - Disclaimer: this function is to meant to help using [Pp] in - existing code that already use the [Format] module without having - to port everything to [Pp]. It is not meant as the normal way to - create [Pp.t] values. -*) + Disclaimer: this function is to meant to help using [Pp] in existing code + that already use the [Format] module without having to port everything to + [Pp]. It is not meant as the normal way to create [Pp.t] values. *) val of_fmt : (Format.formatter -> 'a -> unit) -> 'a -> _ t (** {1 Ast} *) module Ast : sig + (** Stable representation useful for serialization *) type 'a t = | Nop | Seq of 'a t * 'a t @@ -204,6 +203,9 @@ module Ast : sig | Tag of 'a * 'a t end +(** [of_ast t] [Ast.t] to [Pp.t] *) val of_ast : 'a Ast.t -> 'a t +(** [to_ast t] will try to convert [t] to [Ast.t]. When [t] contains values + constructed with [of_fmt], this function will fail and return [Error ()] *) val to_ast : 'a t -> ('a Ast.t, unit) result diff --git a/test/tests.ml b/test/tests.ml index cdea0ba..c78afc0 100644 --- a/test/tests.ml +++ b/test/tests.ml @@ -159,7 +159,7 @@ let%expect_test _ = let%expect_test _ = print (Pp.vbox - ( Pp.box (Pp.text "Error: something went wrong!") + (Pp.box (Pp.text "Error: something went wrong!") ++ Pp.cut ++ Pp.box (Pp.text "Here are a few things you can do:") ++ Pp.cut @@ -180,7 +180,7 @@ let%expect_test _ = ; "take a break from your keyboard" ; "clear your head and try again" ] - ] )); + ])); [%expect {| Error: something went wrong! @@ -200,24 +200,24 @@ let%expect_test _ = let%expect_test _ = print (Pp.hovbox ~indent:2 - ( Array.make 50 (Pp.char 'x') + (Array.make 50 (Pp.char 'x') |> Array.to_list |> Pp.concat - ~sep:(Pp.custom_break ~fits:("", 2, "") ~breaks:(" \\", -1, "")) )); + ~sep:(Pp.custom_break ~fits:("", 2, "") ~breaks:(" \\", -1, "")))); [%expect {| x x x x x x x x x x x x x x x x x x x x x x x x x \ x x x x x x x x x x x x x x x x x x x x x x x x x |}] -let pp_pair ppf (a,b) = Format.fprintf ppf "(%i,@ %i)" a b +let pp_pair ppf (a, b) = Format.fprintf ppf "(%i,@ %i)" a b let%expect_test _ = - print ( - Pp.text "hello" ++ Pp.newline ++ - Pp.vbox (Pp.of_fmt pp_pair (1,2)) ++ Pp.space ++ Pp.text "foo" - ); - [%expect{| + print + (Pp.text "hello" ++ Pp.newline + ++ Pp.vbox (Pp.of_fmt pp_pair (1, 2)) + ++ Pp.space ++ Pp.text "foo"); + [%expect {| hello (1, 2)