Skip to content

Commit e10e63a

Browse files
Julowgpetiot
andauthored
Format Odoc code block metadata fields (#1713)
* Format Odoc code block metadata fields Since `odoc-parser.0.9.0`, Odoc's code blocks have a metadata field: (** {@metadata[ ... ]} *) Co-authored-by: Guillaume Petiot <[email protected]>
1 parent b0664d4 commit e10e63a

File tree

8 files changed

+87
-6
lines changed

8 files changed

+87
-6
lines changed

CHANGES.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848

4949
+ Improve position of `;;` tokens (#1688, @gpetiot)
5050

51-
+ Depend on `odoc-parser` instead of `odoc` (#1683, @kit-ty-kate, @jonludlam)
51+
+ Depend on `odoc-parser` instead of `odoc` (#1683, #1713, @kit-ty-kate, @jonludlam, @julow)
5252
The parser from odoc has been split from the main odoc package and put into its own package, `odoc-parser`.
5353

5454
#### New features

lib/Fmt_odoc.ml

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,17 @@ let fmt_verbatim_block s =
6767
in
6868
hvbox 0 (wrap "{v" "v}" content)
6969

70-
let fmt_code_block conf _s1 s2 =
70+
let fmt_metadata s = str "@" $ str s
71+
72+
let fmt_code_block conf s1 s2 =
73+
let wrap_code x =
74+
str "{"
75+
$ opt s1 (ign_loc ~f:fmt_metadata)
76+
$ fmt "[@;<1 2>" $ x $ fmt "@ ]}"
77+
in
7178
let s2 = Odoc_parser.Loc.value s2 in
7279
match conf.fmt_code s2 with
73-
| Ok formatted -> hvbox 0 (wrap "{[@;<1 2>" "@ ]}" formatted)
80+
| Ok formatted -> hvbox 0 (wrap_code formatted)
7481
| Error () ->
7582
let fmt_line ~first ~last:_ l =
7683
let l = String.rstrip l in
@@ -80,7 +87,7 @@ let fmt_code_block conf _s1 s2 =
8087
in
8188
let lines = String.split_lines s2 in
8289
let box = match lines with _ :: _ :: _ -> vbox 0 | _ -> hvbox 0 in
83-
box (wrap "{[@;<1 2>" "@ ]}" (vbox 0 (list_fl lines fmt_line)))
90+
box (wrap_code (vbox 0 (list_fl lines fmt_line)))
8491

8592
let fmt_code_span s = hovbox 0 (wrap "[" "]" (str (escape_brackets s)))
8693

lib/Normalize.ml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ let fpf = Format.fprintf
6363

6464
let odoc_reference = ign_loc str
6565

66+
let option f fmt = function Some v -> f fmt v | None -> ()
67+
6668
let odoc_style fmt = function
6769
| `Bold -> fpf fmt "Bold"
6870
| `Italic -> fpf fmt "Italic"
@@ -94,7 +96,7 @@ and odoc_inline_elements fmt elems =
9496

9597
let rec odoc_nestable_block_element c fmt = function
9698
| `Paragraph elms -> fpf fmt "Paragraph(%a)" odoc_inline_elements elms
97-
| `Code_block (_, txt) ->
99+
| `Code_block (metadata, txt) ->
98100
let txt = Odoc_parser.Loc.value txt in
99101
let txt =
100102
try
@@ -116,7 +118,7 @@ let rec odoc_nestable_block_element c fmt = function
116118
comments
117119
with _ -> txt
118120
in
119-
fpf fmt "Code_block(%a)" str txt
121+
fpf fmt "Code_block(%a, %a)" (option (ign_loc str)) metadata str txt
120122
| `Verbatim txt -> fpf fmt "Verbatim(%a)" str txt
121123
| `Modules mods -> fpf fmt "Modules(%a)" (list odoc_reference) mods
122124
| `List (ord, _syntax, items) ->

test/passing/tests/doc_comments-after.ml.ref

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,3 +293,17 @@ let _ = ()
293293

294294
(** starts with linebreaks *)
295295
let a = 1
296+
297+
(** {@metadata[ Code block with metadata field ]} *)
298+
299+
(** {@some_tag[
300+
Code block with metadata field. This is a big block that should hopefully break
301+
]} *)
302+
303+
(** {@ocaml[
304+
let _ =
305+
f @@ {aaa= aaa bbb ccc; bbb= aaa bbb ccc; ccc= aaa bbb ccc}
306+
>>= fun () ->
307+
let _ = x in
308+
f @@ g @@ h @@ fun x -> y
309+
]} *)

test/passing/tests/doc_comments-before-except-val.ml.ref

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,3 +293,17 @@ let _ = ()
293293

294294
(** starts with linebreaks *)
295295
let a = 1
296+
297+
(** {@metadata[ Code block with metadata field ]} *)
298+
299+
(** {@some_tag[
300+
Code block with metadata field. This is a big block that should hopefully break
301+
]} *)
302+
303+
(** {@ocaml[
304+
let _ =
305+
f @@ {aaa= aaa bbb ccc; bbb= aaa bbb ccc; ccc= aaa bbb ccc}
306+
>>= fun () ->
307+
let _ = x in
308+
f @@ g @@ h @@ fun x -> y
309+
]} *)

test/passing/tests/doc_comments-before.ml.ref

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,3 +293,17 @@ let _ = ()
293293

294294
(** starts with linebreaks *)
295295
let a = 1
296+
297+
(** {@metadata[ Code block with metadata field ]} *)
298+
299+
(** {@some_tag[
300+
Code block with metadata field. This is a big block that should hopefully break
301+
]} *)
302+
303+
(** {@ocaml[
304+
let _ =
305+
f @@ {aaa= aaa bbb ccc; bbb= aaa bbb ccc; ccc= aaa bbb ccc}
306+
>>= fun () ->
307+
let _ = x in
308+
f @@ g @@ h @@ fun x -> y
309+
]} *)

test/passing/tests/doc_comments.ml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,3 +289,19 @@ let _ = ()
289289
starts with linebreaks
290290
*)
291291
let a = 1
292+
293+
(** {@metadata[ Code block with metadata field ]} *)
294+
295+
(** {@some_tag[ Code block with metadata field. This is a big block that should hopefully break ]} *)
296+
297+
(** {@ocaml[
298+
let _ =
299+
f
300+
@@
301+
{ aaa= aaa bbb ccc
302+
; bbb= aaa bbb ccc
303+
; ccc= aaa bbb ccc }
304+
>>= fun () ->
305+
let _ = x in
306+
f @@ g @@ h @@ fun x -> y
307+
]} *)

test/passing/tests/doc_comments.ml.ref

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,3 +293,17 @@ let _ = ()
293293

294294
(** starts with linebreaks *)
295295
let a = 1
296+
297+
(** {@metadata[ Code block with metadata field ]} *)
298+
299+
(** {@some_tag[
300+
Code block with metadata field. This is a big block that should hopefully break
301+
]} *)
302+
303+
(** {@ocaml[
304+
let _ =
305+
f @@ {aaa= aaa bbb ccc; bbb= aaa bbb ccc; ccc= aaa bbb ccc}
306+
>>= fun () ->
307+
let _ = x in
308+
f @@ g @@ h @@ fun x -> y
309+
]} *)

0 commit comments

Comments
 (0)