From 7f5a1156b89fd9caf53038d96edc751ed0f33d9c Mon Sep 17 00:00:00 2001 From: Ali Caglayan Date: Tue, 31 Oct 2023 16:34:17 +0000 Subject: [PATCH] fix: type of Pp.paragraphf and add changelog We define the format string internally so that it can be enforced somewhat. Signed-off-by: Ali Caglayan --- CHANGES.md | 2 ++ src/pp.ml | 6 ++++-- src/pp.mli | 2 +- test/tests.ml | 3 +++ 4 files changed, 10 insertions(+), 3 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 69bd112..32e03ff 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -3,6 +3,8 @@ Unreleased - Add `Pp.verbatimf`. (#18, @mbarbin) +- Add `Pp.paragraph` and `Pp.paragraphf` (#19, @Alizter) + - Remove `of_fmt` constructor. (#17, @Alizter) 1.2.0 diff --git a/src/pp.ml b/src/pp.ml index e67d498..afaa719 100644 --- a/src/pp.ml +++ b/src/pp.ml @@ -29,6 +29,8 @@ include Ast let of_ast = Fun.id let to_ast = Fun.id +type ('a, 'tag) format_string = ('a, unit, string, 'tag t) format4 + let rec map_tags t ~f = match t with | Nop -> Nop @@ -145,10 +147,10 @@ let space = break ~nspaces:1 ~shift:0 let cut = break ~nspaces:0 ~shift:0 let newline = Newline let text s = Text s -let textf fmt = Printf.ksprintf text fmt +let textf (fmt : ('a, 'tag) format_string) = Printf.ksprintf text fmt let tag tag t = Tag (tag, t) let paragraph s = hovbox (text s) -let paragraphf fmt = hovbox (textf fmt) +let paragraphf (fmt : ('a, 'tag) format_string) = Printf.ksprintf paragraph fmt let enumerate l ~f = vbox diff --git a/src/pp.mli b/src/pp.mli index 79a35d2..c75b6e7 100644 --- a/src/pp.mli +++ b/src/pp.mli @@ -155,7 +155,7 @@ val paragraph : string -> 'tag t (** [paragraphf s] is [textf s] followed by a [hovbox]. The [textf] version of [paragraph]. *) -val paragraphf : ('tag t, unit, string, 'b t) format4 -> 'tag t +val paragraphf : ('a, unit, string, 'tag t) format4 -> 'a (** [enumerate l ~f] produces an enumeration of the form: diff --git a/test/tests.ml b/test/tests.ml index 42aeeb6..248f990 100644 --- a/test/tests.ml +++ b/test/tests.ml @@ -327,3 +327,6 @@ let%expect_test "paragraph" = -< box + text >----------- Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum euismod, nisl eget aliquam ultricies. |}] + +let%expect_test "paragraphf" = print (Pp.paragraphf "Hello World%s" "!"); + [%expect {| Hello World! |}]