Skip to content

Commit a727b4b

Browse files
authored
Renaming of the 'a with_comments in Translation_unit (#2479)
1 parent d0a28cf commit a727b4b

File tree

5 files changed

+39
-30
lines changed

5 files changed

+39
-30
lines changed

lib/Normalize_extended_ast.ml

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

1212
open Extended_ast
1313

14+
type 'a t = 'a Extended_ast.t
15+
1416
let dedup_cmts fragment ast comments =
1517
let of_ast ast =
1618
let docs = ref (Set.empty (module Cmt)) in

lib/Normalize_extended_ast.mli

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@
99
(* *)
1010
(**************************************************************************)
1111

12-
val dedup_cmts : 'a Extended_ast.t -> 'a -> Cmt.t list -> Cmt.t list
12+
type 'a t = 'a Extended_ast.t
13+
14+
val dedup_cmts : 'a t -> 'a -> Cmt.t list -> Cmt.t list
1315
(** Remove comments that duplicate docstrings (or other comments). *)
1416

15-
val equal :
16-
'a Extended_ast.t -> ignore_doc_comments:bool -> Conf.t -> 'a -> 'a -> bool
17+
val equal : 'a t -> ignore_doc_comments:bool -> Conf.t -> 'a -> 'a -> bool
1718
(** Compare fragments for equality up to normalization. *)
1819

1920
val diff_cmts :

lib/Normalize_std_ast.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
open Parser_standard
1313
open Std_ast
1414

15+
type 'a t = 'a Std_ast.t
16+
1517
let is_doc = function
1618
| {attr_name= {Location.txt= "ocaml.doc" | "ocaml.text"; _}; _} -> true
1719
| _ -> false

lib/Normalize_std_ast.mli

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@
99
(* *)
1010
(**************************************************************************)
1111

12-
val ast : 'a Std_ast.t -> Conf.t -> 'a -> 'a
12+
type 'a t = 'a Std_ast.t
13+
14+
val ast : 'a t -> Conf.t -> 'a -> 'a
1315
(** Normalize an AST fragment. *)
1416

15-
val equal :
16-
'a Std_ast.t -> ignore_doc_comments:bool -> Conf.t -> 'a -> 'a -> bool
17+
val equal : 'a t -> ignore_doc_comments:bool -> Conf.t -> 'a -> 'a -> bool
1718
(** Compare fragments for equality up to normalization. *)
1819

19-
val moved_docstrings : 'a Std_ast.t -> Conf.t -> 'a -> 'a -> Cmt.error list
20+
val moved_docstrings : 'a t -> Conf.t -> 'a -> 'a -> Cmt.error list

lib/Translation_unit.ml

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -230,9 +230,9 @@ let check_comments (conf : Conf.t) cmts ~old:t_old ~new_:t_new =
230230
| Ok () -> ()
231231
| Error e -> internal_error (List.map e ~f:(fun x -> `Comment x)) []
232232

233-
let format (type a b) (fg : a Extended_ast.t) (std_fg : b Std_ast.t)
234-
?output_file ~input_name ~prev_source ~parsed ~std_parsed (conf : Conf.t)
235-
=
233+
let format (type ext std) (ext_fg : ext Extended_ast.t)
234+
(std_fg : std Std_ast.t) ?output_file ~input_name ~prev_source
235+
~ext_parsed ~std_parsed (conf : Conf.t) =
236236
let dump_ast fg ~suffix ast =
237237
if conf.opr_opts.debug.v then
238238
Some
@@ -247,23 +247,24 @@ let format (type a b) (fg : a Extended_ast.t) (std_fg : b Std_ast.t)
247247
in
248248
Location.input_name := input_name ;
249249
(* iterate until formatting stabilizes *)
250-
let rec print_check ~i ~(conf : Conf.t) ~prev_source t std_t =
250+
let rec print_check ~i ~(conf : Conf.t) ~prev_source ext_t std_t =
251251
let format ~box_debug =
252252
let open Fmt in
253253
let cmts_t =
254-
Cmts.init fg ~debug:conf.opr_opts.debug.v t.source t.ast t.comments
254+
Cmts.init ext_fg ~debug:conf.opr_opts.debug.v ext_t.source ext_t.ast
255+
ext_t.comments
255256
in
256257
let contents =
257258
with_buffer_formatter
258259
~buffer_size:(String.length prev_source)
259260
( set_margin conf.fmt_opts.margin.v
260261
$ set_max_indent conf.fmt_opts.max_indent.v
261262
$ fmt_if_k
262-
(not (String.is_empty t.prefix))
263-
(str t.prefix $ fmt "@.")
263+
(not (String.is_empty ext_t.prefix))
264+
(str ext_t.prefix $ fmt "@.")
264265
$ with_optional_box_debug ~box_debug
265-
(Fmt_ast.fmt_ast fg ~debug:conf.opr_opts.debug.v t.source
266-
cmts_t conf t.ast ) )
266+
(Fmt_ast.fmt_ast ext_fg ~debug:conf.opr_opts.debug.v
267+
ext_t.source cmts_t conf ext_t.ast ) )
267268
in
268269
(contents, cmts_t)
269270
in
@@ -289,22 +290,22 @@ let format (type a b) (fg : a Extended_ast.t) (std_fg : b Std_ast.t)
289290
if conf.opr_opts.margin_check.v then
290291
check_margin conf ~fmted
291292
~filename:(Option.value output_file ~default:input_name) ;
292-
let strlocs = collect_strlocs fg t.ast in
293+
let strlocs = collect_strlocs ext_fg ext_t.ast in
293294
Ok (strlocs, fmted) )
294295
else
295296
let exn_args () =
296297
[("output file", dump_formatted ~suffix:".invalid-ast" fmted)]
297298
|> List.filter_map ~f:(fun (s, f_opt) ->
298299
Option.map f_opt ~f:(fun f -> (s, String.sexp_of_t f)) )
299300
in
300-
let* t_new =
301+
let* ext_t_new =
301302
match
302-
parse (parse_ast conf) ~disable_w50:true fg conf ~input_name
303+
parse (parse_ast conf) ~disable_w50:true ext_fg conf ~input_name
303304
~source:fmted
304305
with
305306
| exception Sys_error msg -> Error (Error.User_error msg)
306307
| exception exn -> internal_error [`Cannot_parse exn] (exn_args ())
307-
| t_new -> Ok t_new
308+
| ext_t_new -> Ok ext_t_new
308309
in
309310
let* std_t_new =
310311
match
@@ -324,7 +325,8 @@ let format (type a b) (fg : a Extended_ast.t) (std_fg : b Std_ast.t)
324325
(Normalize_std_ast.equal std_fg conf std_t.ast std_t_new.ast
325326
~ignore_doc_comments:(not conf.opr_opts.comment_check.v) ) )
326327
&& not
327-
(Normalize_extended_ast.equal fg conf t.ast t_new.ast
328+
(Normalize_extended_ast.equal ext_fg conf ext_t.ast
329+
ext_t_new.ast
328330
~ignore_doc_comments:(not conf.opr_opts.comment_check.v) )
329331
then
330332
let old_ast =
@@ -364,7 +366,7 @@ let format (type a b) (fg : a Extended_ast.t) (std_fg : b Std_ast.t)
364366
| Some file ->
365367
if i = 1 then Format.eprintf "[DEBUG] AST structure: %s\n" file
366368
| None -> () ) ;
367-
check_comments conf cmts_t ~old:t ~new_:t_new ;
369+
check_comments conf cmts_t ~old:ext_t ~new_:ext_t_new ;
368370
(* Too many iteration ? *)
369371
if i >= conf.opr_opts.max_iters.v then (
370372
Stdlib.flush_all () ;
@@ -373,9 +375,9 @@ let format (type a b) (fg : a Extended_ast.t) (std_fg : b Std_ast.t)
373375
) )
374376
else
375377
(* All good, continue *)
376-
print_check ~i:(i + 1) ~conf ~prev_source:fmted t_new std_t_new
378+
print_check ~i:(i + 1) ~conf ~prev_source:fmted ext_t_new std_t_new
377379
in
378-
try print_check ~i:1 ~conf ~prev_source parsed std_parsed with
380+
try print_check ~i:1 ~conf ~prev_source ext_parsed std_parsed with
379381
| Sys_error msg -> Error (User_error msg)
380382
| exn -> Error (Ocamlformat_bug {exn; input_name})
381383

@@ -384,20 +386,21 @@ let parse_result ?disable_w50 f fragment conf ~source ~input_name =
384386
| exception exn -> Error (Error.Invalid_source {exn; input_name})
385387
| parsed -> Ok parsed
386388

387-
let parse_and_format (type a b) (fg : a Extended_ast.t)
388-
(std_fg : b Std_ast.t) ?output_file ~input_name ~source (conf : Conf.t) =
389+
let parse_and_format (type ext std) (ext_fg : ext Extended_ast.t)
390+
(std_fg : std Std_ast.t) ?output_file ~input_name ~source (conf : Conf.t)
391+
=
389392
Location.input_name := input_name ;
390393
let line_endings = conf.fmt_opts.line_endings.v in
391-
let* parsed =
392-
parse_result (parse_ast conf) ~disable_w50:true fg conf ~source
394+
let* ext_parsed =
395+
parse_result (parse_ast conf) ~disable_w50:true ext_fg conf ~source
393396
~input_name
394397
in
395398
let* std_parsed =
396399
parse_result Std_ast.Parse.ast std_fg conf ~source ~input_name
397400
in
398401
let+ strlocs, formatted =
399-
format fg std_fg ?output_file ~input_name ~prev_source:source ~parsed
400-
~std_parsed conf
402+
format ext_fg std_fg ?output_file ~input_name ~prev_source:source
403+
~ext_parsed ~std_parsed conf
401404
in
402405
Eol_compat.normalize_eol ~exclude_locs:strlocs ~line_endings formatted
403406

0 commit comments

Comments
 (0)