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 all 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
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

#### Changed

- Preserve indentation in multiline OCaml blocks in .mli files (#395, @panglesd)

#### Deprecated

#### Fixed
Expand Down
12 changes: 8 additions & 4 deletions lib/block.ml
Original file line number Diff line number Diff line change
Expand Up @@ -158,15 +158,19 @@ 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 first = Util.Int.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
4 changes: 4 additions & 0 deletions lib/util.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions lib/util.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
36 changes: 36 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,36 @@
(**

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
]}
*)