Skip to content

Commit

Permalink
Build on OCaml 5.3 (#2603)
Browse files Browse the repository at this point in the history
Co-authored-by: Adam Cholewiński <[email protected]>
  • Loading branch information
Julow and adamchol authored Oct 29, 2024
1 parent 864d6a5 commit 50946dc
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ profile. This started with version 0.26.0.
- Fix formatting of short `fun` expressions with the janestreet profile (#2593, @Julow)
- Fix missing parentheses around a let in class expressions (#2599, @Julow)
- Fix dropped attribute in `(module M : S [@attr])` (#2602, @Julow)
- Build on OCaml 5.3 (#2603, @adamchol, @Julow)

### Changes
- The location of attributes for structure items is now tracked and preserved. (#2247, @EmileTrotignon)
Expand Down
2 changes: 1 addition & 1 deletion vendor/ocaml-common/location.ml
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ let highlight_quote ppf
Format.fprintf ppf "@}@,"
| _ ->
(* Multi-line error *)
Misc.pp_two_columns ~sep:"|" ~max_lines ppf
Format_doc.compat (Format_doc.pp_two_columns ~sep:"|" ~max_lines) ppf
@@ List.map (fun (line, line_nb, line_start_cnum) ->
let line = String.mapi (fun i car ->
if ISet.mem iset ~pos:(line_start_cnum + i) then car else '.'
Expand Down
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 50946dc

Please sign in to comment.