Skip to content

Commit c35dddb

Browse files
gpetiotjonludlam
andcommitted
Upgrade mdx to use last version of odoc-parser
Co-authored-by: Jon Ludlam <[email protected]>
1 parent 526248a commit c35dddb

File tree

20 files changed

+1028
-125
lines changed

20 files changed

+1028
-125
lines changed

CHANGES.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
### unreleased
2+
3+
#### Added
4+
5+
- Handle the error-blocks syntax (#439, @jonludlam, @gpetiot)
6+
17
### 2.3.1
28

39
#### Added

lib/block.ml

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ type t = {
114114
os_type_enabled : bool;
115115
set_variables : (string * string) list;
116116
unset_variables : string list;
117+
delim : string option;
117118
value : value;
118119
}
119120

@@ -160,19 +161,45 @@ let rec error_padding = function
160161
let xs = error_padding xs in
161162
x :: xs
162163

163-
let pp_errors ppf t =
164+
let pp_error ?syntax ?delim ppf outputs =
165+
match syntax with
166+
| Some Syntax.Markdown ->
167+
Fmt.pf ppf "```\n```mdx-error\n%a\n"
168+
Fmt.(list ~sep:(any "\n") Output.pp)
169+
outputs
170+
| Some Syntax.Mli | Some Syntax.Mld ->
171+
Fmt.pf ppf "]%a[\n{err@mdx-error[\n%a\n]err}"
172+
Fmt.(option string)
173+
delim
174+
Fmt.(list ~sep:(any "\n") Output.pp)
175+
outputs
176+
| _ -> ()
177+
178+
let has_output t =
179+
match t.value with
180+
| OCaml { errors = []; _ } -> false
181+
| OCaml { errors = _; _ } -> true
182+
| _ -> false
183+
184+
let pp_value ?syntax ppf t =
185+
let delim = t.delim in
164186
match t.value with
165187
| OCaml { errors = []; _ } -> ()
166188
| OCaml { errors; _ } ->
167189
let errors = error_padding errors in
168-
Fmt.pf ppf "```mdx-error\n%a\n```\n"
169-
Fmt.(list ~sep:(any "\n") Output.pp)
170-
errors
190+
pp_error ?syntax ?delim ppf errors
171191
| _ -> ()
172192

173-
let pp_footer ?syntax ppf _ =
193+
let pp_footer ?syntax ppf t =
194+
let delim =
195+
if has_output t then (
196+
pp_value ?syntax ppf t;
197+
None)
198+
else t.delim
199+
in
174200
match syntax with
175-
| Some Syntax.Mli | Some Syntax.Mld -> Fmt.string ppf "]}"
201+
| Some Syntax.Mli | Some Syntax.Mld ->
202+
Fmt.pf ppf "]%a}" Fmt.(option string) delim
176203
| Some Syntax.Cram -> Fmt.string ppf "\n"
177204
| Some Syntax.Markdown | None -> Fmt.string ppf "```\n"
178205

@@ -216,7 +243,9 @@ let pp_header ?syntax ppf t =
216243
| [] -> ()
217244
| labels -> Fmt.pf ppf " %a" (pp_labels ?syntax) labels
218245
in
219-
Fmt.pf ppf "{%a%a[" pp_lang_header lang_headers pp_labels other_labels
246+
Fmt.pf ppf "{%a%a%a["
247+
Fmt.(option string)
248+
t.delim pp_lang_header lang_headers pp_labels other_labels
220249
| Some Syntax.Cram -> pp_labels ?syntax ppf t.labels
221250
| Some Syntax.Markdown | None ->
222251
if t.legacy_labels then
@@ -231,8 +260,7 @@ let pp_header ?syntax ppf t =
231260
let pp ?syntax ppf b =
232261
pp_header ?syntax ppf b;
233262
pp_contents ?syntax ppf b;
234-
pp_footer ?syntax ppf b;
235-
pp_errors ppf b
263+
pp_footer ?syntax ppf b
236264

237265
let directory t = t.dir
238266
let file t = match t.value with Include t -> Some t.file_included | _ -> None
@@ -415,7 +443,7 @@ let infer_block ~loc ~config ~header ~contents ~errors =
415443
let+ () = check_no_errors ~loc errors in
416444
Raw { header })
417445

418-
let mk ~loc ~section ~labels ~legacy_labels ~header ~contents ~errors =
446+
let mk ~loc ~section ~labels ~legacy_labels ~header ~delim ~contents ~errors =
419447
let block_kind =
420448
get_label (function Block_kind x -> Some x | _ -> None) labels
421449
in
@@ -442,6 +470,7 @@ let mk ~loc ~section ~labels ~legacy_labels ~header ~contents ~errors =
442470
os_type_enabled;
443471
set_variables = config.set_variables;
444472
unset_variables = config.unset_variables;
473+
delim;
445474
value;
446475
}
447476

@@ -450,7 +479,7 @@ let mk_include ~loc ~section ~labels =
450479
| Some file_inc ->
451480
let header = Header.infer_from_file file_inc in
452481
mk ~loc ~section ~labels ~legacy_labels:false ~header ~contents:[]
453-
~errors:[]
482+
~errors:[] ~delim:None
454483
| None -> label_required ~loc ~label:"file" ~kind:"include"
455484

456485
let parse_labels ~label_cmt ~legacy_labels =
@@ -476,6 +505,7 @@ let from_raw raw =
476505
in
477506
Util.Result.to_error_list
478507
@@ mk ~loc ~section ~header ~contents ~labels ~legacy_labels ~errors
508+
~delim:None
479509

480510
let is_active ?section:s t =
481511
let active =

lib/block.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ type t = {
105105
(** Whether the current os type complies with the block's version. *)
106106
set_variables : (string * string) list;
107107
unset_variables : string list;
108+
delim : string option;
108109
value : value;
109110
}
110111
(** The type for supported code blocks. *)
@@ -115,6 +116,7 @@ val mk :
115116
labels:Label.t list ->
116117
legacy_labels:bool ->
117118
header:Header.t option ->
119+
delim:string option ->
118120
contents:string list ->
119121
errors:Output.t list ->
120122
(t, [ `Msg of string ]) result

lib/mli_parser.ml

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module Code_block = struct
33

44
type t = {
55
metadata : metadata option;
6+
delimiter : string option;
67
content : Location.t; (* Location of the content *)
78
code_block : Location.t; (* Location of the enclosing code block *)
89
}
@@ -44,18 +45,19 @@ let extract_code_block_info acc ~(location : Lexing.position) ~docstring =
4445
loc_ghost = false;
4546
}
4647
in
47-
fun location (metadata, { O.Loc.location = span; _ }) ->
48+
fun location
49+
{ O.Ast.meta; delimiter; content = { O.Loc.location = span; _ }; _ } ->
4850
let metadata =
4951
Option.map
50-
(fun (lang, labels) ->
51-
let language_tag = O.Loc.value lang in
52-
let labels = Option.map O.Loc.value labels in
52+
(fun { O.Ast.language; tags } ->
53+
let language_tag = O.Loc.value language in
54+
let labels = Option.map O.Loc.value tags in
5355
Code_block.{ language_tag; labels })
54-
metadata
56+
meta
5557
in
5658
let content = convert_loc span in
5759
let code_block = convert_loc location in
58-
{ metadata; content; code_block }
60+
{ metadata; delimiter; content; code_block }
5961
in
6062

6163
(* Fold over the results from odoc-parser, recurse where necessary
@@ -146,9 +148,10 @@ let make_block code_block file_contents =
146148
let len = loc.loc_end.pos_cnum - start in
147149
String.sub file_contents start len
148150
in
151+
let delim = code_block.delimiter in
149152
let contents = slice code_block.content |> String.split_on_char '\n' in
150153
Block.mk ~loc:code_block.code_block ~section:None ~labels ~header
151-
~contents ~legacy_labels:false ~errors:[]
154+
~contents ~legacy_labels:false ~errors:[] ~delim
152155
153156
(* Given the locations of the code blocks within [file_contents], then slice it up into
154157
[Text] and [Block] parts by using the starts and ends of those blocks as

test/bin/mdx-test/expect/simple-mld/test-case.mld

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,11 @@ Indentation test:
5959
val x : int = 1
6060
]}
6161

62-
62+
{delim@ocaml[
63+
let f = 1 + "2"
64+
]delim[
65+
{err@mdx-error[
66+
Line 1, characters 15-18:
67+
Error: This expression has type string but an expression was expected of type
68+
int
69+
]err}]}

test/bin/mdx-test/expect/simple-mli/test-case.mli

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,14 @@ val bar : string
4949

5050
(** {@ocaml skip[1 + 1 = 3]} *)
5151
val baz : string
52+
53+
(**
54+
{[
55+
let f = 1 + "2"
56+
][
57+
{err@mdx-error[
58+
Line 1, characters 15-18:
59+
Error: This expression has type string but an expression was expected of type
60+
int
61+
]err}]}
62+
*)

test/lib/test_block.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ let test_mk =
2323
let test_fun () =
2424
let actual =
2525
Mdx.Block.mk ~loc:Location.none ~section:None ~labels
26-
~legacy_labels:false ~header ~contents ~errors:[]
26+
~legacy_labels:false ~header ~contents ~errors:[] ~delim:None
2727
in
2828
let expected =
2929
Result.map_error

test/lib/test_dep.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ let test_of_block =
2626
| Ok labels -> (
2727
match
2828
Mdx.Block.mk ~loc:Location.none ~section:None ~labels ~header:None
29-
~contents:[] ~legacy_labels:false ~errors:[]
29+
~contents:[] ~legacy_labels:false ~errors:[] ~delim:None
3030
with
3131
| Ok block -> block
3232
| Error _ -> assert false)

0 commit comments

Comments
 (0)