Skip to content

Commit d7fb2b7

Browse files
Merge pull request #395 from panglesd/fix-multilines-ocaml
Fix multilines for OCaml block in .mli file
2 parents d23b06b + 9ea92ed commit d7fb2b7

File tree

6 files changed

+66
-4
lines changed

6 files changed

+66
-4
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
#### Changed
88

9+
- Preserve indentation in multiline OCaml blocks in .mli files (#395, @panglesd)
10+
911
#### Deprecated
1012

1113
#### Fixed

lib/block.ml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,15 +158,19 @@ 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 first = Util.Int.min (Misc.hpad_of_lines [ string ]) hpad in
166+
Astring.String.with_index_range string ~first)
167+
strings
164168

165169
let pp_contents ?syntax ppf t =
166170
match (syntax, t.contents) with
167171
| Some Syntax.Mli, [ line ] -> Fmt.pf ppf "%s" line
168172
| Some Syntax.Mli, lines ->
169-
Fmt.pf ppf "@\n%a@\n" (pp_lines syntax t) (List.map lstrip lines)
173+
Fmt.pf ppf "@\n%a@\n" (pp_lines syntax t) (lstrip lines)
170174
| (Some Cram | Some Normal | None), [] -> ()
171175
| (Some Cram | Some Normal | None), _ ->
172176
Fmt.pf ppf "%a\n" (pp_lines syntax t) t.contents

lib/util.ml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,7 @@ module Process = struct
116116
let wait ~pid =
117117
match snd (Unix.waitpid [] pid) with WEXITED n -> n | _ -> 255
118118
end
119+
120+
module Int = struct
121+
let min a b = if a < b then a else b
122+
end

lib/util.mli

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,7 @@ module Process : sig
7777
Exit code is the same as the child process if it exits normally, or 255
7878
otherwise. *)
7979
end
80+
81+
module Int : sig
82+
val min : int -> int -> int
83+
end

test/bin/mdx-test/expect/dune.inc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,18 @@
275275
(alias runtest)
276276
(action (diff multilines/test-case.md multilines.actual)))
277277

278+
(rule
279+
(target multilines-mli.actual)
280+
(deps (package mdx) (source_tree multilines-mli))
281+
(action
282+
(with-stdout-to %{target}
283+
(chdir multilines-mli
284+
(run ocaml-mdx test --output - test-case.mli)))))
285+
286+
(rule
287+
(alias runtest)
288+
(action (diff multilines-mli/test-case.mli multilines-mli.actual)))
289+
278290
(rule
279291
(target non-det.actual)
280292
(deps (package mdx) (source_tree non-det))
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
(**
2+
3+
In OCaml docstring everything is indented with two spaces
4+
5+
Test multi-lines shell commands:
6+
7+
{@sh[
8+
$ for i in `seq 1 10`; do \
9+
> echo $i; \
10+
> done
11+
1
12+
...
13+
10
14+
]}
15+
16+
This works for normal OCaml fragments:
17+
18+
{[
19+
let rec fact = function
20+
| 1 -> 1
21+
| n -> n * fact (n-1)
22+
]}
23+
24+
The formatting for multilines in .mli files is the following:
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
28+
29+
{[
30+
match None with
31+
| None -> ()
32+
| Some a -> match a with
33+
| None -> ()
34+
| Some b -> b
35+
]}
36+
*)

0 commit comments

Comments
 (0)