Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix multilines for OCaml block in .mli file #395

Merged
merged 5 commits into from
Aug 31, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Leonidas-from-XIV marked this conversation as resolved.
Show resolved Hide resolved
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
12 changes: 12 additions & 0 deletions test/bin/mdx-test/expect/dune.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
45 changes: 45 additions & 0 deletions test/bin/mdx-test/expect/multilines-mli/test-case.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
(**

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:
- 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
]}

But it does not work fine for toplevel (see [multilines/test-case.md] for the
Leonidas-from-XIV marked this conversation as resolved.
Show resolved Hide resolved
correct definition of [fact], which get erased by [mdx]):

{[
# let rec fact = function;;
Line 1, characters 24-26:
Error: Syntax error
]}
*)