Skip to content

Commit

Permalink
parser-shims: Vendor Format_doc.pp_two_columns
Browse files Browse the repository at this point in the history
  • Loading branch information
Julow committed Oct 29, 2024
1 parent 1590122 commit 699e4eb
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
29 changes: 29 additions & 0 deletions vendor/parser-shims/ocamlformat_parser_shims.ml
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,32 @@ module Builtin_attributes = struct

let mark_payload_attrs_used _ = ()
end

module Format_doc = struct
open Format

type 'a t = formatter -> 'a -> unit

let compat t ppf x = t ppf x

let pp_two_columns ?(sep = "|") ?max_lines ppf (lines: (string * string) list) =
let left_column_size =
List.fold_left (fun acc (s, _) -> Int.max acc (String.length s)) 0 lines in
let lines_nb = List.length lines in
let ellipsed_first, ellipsed_last =
match max_lines with
| Some max_lines when lines_nb > max_lines ->
let printed_lines = max_lines - 1 in (* the ellipsis uses one line *)
let lines_before = printed_lines / 2 + printed_lines mod 2 in
let lines_after = printed_lines / 2 in
(lines_before, lines_nb - lines_after - 1)
| _ -> (-1, -1)
in
fprintf ppf "@[<v>";
List.iteri (fun k (line_l, line_r) ->
if k = ellipsed_first then fprintf ppf "...@,";
if ellipsed_first <= k && k <= ellipsed_last then ()
else fprintf ppf "%*s %s %s@," left_column_size line_l sep line_r
) lines;
fprintf ppf "@]"
end
6 changes: 6 additions & 0 deletions vendor/parser-shims/ocamlformat_parser_shims.mli
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,9 @@ module Builtin_attributes : sig

val mark_payload_attrs_used : 'a -> unit
end

module Format_doc : sig
type 'a t
val compat : 'a t -> Format.formatter -> 'a -> unit
val pp_two_columns : ?sep : string -> ?max_lines:int -> (string * string) list t
end

0 comments on commit 699e4eb

Please sign in to comment.