Skip to content

Commit 95ee0a1

Browse files
authored
Refactor: Use Stdlib instead of Base.Caml (#2260)
* 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.
1 parent 92ba086 commit 95ee0a1

23 files changed

+98
-104
lines changed

bench/bench.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ let tests =
4343
List.map
4444
(fun {name; input_name; kind; source; conf; action} ->
4545
Test.make
46-
~name:(Caml.Format.sprintf "%s (%s)" name input_name)
46+
~name:(Format.sprintf "%s (%s)" name input_name)
4747
( Staged.stage
4848
@@ fun () ->
4949
match action with

bin/ocamlformat-rpc/main.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
(** OCamlFormat-RPC *)
1313

1414
let () =
15-
Caml.at_exit (Format.pp_print_flush Format.err_formatter) ;
16-
Caml.at_exit (Format_.pp_print_flush Format_.err_formatter)
15+
Stdlib.at_exit (Format.pp_print_flush Format.err_formatter) ;
16+
Stdlib.at_exit (Format_.pp_print_flush Format_.err_formatter)
1717

1818
open Cmdliner
1919

@@ -75,4 +75,4 @@ let info =
7575

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

78-
let () = Caml.exit @@ Cmd.eval_result (Cmd.v info rpc_main_t)
78+
let () = Stdlib.exit @@ Cmd.eval_result (Cmd.v info rpc_main_t)

bin/ocamlformat/main.ml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313

1414
open Ocamlformat_lib ;;
1515

16-
Caml.at_exit (Format.pp_print_flush Format.err_formatter) ;;
16+
Stdlib.at_exit (Format.pp_print_flush Format.err_formatter) ;;
1717

18-
Caml.at_exit (Format_.pp_print_flush Format_.err_formatter)
18+
Stdlib.at_exit (Format_.pp_print_flush Format_.err_formatter)
1919

2020
let format ?output_file ~kind ~input_name ~source (conf : Conf.t) =
2121
if conf.opr_opts.disable.v then Ok source
@@ -101,9 +101,9 @@ let run_action action =
101101
match Bin_conf.action () with
102102
| Ok (`Ok action) -> (
103103
match run_action action with
104-
| Ok () -> Caml.exit 0
104+
| Ok () -> Stdlib.exit 0
105105
| Error errors ->
106106
List.iter errors ~f:(fun error -> error ()) ;
107-
Caml.exit 1 )
108-
| Ok (`Version | `Help) -> Caml.exit 0
109-
| Error _ -> Caml.exit 1
107+
Stdlib.exit 1 )
108+
| Ok (`Version | `Help) -> Stdlib.exit 0
109+
| Error _ -> Stdlib.exit 1

lib/Ast.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -817,7 +817,7 @@ end = struct
817817
ignore (f x) ;
818818
true
819819
with exc ->
820-
let bt = Caml.Printexc.get_backtrace () in
820+
let bt = Stdlib.Printexc.get_backtrace () in
821821
dump x Format.err_formatter ;
822822
Format.eprintf "%s%!" bt ;
823823
raise exc )

lib/Cmts.ml

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

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

14-
module Format = Format_
1514
open Migrate_ast
1615

1716
type layout_cache_key =
@@ -23,10 +22,9 @@ module Layout_cache = struct
2322
module Key = struct
2423
type t = layout_cache_key
2524

26-
let expression_to_string e =
27-
Caml.Format.asprintf "%a" Printast.expression e
25+
let expression_to_string e = Format.asprintf "%a" Printast.expression e
2826

29-
let pattern_to_string e = Caml.Format.asprintf "%a" Printast.pattern e
27+
let pattern_to_string e = Format.asprintf "%a" Printast.pattern e
3028

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

303301
(** Relocate comments, for Ast transformations such as sugaring. *)
304302
let relocate (t : t) ~src ~before ~after =
305303
if t.debug then
306-
Caml.Format.eprintf "relocate %a to %a and %a@\n%!" Location.fmt src
304+
Format.eprintf "relocate %a to %a and %a@\n%!" Location.fmt src
307305
Location.fmt before Location.fmt after ;
308306
let merge_and_sort x y =
309307
List.rev_append x y
@@ -420,19 +418,18 @@ let init fragment ~debug source asts comments_n_docstrings =
420418
; after= get_cmts `After }
421419
in
422420
Printast.cmts := Some cmts ;
423-
Caml.Format.eprintf "AST:\n%a\n%!"
424-
(Extended_ast.Printast.ast fragment)
425-
asts ) ) ;
421+
Format.eprintf "AST:\n%a\n%!" (Extended_ast.Printast.ast fragment) asts
422+
) ) ;
426423
t
427424

428425
let preserve_nomemo f t =
429426
let original = copy t in
430427
let finally () = restore original ~into:t in
431428
Exn.protect ~finally ~f:(fun () ->
432429
let buf = Buffer.create 128 in
433-
let fs = Format.formatter_of_buffer buf in
430+
let fs = Format_.formatter_of_buffer buf in
434431
Fmt.eval fs (f ()) ;
435-
Format.pp_print_flush fs () ;
432+
Format_.pp_print_flush fs () ;
436433
Buffer.contents buf )
437434

438435
let preserve ~cache_key f t =

lib/Cmts.mli

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
removed from the data structure. This is significant in cases where there
2424
are multiple Ast terms with the same location. *)
2525

26-
module Format = Format_
27-
2826
type t
2927

3028
val init :

lib/Fmt.ml

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111

1212
(** Formatting combinators *)
1313

14-
module Format = Format_
15-
1614
(** Define the core type and minimal combinators.
1715
1816
Other higher level functions like [fmt_if] or [list_pn] are implemented
@@ -24,7 +22,7 @@ module T : sig
2422
val ( $ ) : t -> t -> t
2523
(** Sequence *)
2624

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

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

42-
val eval : Format.formatter -> t -> unit
40+
val eval : Format_.formatter -> t -> unit
4341
(** Main function to evaluate a term using an actual formatter. *)
4442
end = struct
45-
type t = (Format.formatter -> unit) Staged.t
43+
type t = (Format_.formatter -> unit) Staged.t
4644

4745
let ( $ ) f g =
4846
let f = Staged.unstage f in
@@ -60,7 +58,7 @@ end = struct
6058
Staged.stage (fun fs ->
6159
try t fs
6260
with exn ->
63-
Format.pp_print_flush fs () ;
61+
Format_.pp_print_flush fs () ;
6462
on_error exn )
6563

6664
let lazy_ f =
@@ -71,14 +69,15 @@ end
7169

7270
include T
7371

74-
type s = (unit, Format.formatter, unit) format
72+
type s = (unit, Format_.formatter, unit) format
7573

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

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

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

8382
let max_indent = ref None
8483

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

8988
let pp_color_k color_code k fs =
90-
let c = Format.sprintf "\x1B[%dm" in
91-
Format.fprintf fs "@<0>%s%t@<0>%s" (c color_code) k (c 0)
89+
let c = Format_.sprintf "\x1B[%dm" in
90+
Format_.fprintf fs "@<0>%s%t@<0>%s" (c color_code) k (c 0)
9291

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

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

9796
let cbreak ~fits ~breaks =
98-
with_pp (fun fs -> Format.pp_print_custom_break fs ~fits ~breaks)
97+
with_pp (fun fs -> Format_.pp_print_custom_break fs ~fits ~breaks)
9998

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

@@ -112,16 +111,16 @@ let sequence l =
112111
in
113112
go l (List.length l)
114113

115-
let fmt f = with_pp (fun fs -> Format.fprintf fs f)
114+
let fmt f = with_pp (fun fs -> Format_.fprintf fs f)
116115

117116
(** Primitive types -----------------------------------------------------*)
118117

119-
let char c = with_pp (fun fs -> Format.pp_print_char fs c)
118+
let char c = with_pp (fun fs -> Format_.pp_print_char fs c)
120119

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

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

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

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

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

179-
let if_newline s = with_pp (fun fs -> Format.pp_print_string_if_newline fs s)
178+
let if_newline s =
179+
with_pp (fun fs -> Format_.pp_print_string_if_newline fs s)
180180

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

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

186186
type behavior = Fit | Break
187187

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

192192
let fits_breaks ?force ?(hint = (0, Int.min_value)) ?(level = 0) fits breaks
193193
=
@@ -251,24 +251,24 @@ let debug_box_open ?name box_kind n fs =
251251
if !box_debug_enabled then (
252252
let name =
253253
match name with
254-
| Some s -> Format.sprintf "%s:%s" box_kind s
254+
| Some s -> Format_.sprintf "%s:%s" box_kind s
255255
| None -> box_kind
256256
in
257-
let openning = if n = 0 then name else Format.sprintf "%s<%d" name n in
257+
let openning = if n = 0 then name else Format_.sprintf "%s<%d" name n in
258258
pp_color_k (box_depth_color ())
259-
(fun fs -> Format.fprintf fs "@<0>[@<0>%s@<0>>" openning)
259+
(fun fs -> Format_.fprintf fs "@<0>[@<0>%s@<0>>" openning)
260260
fs ;
261261
Int.incr box_depth )
262262

263263
let debug_box_close fs =
264264
if !box_debug_enabled then
265265
if !box_depth = 0 then
266266
(* mismatched close, red background *)
267-
pp_color_k 41 (fun fs -> Format.fprintf fs "@<0>]") fs
267+
pp_color_k 41 (fun fs -> Format_.fprintf fs "@<0>]") fs
268268
else (
269269
Int.decr box_depth ;
270270
pp_color_k (box_depth_color ())
271-
(fun fs -> Format.fprintf fs "@<0>]")
271+
(fun fs -> Format_.fprintf fs "@<0>]")
272272
fs )
273273

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

282282
and open_vbox ?name n =
283283
with_pp (fun fs ->
284284
let n = apply_max_indent n in
285285
debug_box_open ?name "v" n fs ;
286-
Format.pp_open_vbox fs n )
286+
Format_.pp_open_vbox fs n )
287287

288288
and open_hvbox ?name n =
289289
with_pp (fun fs ->
290290
let n = apply_max_indent n in
291291
debug_box_open ?name "hv" n fs ;
292-
Format.pp_open_hvbox fs n )
292+
Format_.pp_open_hvbox fs n )
293293

294294
and open_hovbox ?name n =
295295
with_pp (fun fs ->
296296
let n = apply_max_indent n in
297297
debug_box_open ?name "hov" n fs ;
298-
Format.pp_open_hovbox fs n )
298+
Format_.pp_open_hovbox fs n )
299299

300300
and close_box =
301-
with_pp (fun fs -> debug_box_close fs ; Format.pp_close_box fs ())
301+
with_pp (fun fs ->
302+
debug_box_close fs ;
303+
Format_.pp_close_box fs () )
302304

303305
(** Wrapping boxes ------------------------------------------------------*)
304306

lib/Fmt.mli

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@
1111

1212
(** Formatting combinators *)
1313

14-
module Format = Format_
15-
1614
(** Format strings that accept no arguments. *)
17-
type s = (unit, Format.formatter, unit) format
15+
type s = (unit, Format_.formatter, unit) format
1816

1917
(** Format thunks. *)
2018
type t
@@ -45,7 +43,7 @@ val set_margin : int -> t
4543
val set_max_indent : int option -> t
4644
(** Set the maximum indentation. *)
4745

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

5149
val protect : t -> on_error:(exn -> unit) -> t

0 commit comments

Comments
 (0)