Skip to content

Commit

Permalink
fix multiline indentation for ocaml blocks in .mli files
Browse files Browse the repository at this point in the history
Signed-off-by: Paul-Elliot <peada@free.fr>
  • Loading branch information
panglesd committed Aug 30, 2022
1 parent d6180a4 commit 907eb65
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
13 changes: 9 additions & 4 deletions lib/block.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 6 additions & 5 deletions test/bin/mdx-test/expect/multilines-mli/test-case.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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;;
Expand Down

0 comments on commit 907eb65

Please sign in to comment.