Skip to content

Commit 4c0e6b8

Browse files
authored
Merge pull request #3928 from nojb/format_dune_tweaks
dune formatting: style tweaks
2 parents b2542b9 + c556c5b commit 4c0e6b8

File tree

15 files changed

+147
-81
lines changed

15 files changed

+147
-81
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ Unreleased
7272
- instrumentations backends can now receive arguments via `(instrumentation
7373
(backend <name> <args>))`. (#3906, #3932, @nojb)
7474

75+
- Tweak auto-formatting of `dune` files to improve readability. (#3928, @nojb)
76+
7577
2.7.1 (2/09/2020)
7678
-----------------
7779

bin/describe.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,8 @@ let print_as_sexp dyn =
287287
|> Dune_lang.Ast.add_loc ~loc:Loc.none
288288
|> Dune_lang.Cst.concrete
289289
in
290-
Dune_engine.Format_dune_lang.pp_top_sexps Stdlib.Format.std_formatter [ cst ]
290+
Pp.to_fmt Stdlib.Format.std_formatter
291+
(Dune_engine.Format_dune_lang.pp_top_sexps [ cst ])
291292

292293
let term =
293294
let+ common = Common.term

bin/dune

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,20 @@
11
(executable
22
(name main)
3-
(libraries memo dune_lang fiber stdune unix cache_daemon cache dune_rules
4-
dune_engine dune_util cmdliner threads.posix build_info dune_csexp)
3+
(libraries
4+
memo
5+
dune_lang
6+
fiber
7+
stdune
8+
unix
9+
cache_daemon
10+
cache
11+
dune_rules
12+
dune_engine
13+
dune_util
14+
cmdliner
15+
threads.posix
16+
build_info
17+
dune_csexp)
518
(bootstrap_info bootstrap-info))
619

720
(rule

bin/printenv.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ let pp ppf ~fields sexps =
2929
if do_print then
3030
Dune_lang.Ast.add_loc sexp ~loc:Loc.none
3131
|> Dune_lang.Cst.concrete |> List.singleton
32-
|> Format.fprintf ppf "%a@?" Dune_engine.Format_dune_lang.pp_top_sexps)
32+
|> Dune_engine.Format_dune_lang.pp_top_sexps
33+
|> Format.fprintf ppf "%a@?" Pp.to_fmt)
3334

3435
let term =
3536
let+ common = Common.term

otherlibs/action-plugin/src/dune

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
(public_name dune-action-plugin)
44
(libraries dune-private-libs.stdune dune-glob dune-private-libs.dune_csexp)
55
(synopsis
6-
"[Internal] Monadic interface for defining scripts with dynamic or complex sets of depencencies."))
6+
"[Internal] Monadic interface for defining scripts with dynamic or complex sets of depencencies."))

src/dune_engine/dune

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,28 @@
22

33
(library
44
(name dune_engine)
5-
(libraries unix stdune fiber incremental_cycles dag memo xdg dune_re
6-
threads.posix opam_file_format dune_lang cache_daemon cache dune_glob
7-
ocaml_config catapult jbuild_support dune_action_plugin dune_util
8-
build_path_prefix_map dune_section)
5+
(libraries
6+
unix
7+
stdune
8+
fiber
9+
incremental_cycles
10+
dag
11+
memo
12+
xdg
13+
dune_re
14+
threads.posix
15+
opam_file_format
16+
dune_lang
17+
cache_daemon
18+
cache
19+
dune_glob
20+
ocaml_config
21+
catapult
22+
jbuild_support
23+
dune_action_plugin
24+
dune_util
25+
build_path_prefix_map
26+
dune_section)
927
(synopsis "Internal Dune library, do not use!"))
1028

1129
(ocamllex dune_lexer)
Lines changed: 32 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
open! Stdune
22
open! Import
3+
open Pp.O
34

45
type 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

9586
let 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))

src/dune_engine/format_dune_lang.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ val write_file : path:Path.t -> Dune_lang.Cst.t list -> unit
1515
val format_file : input:Path.t option -> output:Path.t option -> unit
1616

1717
(** Pretty-print a list of toplevel s-expressions *)
18-
val pp_top_sexps : Format.formatter -> Dune_lang.Cst.t list -> unit
18+
val pp_top_sexps : Dune_lang.Cst.t list -> _ Pp.t

src/dune_rules/dune

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,30 @@
22

33
(library
44
(name dune_rules)
5-
(libraries unix stdune fiber incremental_cycles dag memo xdg dune_re
6-
threads.posix opam_file_format dune_lang cache_daemon cache dune_glob
7-
ocaml_config catapult jbuild_support dune_action_plugin dune_util
8-
dune_meta_parser dune_section build_path_prefix_map dune_engine)
5+
(libraries
6+
unix
7+
stdune
8+
fiber
9+
incremental_cycles
10+
dag
11+
memo
12+
xdg
13+
dune_re
14+
threads.posix
15+
opam_file_format
16+
dune_lang
17+
cache_daemon
18+
cache
19+
dune_glob
20+
ocaml_config
21+
catapult
22+
jbuild_support
23+
dune_action_plugin
24+
dune_util
25+
dune_meta_parser
26+
dune_section
27+
build_path_prefix_map
28+
dune_engine)
929
(synopsis "Internal Dune library, do not use!"))
1030

1131
(ocamllex ocamlobjinfo cram_lexer)

src/dune_rules/upgrader.ml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,8 @@ module Common = struct
134134
let string_of_sexps sexps comments =
135135
let new_csts = List.map sexps ~f:Dune_lang.Cst.concrete in
136136
Dune_lang.Parser.insert_comments new_csts comments
137-
|> Format.asprintf "%a@?" Format_dune_lang.pp_top_sexps
137+
|> Format_dune_lang.pp_top_sexps
138+
|> Format.asprintf "%a@?" Pp.to_fmt
138139
end
139140

140141
module V1 = struct
@@ -324,7 +325,9 @@ module V1 = struct
324325
(List.map ~f:Dune_lang.Cst.concrete sexps)
325326
comments
326327
in
327-
let contents = Format.asprintf "%a@?" Format_dune_lang.pp_top_sexps sexps in
328+
let contents =
329+
Format.asprintf "%a@?" Pp.to_fmt (Format_dune_lang.pp_top_sexps sexps)
330+
in
328331
todo.to_rename_and_edit <-
329332
{ original_file = file; new_file; extra_files_to_delete; contents }
330333
:: todo.to_rename_and_edit

0 commit comments

Comments
 (0)