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

Allow to safely inject delayed formatters. #1

Merged
2 commits merged into from
Nov 10, 2020
Merged
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
6 changes: 6 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
Next
----

- Add `of_fmt` to compose with existing pretty printers written in `Format`
(#1).

1.0.1
-----

Expand Down
6 changes: 6 additions & 0 deletions src/pp.ml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type +'a t =
| Newline
| Text of string
| Tag of 'a * 'a t
| Format of (Format.formatter -> unit)

let rec map_tags t ~f =
match t with
Expand All @@ -34,6 +35,7 @@ let rec map_tags t ~f =
| Hovbox (indent, t) -> Hovbox (indent, map_tags t ~f)
| (Verbatim _ | Char _ | Break _ | Newline | Text _) as t -> t
| Tag (tag, t) -> Tag (f tag, map_tags t ~f)
| Format f -> Format f

let rec filter_map_tags t ~f =
match t with
Expand All @@ -52,6 +54,7 @@ let rec filter_map_tags t ~f =
match f tag with
| None -> t
| Some tag -> Tag (tag, t) )
| Format f -> Format f

module Render = struct
open Format
Expand Down Expand Up @@ -94,6 +97,7 @@ module Render = struct
| Newline -> pp_force_newline ppf ()
| Text s -> pp_print_text ppf s
| Tag (tag, t) -> tag_handler ppf tag t
| Format f -> f ppf
end

let to_fmt_with_tags = Render.render
Expand Down Expand Up @@ -174,3 +178,5 @@ let chain l ~f =
module O = struct
let ( ++ ) = seq
end

let of_fmt f x = Format (fun ppf -> f ppf x)
11 changes: 11 additions & 0 deletions src/pp.mli
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,14 @@ val to_fmt_with_tags :
-> 'a t
-> tag_handler:(Format.formatter -> 'a -> 'a t -> unit)
-> unit

(** {1 Injection} *)

(** 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.
*)
val of_fmt : (Format.formatter -> 'a -> unit) -> 'a -> _ t
13 changes: 13 additions & 0 deletions test/tests.ml
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,16 @@ let%expect_test _ =
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%expect_test _ =
print (
Pp.text "hello" ++ Pp.newline ++
Pp.vbox (Pp.of_fmt pp_pair (1,2)) ++ Pp.space ++ Pp.text "foo"
);
[%expect{|
hello
(1,
2)
foo |}]