11open ! Stdune
22open ! Import
3+ open Pp.O
34
45type dune_file =
56 | OCaml_syntax of Loc .t
@@ -29,73 +30,63 @@ let can_be_displayed_wrapped =
2930 | Comment _ ->
3031 false )
3132
32- let pp_simple fmt t =
33+ let pp_simple t =
3334 Dune_lang.Cst. abstract t |> Option. value_exn |> Dune_lang.Ast. remove_locs
34- |> Dune_lang.Deprecated. pp fmt
35+ |> Dune_lang. pp
3536
36- let print_wrapped_list fmt =
37- Format. fprintf fmt " (@[<hov 1>%a@])"
38- (Format. pp_print_list
39- ~pp_sep: (fun fmt () -> Format. fprintf fmt " @ " )
40- pp_simple)
37+ let print_wrapped_list x =
38+ Pp. hvbox ~indent: 1
39+ (Pp. char '(' ++ Pp. concat_map ~sep: Pp. space ~f: pp_simple x ++ Pp. char ')' )
4140
42- let pp_comment_line fmt l = Format. fprintf fmt " ;%s " l
41+ let pp_comment_line l = Pp. char ';' ++ Pp. verbatim l
4342
44- let pp_comment loc fmt (comment : Dune_lang.Cst.Comment.t ) =
43+ let pp_comment loc (comment : Dune_lang.Cst.Comment.t ) =
4544 match comment with
46- | Lines ls ->
47- Format. fprintf fmt " @[<v 0>%a@]"
48- (Format. pp_print_list
49- ~pp_sep: (fun fmt () -> Format. fprintf fmt " @;" )
50- pp_comment_line)
51- ls
45+ | Lines ls -> Pp. vbox (Pp. concat_map ~sep: Pp. cut ~f: pp_comment_line ls)
5246 | Legacy ->
5347 User_error. raise ~loc
5448 [ Pp. text " Formatting is only supported with the dune syntax" ]
5549
56- let pp_break fmt attached =
50+ let pp_break attached =
5751 if attached then
58- Format. fprintf fmt " "
52+ Pp. char ' '
5953 else
60- Format. fprintf fmt " @, "
54+ Pp. cut
6155
62- let pp_list_with_comments pp_sexp fmt sexps =
63- let rec go fmt (l : Dune_lang.Cst.t list ) =
56+ let pp_list_with_comments pp_sexp sexps =
57+ let rec go (l : Dune_lang.Cst.t list ) =
6458 match l with
6559 | x :: Comment (loc , c ) :: xs ->
6660 let attached = Loc. on_same_line (Dune_lang.Cst. loc x) loc in
67- Format. fprintf fmt " %a%a%a@,%a" pp_sexp x pp_break attached
68- (pp_comment loc) c go xs
69- | Comment (loc , c ) :: xs ->
70- Format. fprintf fmt " %a@,%a" (pp_comment loc) c go xs
71- | [ x ] -> Format. fprintf fmt " %a" pp_sexp x
72- | x :: xs -> Format. fprintf fmt " %a@,%a" pp_sexp x go xs
73- | [] -> ()
61+ pp_sexp x ++ pp_break attached ++ pp_comment loc c ++ Pp. cut ++ go xs
62+ | Comment (loc , c ) :: xs -> pp_comment loc c ++ Pp. cut ++ go xs
63+ | [ x ] -> pp_sexp x
64+ | x :: xs -> pp_sexp x ++ Pp. cut ++ go xs
65+ | [] -> Pp. nop
7466 in
75- go fmt sexps
67+ go sexps
7668
77- let rec pp_sexp fmt : Dune_lang.Cst.t -> _ = function
78- | (Atom _ | Quoted_string _ | Template _ ) as sexp -> pp_simple fmt sexp
69+ let rec pp_sexp : Dune_lang.Cst.t -> _ = function
70+ | (Atom _ | Quoted_string _ | Template _ ) as sexp -> pp_simple sexp
7971 | List (_ , sexps ) ->
80- Format. fprintf fmt " @[<v 1>%a@] "
72+ Pp. vbox ~indent: 1
8173 ( if can_be_displayed_wrapped sexps then
82- print_wrapped_list
74+ print_wrapped_list sexps
8375 else
84- pp_sexp_list )
85- sexps
86- | Comment (loc , c ) -> pp_comment loc fmt c
76+ pp_sexp_list sexps )
77+ | Comment (loc , c ) -> pp_comment loc c
8778
88- and pp_sexp_list fmt = Format. fprintf fmt " (%a)" (pp_list_with_comments pp_sexp)
79+ and pp_sexp_list sexps =
80+ Pp. char '(' ++ pp_list_with_comments pp_sexp sexps ++ Pp. char ')'
8981
90- let pp_top_sexp fmt sexp = Format. fprintf fmt " %a \n " pp_sexp sexp
82+ let pp_top_sexp sexp = pp_sexp sexp ++ Pp. char '\n'
9183
92- let pp_top_sexps =
93- Format. pp_print_list ~pp_sep: Format. pp_print_newline pp_top_sexp
84+ let pp_top_sexps = Pp. concat_map ~sep: Pp. newline ~f: pp_top_sexp
9485
9586let write_file ~path sexps =
9687 let f oc =
9788 let fmt = Format. formatter_of_out_channel oc in
98- Format. fprintf fmt " %a%!" pp_top_sexps sexps
89+ Format. fprintf fmt " %a%!" Pp. to_fmt ( pp_top_sexps sexps)
9990 in
10091 Io. with_file_out ~binary: true path ~f
10192
@@ -116,4 +107,4 @@ let format_file ~input ~output =
116107 | Sexps sexps ->
117108 with_output (fun oc ->
118109 let oc = Format. formatter_of_out_channel oc in
119- Format. fprintf oc " %a%!" pp_top_sexps sexps)
110+ Format. fprintf oc " %a%!" Pp. to_fmt ( pp_top_sexps sexps) )
0 commit comments