Skip to content

Commit

Permalink
Refactor: Use Stdlib instead of Base.Caml (#2260)
Browse files Browse the repository at this point in the history
* Refactor: Use Stdlib instead of Base.Caml

Base.Caml might be removed in a future version of Base.
The Stdlib module is available on all supported versions of OCaml.

* Remove aliases to the Format_ module

Make sure uses of Format_ and Format (Ocamlformat_stdlib) are clear.
  • Loading branch information
Julow authored Mar 3, 2023
1 parent 92ba086 commit 95ee0a1
Show file tree
Hide file tree
Showing 23 changed files with 98 additions and 104 deletions.
2 changes: 1 addition & 1 deletion bench/bench.ml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ let tests =
List.map
(fun {name; input_name; kind; source; conf; action} ->
Test.make
~name:(Caml.Format.sprintf "%s (%s)" name input_name)
~name:(Format.sprintf "%s (%s)" name input_name)
( Staged.stage
@@ fun () ->
match action with
Expand Down
6 changes: 3 additions & 3 deletions bin/ocamlformat-rpc/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
(** OCamlFormat-RPC *)

let () =
Caml.at_exit (Format.pp_print_flush Format.err_formatter) ;
Caml.at_exit (Format_.pp_print_flush Format_.err_formatter)
Stdlib.at_exit (Format.pp_print_flush Format.err_formatter) ;
Stdlib.at_exit (Format_.pp_print_flush Format_.err_formatter)

open Cmdliner

Expand Down Expand Up @@ -75,4 +75,4 @@ let info =

let rpc_main_t = Term.(const Ocamlformat_rpc.run $ const ())

let () = Caml.exit @@ Cmd.eval_result (Cmd.v info rpc_main_t)
let () = Stdlib.exit @@ Cmd.eval_result (Cmd.v info rpc_main_t)
12 changes: 6 additions & 6 deletions bin/ocamlformat/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@

open Ocamlformat_lib ;;

Caml.at_exit (Format.pp_print_flush Format.err_formatter) ;;
Stdlib.at_exit (Format.pp_print_flush Format.err_formatter) ;;

Caml.at_exit (Format_.pp_print_flush Format_.err_formatter)
Stdlib.at_exit (Format_.pp_print_flush Format_.err_formatter)

let format ?output_file ~kind ~input_name ~source (conf : Conf.t) =
if conf.opr_opts.disable.v then Ok source
Expand Down Expand Up @@ -101,9 +101,9 @@ let run_action action =
match Bin_conf.action () with
| Ok (`Ok action) -> (
match run_action action with
| Ok () -> Caml.exit 0
| Ok () -> Stdlib.exit 0
| Error errors ->
List.iter errors ~f:(fun error -> error ()) ;
Caml.exit 1 )
| Ok (`Version | `Help) -> Caml.exit 0
| Error _ -> Caml.exit 1
Stdlib.exit 1 )
| Ok (`Version | `Help) -> Stdlib.exit 0
| Error _ -> Stdlib.exit 1
2 changes: 1 addition & 1 deletion lib/Ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -817,7 +817,7 @@ end = struct
ignore (f x) ;
true
with exc ->
let bt = Caml.Printexc.get_backtrace () in
let bt = Stdlib.Printexc.get_backtrace () in
dump x Format.err_formatter ;
Format.eprintf "%s%!" bt ;
raise exc )
Expand Down
19 changes: 8 additions & 11 deletions lib/Cmts.ml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

(** Placing and formatting comments in a parsetree. *)

module Format = Format_
open Migrate_ast

type layout_cache_key =
Expand All @@ -23,10 +22,9 @@ module Layout_cache = struct
module Key = struct
type t = layout_cache_key

let expression_to_string e =
Caml.Format.asprintf "%a" Printast.expression e
let expression_to_string e = Format.asprintf "%a" Printast.expression e

let pattern_to_string e = Caml.Format.asprintf "%a" Printast.pattern e
let pattern_to_string e = Format.asprintf "%a" Printast.pattern e

let sexp_of_arg_label = function
| Asttypes.Nolabel -> Sexp.Atom "Nolabel"
Expand Down Expand Up @@ -297,13 +295,13 @@ let rec place t loc_tree ?prev_loc ?deep_loc locs cmts =
| None ->
if t.debug then
List.iter (CmtSet.to_list cmts) ~f:(fun {Cmt.txt; _} ->
Format.eprintf "lost: %s@\n%!" txt ) ) ;
Format_.eprintf "lost: %s@\n%!" txt ) ) ;
deep_loc

(** Relocate comments, for Ast transformations such as sugaring. *)
let relocate (t : t) ~src ~before ~after =
if t.debug then
Caml.Format.eprintf "relocate %a to %a and %a@\n%!" Location.fmt src
Format.eprintf "relocate %a to %a and %a@\n%!" Location.fmt src
Location.fmt before Location.fmt after ;
let merge_and_sort x y =
List.rev_append x y
Expand Down Expand Up @@ -420,19 +418,18 @@ let init fragment ~debug source asts comments_n_docstrings =
; after= get_cmts `After }
in
Printast.cmts := Some cmts ;
Caml.Format.eprintf "AST:\n%a\n%!"
(Extended_ast.Printast.ast fragment)
asts ) ) ;
Format.eprintf "AST:\n%a\n%!" (Extended_ast.Printast.ast fragment) asts
) ) ;
t

let preserve_nomemo f t =
let original = copy t in
let finally () = restore original ~into:t in
Exn.protect ~finally ~f:(fun () ->
let buf = Buffer.create 128 in
let fs = Format.formatter_of_buffer buf in
let fs = Format_.formatter_of_buffer buf in
Fmt.eval fs (f ()) ;
Format.pp_print_flush fs () ;
Format_.pp_print_flush fs () ;
Buffer.contents buf )

let preserve ~cache_key f t =
Expand Down
2 changes: 0 additions & 2 deletions lib/Cmts.mli
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
removed from the data structure. This is significant in cases where there
are multiple Ast terms with the same location. *)

module Format = Format_

type t

val init :
Expand Down
58 changes: 30 additions & 28 deletions lib/Fmt.ml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@

(** Formatting combinators *)

module Format = Format_

(** Define the core type and minimal combinators.
Other higher level functions like [fmt_if] or [list_pn] are implemented
Expand All @@ -24,7 +22,7 @@ module T : sig
val ( $ ) : t -> t -> t
(** Sequence *)

val with_pp : (Format.formatter -> unit) -> t
val with_pp : (Format_.formatter -> unit) -> t
(** Use an arbitrary pretty-printing function *)

val protect : t -> on_error:(exn -> unit) -> t
Expand All @@ -39,10 +37,10 @@ module T : sig
See [tests_lazy] in [Test_fmt]. *)

val eval : Format.formatter -> t -> unit
val eval : Format_.formatter -> t -> unit
(** Main function to evaluate a term using an actual formatter. *)
end = struct
type t = (Format.formatter -> unit) Staged.t
type t = (Format_.formatter -> unit) Staged.t

let ( $ ) f g =
let f = Staged.unstage f in
Expand All @@ -60,7 +58,7 @@ end = struct
Staged.stage (fun fs ->
try t fs
with exn ->
Format.pp_print_flush fs () ;
Format_.pp_print_flush fs () ;
on_error exn )

let lazy_ f =
Expand All @@ -71,14 +69,15 @@ end

include T

type s = (unit, Format.formatter, unit) format
type s = (unit, Format_.formatter, unit) format

type sp = Blank | Cut | Space | Break of int * int

let ( >$ ) f g x = f $ g x

let set_margin n =
with_pp (fun fs -> Format.pp_set_geometry fs ~max_indent:n ~margin:(n + 1))
with_pp (fun fs ->
Format_.pp_set_geometry fs ~max_indent:n ~margin:(n + 1) )

let max_indent = ref None

Expand All @@ -87,15 +86,15 @@ let set_max_indent x = with_pp (fun _ -> max_indent := x)
(** Debug of formatting -------------------------------------------------*)

let pp_color_k color_code k fs =
let c = Format.sprintf "\x1B[%dm" in
Format.fprintf fs "@<0>%s%t@<0>%s" (c color_code) k (c 0)
let c = Format_.sprintf "\x1B[%dm" in
Format_.fprintf fs "@<0>%s%t@<0>%s" (c color_code) k (c 0)

(** Break hints and format strings --------------------------------------*)

let break n o = with_pp (fun fs -> Format.pp_print_break fs n o)
let break n o = with_pp (fun fs -> Format_.pp_print_break fs n o)

let cbreak ~fits ~breaks =
with_pp (fun fs -> Format.pp_print_custom_break fs ~fits ~breaks)
with_pp (fun fs -> Format_.pp_print_custom_break fs ~fits ~breaks)

let noop = with_pp (fun _ -> ())

Expand All @@ -112,16 +111,16 @@ let sequence l =
in
go l (List.length l)

let fmt f = with_pp (fun fs -> Format.fprintf fs f)
let fmt f = with_pp (fun fs -> Format_.fprintf fs f)

(** Primitive types -----------------------------------------------------*)

let char c = with_pp (fun fs -> Format.pp_print_char fs c)
let char c = with_pp (fun fs -> Format_.pp_print_char fs c)

let utf8_length s =
Uuseg_string.fold_utf_8 `Grapheme_cluster (fun n _ -> n + 1) 0 s

let str_as n s = with_pp (fun fs -> Format.pp_print_as fs n s)
let str_as n s = with_pp (fun fs -> Format_.pp_print_as fs n s)

let str s = if String.is_empty s then noop else str_as (utf8_length s) s

Expand Down Expand Up @@ -176,18 +175,19 @@ let fmt_opt o = Option.value o ~default:noop

(** Conditional on immediately following a line break -------------------*)

let if_newline s = with_pp (fun fs -> Format.pp_print_string_if_newline fs s)
let if_newline s =
with_pp (fun fs -> Format_.pp_print_string_if_newline fs s)

let break_unless_newline n o =
with_pp (fun fs -> Format.pp_print_or_newline fs n o "" "")
with_pp (fun fs -> Format_.pp_print_or_newline fs n o "" "")

(** Conditional on breaking of enclosing box ----------------------------*)

type behavior = Fit | Break

let fits_or_breaks ~level fits nspaces offset breaks =
with_pp (fun fs ->
Format.pp_print_fits_or_breaks fs ~level fits nspaces offset breaks )
Format_.pp_print_fits_or_breaks fs ~level fits nspaces offset breaks )

let fits_breaks ?force ?(hint = (0, Int.min_value)) ?(level = 0) fits breaks
=
Expand Down Expand Up @@ -251,24 +251,24 @@ let debug_box_open ?name box_kind n fs =
if !box_debug_enabled then (
let name =
match name with
| Some s -> Format.sprintf "%s:%s" box_kind s
| Some s -> Format_.sprintf "%s:%s" box_kind s
| None -> box_kind
in
let openning = if n = 0 then name else Format.sprintf "%s<%d" name n in
let openning = if n = 0 then name else Format_.sprintf "%s<%d" name n in
pp_color_k (box_depth_color ())
(fun fs -> Format.fprintf fs "@<0>[@<0>%s@<0>>" openning)
(fun fs -> Format_.fprintf fs "@<0>[@<0>%s@<0>>" openning)
fs ;
Int.incr box_depth )

let debug_box_close fs =
if !box_debug_enabled then
if !box_depth = 0 then
(* mismatched close, red background *)
pp_color_k 41 (fun fs -> Format.fprintf fs "@<0>]") fs
pp_color_k 41 (fun fs -> Format_.fprintf fs "@<0>]") fs
else (
Int.decr box_depth ;
pp_color_k (box_depth_color ())
(fun fs -> Format.fprintf fs "@<0>]")
(fun fs -> Format_.fprintf fs "@<0>]")
fs )

let apply_max_indent n = Option.value_map !max_indent ~f:(min n) ~default:n
Expand All @@ -277,28 +277,30 @@ let open_box ?name n =
with_pp (fun fs ->
let n = apply_max_indent n in
debug_box_open ?name "b" n fs ;
Format.pp_open_box fs n )
Format_.pp_open_box fs n )

and open_vbox ?name n =
with_pp (fun fs ->
let n = apply_max_indent n in
debug_box_open ?name "v" n fs ;
Format.pp_open_vbox fs n )
Format_.pp_open_vbox fs n )

and open_hvbox ?name n =
with_pp (fun fs ->
let n = apply_max_indent n in
debug_box_open ?name "hv" n fs ;
Format.pp_open_hvbox fs n )
Format_.pp_open_hvbox fs n )

and open_hovbox ?name n =
with_pp (fun fs ->
let n = apply_max_indent n in
debug_box_open ?name "hov" n fs ;
Format.pp_open_hovbox fs n )
Format_.pp_open_hovbox fs n )

and close_box =
with_pp (fun fs -> debug_box_close fs ; Format.pp_close_box fs ())
with_pp (fun fs ->
debug_box_close fs ;
Format_.pp_close_box fs () )

(** Wrapping boxes ------------------------------------------------------*)

Expand Down
6 changes: 2 additions & 4 deletions lib/Fmt.mli
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@

(** Formatting combinators *)

module Format = Format_

(** Format strings that accept no arguments. *)
type s = (unit, Format.formatter, unit) format
type s = (unit, Format_.formatter, unit) format

(** Format thunks. *)
type t
Expand Down Expand Up @@ -45,7 +43,7 @@ val set_margin : int -> t
val set_max_indent : int option -> t
(** Set the maximum indentation. *)

val eval : Format.formatter -> t -> unit
val eval : Format_.formatter -> t -> unit
(** [eval fs t] runs format thunk [t] outputting to [fs] *)

val protect : t -> on_error:(exn -> unit) -> t
Expand Down
Loading

0 comments on commit 95ee0a1

Please sign in to comment.