Skip to content

Commit 907eb65

Browse files
committed
fix multiline indentation for ocaml blocks in .mli files
Signed-off-by: Paul-Elliot <[email protected]>
1 parent d6180a4 commit 907eb65

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

lib/block.ml

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,15 +158,20 @@ let pp_lines syntax t =
158158
in
159159
Fmt.(list ~sep:(any "\n") pp)
160160

161-
let lstrip string =
162-
let hpad = Misc.hpad_of_lines [ string ] in
163-
Astring.String.with_index_range string ~first:hpad
161+
let lstrip strings =
162+
let hpad = Misc.hpad_of_lines strings in
163+
List.map
164+
(fun string ->
165+
let min a b = if a < b then a else b in
166+
let first = min (Misc.hpad_of_lines [ string ]) hpad in
167+
Astring.String.with_index_range string ~first)
168+
strings
164169

165170
let pp_contents ?syntax ppf t =
166171
match (syntax, t.contents) with
167172
| Some Syntax.Mli, [ line ] -> Fmt.pf ppf "%s" line
168173
| Some Syntax.Mli, lines ->
169-
Fmt.pf ppf "@\n%a@\n" (pp_lines syntax t) (List.map lstrip lines)
174+
Fmt.pf ppf "@\n%a@\n" (pp_lines syntax t) (lstrip lines)
170175
| (Some Cram | Some Normal | None), [] -> ()
171176
| (Some Cram | Some Normal | None), _ ->
172177
Fmt.pf ppf "%a\n" (pp_lines syntax t) t.contents

test/bin/mdx-test/expect/multilines-mli/test-case.mli

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,20 @@ This works for normal OCaml fragments:
2222
]}
2323
2424
The formatting for multilines in .mli files is the following:
25-
- Everything is indented with two spaces after the column of the opening code
26-
block.
25+
- The first line is indented two spaces after the comment opening
26+
- The other lines are indented to keep the original indentation relative to the
27+
first line
2728
2829
{[
2930
match None with
3031
| None -> ()
3132
| Some a -> match a with
32-
| None -> ()
33-
| Some b -> b
33+
| None -> ()
34+
| Some b -> b
3435
]}
3536
3637
But it does not work fine for toplevel (see [multilines/test-case.md] for the
37-
correct definition of [fact]):
38+
correct definition of [fact], which get erased by [mdx]):
3839
3940
{[
4041
# let rec fact = function;;

0 commit comments

Comments
 (0)