diff --git a/lib/dune b/lib/dune index b0e061b..8b69147 100644 --- a/lib/dune +++ b/lib/dune @@ -2,6 +2,7 @@ (public_name pretty_expressive) (name pretty_expressive) (modules (:standard \ prelude)) + ; (libraries batteries) (modules_without_implementation signature)) (mdx (files :standard *.mli) diff --git a/lib/printer.ml b/lib/printer.ml index 5178cdf..14c3ff9 100644 --- a/lib/printer.ml +++ b/lib/printer.ml @@ -341,6 +341,7 @@ module MakeCompat (C : Signature.CostFactory): (Signature.PrinterCompatT with ty let (<>) = (^^) end +(* $MDX part-begin=default_cost_factory *) let default_cost_factory ~page_width ?computation_width () = (module struct type t = int * int @@ -369,3 +370,4 @@ let default_cost_factory ~page_width ?computation_width () = let debug (o, h) = Printf.sprintf "(%d %d)" o h end: Signature.CostFactory with type t = int * int) +(* $MDX part-end *) diff --git a/lib/printer.mli b/lib/printer.mli index 3a167d5..ad4ecba 100644 --- a/lib/printer.mli +++ b/lib/printer.mli @@ -23,33 +23,33 @@ val default_cost_factory : page_width:int -> ?computation_width:int -> unit -> Internally, [default_cost_factory] is defined as: - {@ocaml skip[ - let default_cost_factory ~page_width ?computation_width () = - (module struct - type t = int * int - let limit = match computation_width with - | None -> (float_of_int page_width) *. 1.2 |> int_of_float - | Some computation_width -> computation_width - - let text pos len = - let stop = pos + len in - if stop > page_width then - let maxwc = max page_width pos in - let a = maxwc - page_width in - let b = stop - maxwc in - (b * (2*a + b), 0) - else - (0, 0) - - let newline _ = (0, 1) - - let combine (o1, h1) (o2, h2) = - (o1 + o2, h1 + h2) - - let le (o1, h1) (o2, h2) = - if o1 = o2 then h1 <= h2 else o1 < o2 - - let debug (o, h) = Printf.sprintf "(%d %d)" o h - - end: Signature.CostFactory with type t = int * int) - ]} *) + {@ocaml file=printer.ml,part=default_cost_factory[ +let default_cost_factory ~page_width ?computation_width () = + (module struct + type t = int * int + let limit = match computation_width with + | None -> (float_of_int page_width) *. 1.2 |> int_of_float + | Some computation_width -> computation_width + + let text pos len = + let stop = pos + len in + if stop > page_width then + let maxwc = max page_width pos in + let a = maxwc - page_width in + let b = stop - maxwc in + (b * (2*a + b), 0) + else + (0, 0) + + let newline _ = (0, 1) + + let combine (o1, h1) (o2, h2) = + (o1 + o2, h1 + h2) + + let le (o1, h1) (o2, h2) = + if o1 = o2 then h1 <= h2 else o1 < o2 + + let debug (o, h) = Printf.sprintf "(%d %d)" o h + + end: Signature.CostFactory with type t = int * int) +]} *) diff --git a/test/pretty_expressive.ml b/test/pretty_expressive.ml index b309bae..3a228c1 100644 --- a/test/pretty_expressive.ml +++ b/test/pretty_expressive.ml @@ -30,42 +30,42 @@ let print_doc_group (w : int) = let test_choice_doc_80 () = Alcotest.(check string) "same string" (String.concat "\n" - [ "while (true) {" - ; " f();" - ; " if (done()) exit();" - ; "}" + [ "while (true) {" ; + " f();" ; + " if (done()) exit();" ; + "}" ]) (print_doc_choice 80) let test_choice_doc_20 () = Alcotest.(check string) "same string" (String.concat "\n" - [ "while (true) {" - ; " f();" - ; " if (done())" - ; " exit();" - ; "}" + [ "while (true) {" ; + " f();" ; + " if (done())" ; + " exit();" ; + "}" ]) (print_doc_choice 20) let test_group_doc_80 () = Alcotest.(check string) "same string" (String.concat "\n" - [ "while (true) {" - ; " f();" - ; " if (done()) exit();" - ; "}" + [ "while (true) {" ; + " f();" ; + " if (done()) exit();" ; + "}" ]) (print_doc_group 80) let test_group_doc_20 () = Alcotest.(check string) "same string" (String.concat "\n" - [ "while (true) {" - ; " f();" - ; " if (done())" - ; " exit();" - ; "}" + [ "while (true) {" ; + " f();" ; + " if (done())" ; + " exit();" ; + "}" ]) (print_doc_group 20) @@ -101,19 +101,19 @@ let example_sexp = List [Atom "a"; Atom "b"; Atom "c"; Atom "d"] let test_sexp_4 () = Alcotest.(check string) "same string" (String.concat "\n" - [ "(a" - ; " b" - ; " c" - ; " d)" + [ "(a" ; + " b" ; + " c" ; + " d)" ]) (print_sexp example_sexp 4) let test_sexp_6 () = Alcotest.(check string) "same string" (String.concat "\n" - [ "(a b" - ; " c" - ; " d)" + [ "(a b" ; + " c" ; + " d)" ]) (print_sexp example_sexp 6) @@ -124,13 +124,13 @@ let test_sexp_10 () = (print_sexp example_sexp 10) let suite = - [ "choice; w = 80", `Quick, test_choice_doc_80 - ; "choice; w = 20", `Quick, test_choice_doc_20 - ; "group; w = 80", `Quick, test_group_doc_80 - ; "group; w = 20", `Quick, test_group_doc_20 - ; "sexp; w = 4", `Quick, test_sexp_4 - ; "sexp; w = 6", `Quick, test_sexp_6 - ; "sexp; w = 10", `Quick, test_sexp_10 + [ "choice; w = 80", `Quick, test_choice_doc_80 ; + "choice; w = 20", `Quick, test_choice_doc_20 ; + "group; w = 80", `Quick, test_group_doc_80 ; + "group; w = 20", `Quick, test_group_doc_20 ; + "sexp; w = 4", `Quick, test_sexp_4 ; + "sexp; w = 6", `Quick, test_sexp_6 ; + "sexp; w = 10", `Quick, test_sexp_10 ] let () =