From d6180a4e8a17b9d7b0d5fb6a9aff8ed6aa6a4fc5 Mon Sep 17 00:00:00 2001 From: Paul-Elliot Date: Tue, 30 Aug 2022 16:49:52 +0200 Subject: [PATCH 1/5] adding test for multilines in `.mli` Signed-off-by: Paul-Elliot --- test/bin/mdx-test/expect/dune.inc | 12 +++++ .../expect/multilines-mli/test-case.mli | 44 +++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 test/bin/mdx-test/expect/multilines-mli/test-case.mli diff --git a/test/bin/mdx-test/expect/dune.inc b/test/bin/mdx-test/expect/dune.inc index 54b10eeba..fb6de43cc 100644 --- a/test/bin/mdx-test/expect/dune.inc +++ b/test/bin/mdx-test/expect/dune.inc @@ -275,6 +275,18 @@ (alias runtest) (action (diff multilines/test-case.md multilines.actual))) +(rule + (target multilines-mli.actual) + (deps (package mdx) (source_tree multilines-mli)) + (action + (with-stdout-to %{target} + (chdir multilines-mli + (run ocaml-mdx test --output - test-case.mli))))) + +(rule + (alias runtest) + (action (diff multilines-mli/test-case.mli multilines-mli.actual))) + (rule (target non-det.actual) (deps (package mdx) (source_tree non-det)) diff --git a/test/bin/mdx-test/expect/multilines-mli/test-case.mli b/test/bin/mdx-test/expect/multilines-mli/test-case.mli new file mode 100644 index 000000000..cafa05e22 --- /dev/null +++ b/test/bin/mdx-test/expect/multilines-mli/test-case.mli @@ -0,0 +1,44 @@ +(** + +In OCaml docstring everything is indented with two spaces + +Test multi-lines shell commands: + +{@sh[ + $ for i in `seq 1 10`; do \ + > echo $i; \ + > done + 1 + ... + 10 +]} + +This works for normal OCaml fragments: + +{[ + let rec fact = function + | 1 -> 1 + | n -> n * fact (n-1) +]} + +The formatting for multilines in .mli files is the following: +- Everything is indented with two spaces after the column of the opening code + block. + + {[ + match None with + | None -> () + | Some a -> match a with + | None -> () + | Some b -> b + ]} + +But it does not work fine for toplevel (see [multilines/test-case.md] for the +correct definition of [fact]): + +{[ + # let rec fact = function;; + Line 1, characters 24-26: + Error: Syntax error +]} +*) From 907eb6567283a33ecb8b29b8934c9ca28cdbe717 Mon Sep 17 00:00:00 2001 From: Paul-Elliot Date: Tue, 30 Aug 2022 16:53:28 +0200 Subject: [PATCH 2/5] fix multiline indentation for ocaml blocks in .mli files Signed-off-by: Paul-Elliot --- lib/block.ml | 13 +++++++++---- .../mdx-test/expect/multilines-mli/test-case.mli | 11 ++++++----- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/lib/block.ml b/lib/block.ml index 4a53ce329..cdcece6d2 100644 --- a/lib/block.ml +++ b/lib/block.ml @@ -158,15 +158,20 @@ let pp_lines syntax t = in Fmt.(list ~sep:(any "\n") pp) -let lstrip string = - let hpad = Misc.hpad_of_lines [ string ] in - Astring.String.with_index_range string ~first:hpad +let lstrip strings = + let hpad = Misc.hpad_of_lines strings in + List.map + (fun string -> + let min a b = if a < b then a else b in + let first = min (Misc.hpad_of_lines [ string ]) hpad in + Astring.String.with_index_range string ~first) + strings let pp_contents ?syntax ppf t = match (syntax, t.contents) with | Some Syntax.Mli, [ line ] -> Fmt.pf ppf "%s" line | Some Syntax.Mli, lines -> - Fmt.pf ppf "@\n%a@\n" (pp_lines syntax t) (List.map lstrip lines) + Fmt.pf ppf "@\n%a@\n" (pp_lines syntax t) (lstrip lines) | (Some Cram | Some Normal | None), [] -> () | (Some Cram | Some Normal | None), _ -> Fmt.pf ppf "%a\n" (pp_lines syntax t) t.contents diff --git a/test/bin/mdx-test/expect/multilines-mli/test-case.mli b/test/bin/mdx-test/expect/multilines-mli/test-case.mli index cafa05e22..93aaeb6d2 100644 --- a/test/bin/mdx-test/expect/multilines-mli/test-case.mli +++ b/test/bin/mdx-test/expect/multilines-mli/test-case.mli @@ -22,19 +22,20 @@ This works for normal OCaml fragments: ]} The formatting for multilines in .mli files is the following: -- Everything is indented with two spaces after the column of the opening code - block. +- The first line is indented two spaces after the comment opening +- The other lines are indented to keep the original indentation relative to the + first line {[ match None with | None -> () | Some a -> match a with - | None -> () - | Some b -> b + | None -> () + | Some b -> b ]} But it does not work fine for toplevel (see [multilines/test-case.md] for the -correct definition of [fact]): +correct definition of [fact], which get erased by [mdx]): {[ # let rec fact = function;; From 7825f9aa2bbf2913831a7956907bb2ed0ae47d2d Mon Sep 17 00:00:00 2001 From: Paul-Elliot Date: Wed, 31 Aug 2022 12:35:19 +0200 Subject: [PATCH 3/5] Adding `Util.min` to the `Util` module `Int.min` was introduced in OCaml 4.13 Signed-off-by: Paul-Elliot --- lib/block.ml | 3 +-- lib/util.ml | 4 ++++ lib/util.mli | 4 ++++ 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/block.ml b/lib/block.ml index cdcece6d2..b7a00c4d9 100644 --- a/lib/block.ml +++ b/lib/block.ml @@ -162,8 +162,7 @@ let lstrip strings = let hpad = Misc.hpad_of_lines strings in List.map (fun string -> - let min a b = if a < b then a else b in - let first = min (Misc.hpad_of_lines [ string ]) hpad in + let first = Util.Int.min (Misc.hpad_of_lines [ string ]) hpad in Astring.String.with_index_range string ~first) strings diff --git a/lib/util.ml b/lib/util.ml index b8c8d342c..33d87efe7 100644 --- a/lib/util.ml +++ b/lib/util.ml @@ -116,3 +116,7 @@ module Process = struct let wait ~pid = match snd (Unix.waitpid [] pid) with WEXITED n -> n | _ -> 255 end + +module Int = struct + let min a b = if a < b then a else b +end diff --git a/lib/util.mli b/lib/util.mli index ab07c034d..e603999fa 100644 --- a/lib/util.mli +++ b/lib/util.mli @@ -77,3 +77,7 @@ module Process : sig Exit code is the same as the child process if it exits normally, or 255 otherwise. *) end + +module Int : sig + val min : int -> int -> int +end From cc3e00d84de3debc276d986b667648983c7f91f6 Mon Sep 17 00:00:00 2001 From: Paul-Elliot Date: Wed, 31 Aug 2022 12:36:07 +0200 Subject: [PATCH 4/5] Remove test that mdx is failing in toplevel multilines in mli Signed-off-by: Paul-Elliot --- test/bin/mdx-test/expect/multilines-mli/test-case.mli | 9 --------- 1 file changed, 9 deletions(-) diff --git a/test/bin/mdx-test/expect/multilines-mli/test-case.mli b/test/bin/mdx-test/expect/multilines-mli/test-case.mli index 93aaeb6d2..a2b92a74d 100644 --- a/test/bin/mdx-test/expect/multilines-mli/test-case.mli +++ b/test/bin/mdx-test/expect/multilines-mli/test-case.mli @@ -33,13 +33,4 @@ The formatting for multilines in .mli files is the following: | None -> () | Some b -> b ]} - -But it does not work fine for toplevel (see [multilines/test-case.md] for the -correct definition of [fact], which get erased by [mdx]): - -{[ - # let rec fact = function;; - Line 1, characters 24-26: - Error: Syntax error -]} *) From 9ea92edd71b03b58388faffb462d335b582dd036 Mon Sep 17 00:00:00 2001 From: Paul-Elliot Date: Wed, 31 Aug 2022 12:37:00 +0200 Subject: [PATCH 5/5] Adding changelog entry for #395 Signed-off-by: Paul-Elliot --- CHANGES.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index a10ab881c..57a3372fd 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -6,6 +6,8 @@ #### Changed +- Preserve indentation in multiline OCaml blocks in .mli files (#395, @panglesd) + #### Deprecated #### Fixed