Skip to content

Update vendored odoc-parser to version 2.4 #2631

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

Merged
merged 9 commits into from
Nov 27, 2024
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 @@ -56,6 +56,8 @@ profile. This started with version 0.26.0.
- Added back the flag `--disable-outside-detected-project` (#2439, @gpetiot)
It was removed in version 0.22.

- Support newer Odoc syntax (#2631, @Julow)

### Changed

- \* Consistent formatting of comments (#2371, #2550, @Julow)
Expand Down
44 changes: 36 additions & 8 deletions lib/Docstring.ml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
(* *)
(**************************************************************************)

module Ast = Ocamlformat_odoc_parser.Ast
module Odoc_parser = Ocamlformat_odoc_parser.Odoc_parser

let parse ~loc text =
Expand Down Expand Up @@ -56,8 +57,6 @@ let odoc_reference = ign_loc str

let option f fmt = function Some v -> f fmt v | None -> ()

let pair fmt_a fmt_b fmt (a, b) = fpf fmt "(%a,%a)" fmt_a a fmt_b b

let odoc_style fmt = function
| `Bold -> fpf fmt "Bold"
| `Italic -> fpf fmt "Italic"
Expand Down Expand Up @@ -88,15 +87,31 @@ let rec odoc_inline_element fmt = function
and odoc_inline_elements fmt elems =
list (ign_loc odoc_inline_element) fmt elems

let light_heavy_to_string = function `Light -> "Light" | `Heavy -> "Heavy"

let alignment_to_string = function
| `Left -> "Left"
| `Right -> "Right"
| `Center -> "Center"

let header_data_to_string = function `Header -> "Header" | `Data -> "Data"

let rec odoc_nestable_block_element c fmt = function
| `Paragraph elms -> fpf fmt "Paragraph(%a)" odoc_inline_elements elms
| `Code_block (metadata, txt) ->
let txt = Odoc_parser.Loc.value txt in
let txt = c.normalize_code txt in
let fmt_metadata =
option (pair (ign_loc str) (option (ign_loc str)))
| `Code_block (b : Ast.code_block) ->
let fmt_metadata fmt (m : Ast.code_block_meta) =
fpf fmt "(%a, %a)" (ign_loc str) m.language
(option (ign_loc str))
m.tags
in
fpf fmt "Code_block(%a, %a)" fmt_metadata metadata str txt
let fmt_content =
ign_loc (fun fmt s -> str fmt (c.normalize_code s))
in
let fmt_output =
option (list (ign_loc (odoc_nestable_block_element c)))
in
fpf fmt "Code_block(%a, %a, %a, %a)" (option fmt_metadata) b.meta
(option str) b.delimiter fmt_content b.content fmt_output b.output
| `Math_block txt -> fpf fmt "Math_block(%a)" str txt
| `Verbatim txt -> fpf fmt "Verbatim(%a)" str txt
| `Modules mods -> fpf fmt "Modules(%a)" (list odoc_reference) mods
Expand All @@ -106,6 +121,18 @@ let rec odoc_nestable_block_element c fmt = function
fpf fmt "Item(%a)" (odoc_nestable_block_elements c) elems
in
fpf fmt "List(%s,%a)" ord (list list_item) items
| `Table ((grid, alignment), syntax) ->
let pp_align fmt aln = fpf fmt "%s" (alignment_to_string aln) in
let pp_cell fmt (elems, header) =
fpf fmt "(%a,%s)"
(odoc_nestable_block_elements c)
elems
(header_data_to_string header)
in
let pp_grid = list (list pp_cell) in
let pp_alignment = option (list (option pp_align)) in
fpf fmt "Table((%a,%a),%s)" pp_grid grid pp_alignment alignment
(light_heavy_to_string syntax)

and odoc_nestable_block_elements c fmt elems =
list (ign_loc (odoc_nestable_block_element c)) fmt elems
Expand Down Expand Up @@ -135,6 +162,7 @@ let odoc_tag c fmt = function
| `Inline -> fpf fmt "Inline"
| `Open -> fpf fmt "Open"
| `Closed -> fpf fmt "Closed"
| `Hidden -> fpf fmt "Hidden"

let odoc_block_element c fmt = function
| `Heading (lvl, lbl, content) ->
Expand Down
Loading