Skip to content

Commit 7b76ef4

Browse files
committed
odoc: Don't fail for removed whitespaces in code blocks
During normalisation, allow whitespaces to disappear from code blocks. This happens when code if formatted.
1 parent 60de7b2 commit 7b76ef4

File tree

2 files changed

+48
-45
lines changed

2 files changed

+48
-45
lines changed

lib/Docstring.ml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,14 @@ let list f fmt l =
7979
let pp_sep _ () = () in
8080
Format.pp_print_list ~pp_sep f fmt l
8181

82-
let str fmt s =
83-
let pp_sep fmt () = Format.pp_print_string fmt " " in
82+
let str_with_sep ~pp_sep fmt s =
8483
Format.pp_print_list ~pp_sep Astring.String.Sub.pp fmt
8584
(normalize_text_subs s)
8685

86+
let str fmt s =
87+
let pp_sep fmt () = Format.pp_print_string fmt " " in
88+
str_with_sep ~pp_sep fmt s
89+
8790
let ign_loc f fmt with_loc = f fmt with_loc.Odoc_parser.Loc.value
8891

8992
let fpf = Format.fprintf
@@ -154,7 +157,8 @@ let rec odoc_nestable_block_element c fmt : Ast.nestable_block_element -> _ =
154157
m.tags
155158
in
156159
let fmt_content =
157-
ign_loc (fun fmt s -> str fmt (c.normalize_code s))
160+
let pp_sep _ () = () in
161+
ign_loc (fun fmt s -> str_with_sep ~pp_sep fmt (c.normalize_code s))
158162
in
159163
let fmt_output =
160164
option (list (ign_loc (odoc_nestable_block_element c)))

test/passing/refs.default/repl.mli.ref

Lines changed: 41 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,105 +3,104 @@ type t = k
33

44
Block delimiters should be on their own line:
55
{[
6-
let x = 1
6+
let x = 1
77
]}
88

99
As of odoc 2.1, a block can carry metadata:
1010
{@ocaml[
11-
let x = 2
11+
let x = 2
1212
]}
1313

1414
An OCaml block that should break:
1515
{[
16-
let x = 2 in
17-
x + x
16+
let x = 2 in
17+
x + x
1818
]}
1919

2020
A toplevel phrase with no output:
2121
{[
22-
# let x = 2 and y = 3 in
23-
x + y
24-
;;
22+
# let x = 2 and y = 3 in
23+
x + y
24+
;;
2525
]}
2626

2727
A toplevel phrase with output:
2828
{@ocaml[
29-
# let x = 2;;
30-
val x : int = 2
29+
# let x = 2;;
30+
val x : int = 2
3131
]}
3232

3333
Many toplevel phrases without output:
3434
{[
35-
# let x = 2;;
36-
# x + 2;;
37-
# let x = 2 and y = 3 in
38-
x + y
39-
;;
35+
# let x = 2;;
36+
# x + 2;;
37+
# let x = 2 and y = 3 in
38+
x + y
39+
;;
4040
]}
4141

4242
Many toplevel phrases with output:
4343
{[
44-
# let x = 2;;
45-
val x : int = 2
46-
# x + 2;;
47-
- : int = 4
48-
# let x = 2 and y = 3 in
49-
x + y
50-
;;
44+
# let x = 2;;
45+
val x : int = 2
46+
# x + 2;;
47+
- : int = 4
48+
# let x = 2 and y = 3 in
49+
x + y
50+
;;
5151
]}
5252

5353
Output are printed after a newline:
5454
{[
55-
# let x = 2;; val x : int = 2
56-
# let x = 3;;
57-
# let x = 4;; val x : int = 4
55+
# let x = 2;; val x : int = 2
56+
# let x = 3;;
57+
# let x = 4;; val x : int = 4
5858
]}
5959

6060
Excessive linebreaks are removed:
6161
{[
62-
# let x = 2 in
63-
x + 1
64-
;;
62+
63+
# let x = 2 in x+1;;
64+
6565
output
66-
# let y = 3 in
67-
y + 1
68-
;;
66+
67+
# let y = 3 in y+1;;
6968
]}
7069

7170
Linebreak after `#`:
7271
{[
73-
# let x = 2 in
74-
x + 1
75-
;;
72+
# let x = 2 in
73+
x + 1
74+
;;
7675
]} *)
7776

7877
(** INVALID BLOCKS: The formatting of invalid blocks is preserved.
7978

8079
Invalid toplevel phrase/ocaml block:
8180
{[
82-
- : int =
83-
4
81+
- : int =
82+
4
8483
]}
8584

8685
Output before a toplevel phrase:
8786
{[
88-
- : int = 4
89-
# 2+2;;
87+
- : int = 4
88+
# 2+2;;
9089
]}
9190

9291
No `;;` at the end of the phrase, no output:
9392
{[
94-
# let x = 2 in x+1
93+
# let x = 2 in x+1
9594
]}
9695

9796
No `;;` at the end of the phrase, with output:
9897
{[
99-
# let x = 2 in x+1
100-
some output
98+
# let x = 2 in x+1
99+
some output
101100
]}
102101

103102
Multiple phrases without `;;` at the end:
104103
{[
105-
# let x = 2 in x+1
106-
# let x = 4 in x+1
104+
# let x = 2 in x+1
105+
# let x = 4 in x+1
107106
]} *)

0 commit comments

Comments
 (0)