Skip to content

Commit

Permalink
Tweak dune file formatting: avoid pure vertical layout when formattin…
Browse files Browse the repository at this point in the history
…g wrapped lists (#10892)

* format-dune-file: use a hov box instead of a hv box for wrapped lists

Signed-off-by: Nicolás Ojeda Bär <n.oje.bar@gmail.com>
Signed-off-by: Marek Kubica <marek@tarides.com>

* Add test

Signed-off-by: Nicolás Ojeda Bär <n.oje.bar@gmail.com>
Signed-off-by: Marek Kubica <marek@tarides.com>

* Accept

Signed-off-by: Nicolás Ojeda Bär <n.oje.bar@gmail.com>
Signed-off-by: Marek Kubica <marek@tarides.com>

* Add changelog entry

Signed-off-by: Marek Kubica <marek@tarides.com>

---------

Signed-off-by: Nicolás Ojeda Bär <n.oje.bar@gmail.com>
Signed-off-by: Marek Kubica <marek@tarides.com>
Co-authored-by: Marek Kubica <marek@tarides.com>
  • Loading branch information
nojb and Leonidas-from-XIV authored Oct 18, 2024
1 parent ed871ba commit af95981
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 82 deletions.
2 changes: 2 additions & 0 deletions doc/changes/10892.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Format long lists in s-expressions to fill the line instead of formatting
them in a vertical way (#10892, fixes #10860, @nojb)
5 changes: 4 additions & 1 deletion src/dune_lang/format.ml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ let print_wrapped_list ~version x =
let inner = Pp.concat_map ~sep:Pp.space ~f:pp_simple x in
if version < (2, 8)
then Pp.char '(' ++ Pp.hovbox ~indent:1 inner ++ Pp.char ')'
else Pp.hvbox ~indent:1 (Pp.char '(' ++ inner ++ Pp.char ')')
else
(if version < (3, 17) then Pp.hvbox else Pp.hovbox)
~indent:1
(Pp.char '(' ++ inner ++ Pp.char ')')
;;

let pp_comment_line l = Pp.char ';' ++ Pp.verbatim l
Expand Down
51 changes: 17 additions & 34 deletions test/blackbox-tests/test-cases/describe/describe-workspace-pp.t
Original file line number Diff line number Diff line change
Expand Up @@ -104,20 +104,13 @@ not stable across different setups.
(executables
((names (exe))
(requires
(c39d8e11db2363236e69af7750ce7b9a
c9367091ddd9a70d99fc22ede348f17c
1f2b5eb300ea716920494385a31bb5fb
5014e215e204cf8da6c32644cda1b31e
249b2edaf3cc552a247667041bb5f015
ba85adfb1c97e7d7af3df35b16b2fc0d
2363fd46dac995a1c79679dfa1a9881b
43b7cbe1f93f4f502ec614971027cff9
e68a558facd1546b51c7abdbf6aed1cb
24f4eb12e3ff51b310dbf7443c6087be
449445be7a24ce51e119d57e9e255d3f
5ae836dcdead11d5c16815297c5a1ae6
2c61db8e94cb08e0fe642152aee8121a
6fb5d46437c55abca48c8b995f8afa51
(c39d8e11db2363236e69af7750ce7b9a c9367091ddd9a70d99fc22ede348f17c
1f2b5eb300ea716920494385a31bb5fb 5014e215e204cf8da6c32644cda1b31e
249b2edaf3cc552a247667041bb5f015 ba85adfb1c97e7d7af3df35b16b2fc0d
2363fd46dac995a1c79679dfa1a9881b 43b7cbe1f93f4f502ec614971027cff9
e68a558facd1546b51c7abdbf6aed1cb 24f4eb12e3ff51b310dbf7443c6087be
449445be7a24ce51e119d57e9e255d3f 5ae836dcdead11d5c16815297c5a1ae6
2c61db8e94cb08e0fe642152aee8121a 6fb5d46437c55abca48c8b995f8afa51
f9851d3f8ae32391e7594cf97332a78c))
(modules
(((name Exe)
Expand Down Expand Up @@ -147,8 +140,7 @@ not stable across different setups.
(uid f9851d3f8ae32391e7594cf97332a78c)
(local true)
(requires
(ba85adfb1c97e7d7af3df35b16b2fc0d
2c61db8e94cb08e0fe642152aee8121a
(ba85adfb1c97e7d7af3df35b16b2fc0d 2c61db8e94cb08e0fe642152aee8121a
6fb5d46437c55abca48c8b995f8afa51))
(source_dir _build/default/dummy_ppx)
(modules
Expand Down Expand Up @@ -187,14 +179,10 @@ not stable across different setups.
(uid 2c61db8e94cb08e0fe642152aee8121a)
(local false)
(requires
(ba85adfb1c97e7d7af3df35b16b2fc0d
2363fd46dac995a1c79679dfa1a9881b
5014e215e204cf8da6c32644cda1b31e
43b7cbe1f93f4f502ec614971027cff9
e68a558facd1546b51c7abdbf6aed1cb
24f4eb12e3ff51b310dbf7443c6087be
5ae836dcdead11d5c16815297c5a1ae6
249b2edaf3cc552a247667041bb5f015
(ba85adfb1c97e7d7af3df35b16b2fc0d 2363fd46dac995a1c79679dfa1a9881b
5014e215e204cf8da6c32644cda1b31e 43b7cbe1f93f4f502ec614971027cff9
e68a558facd1546b51c7abdbf6aed1cb 24f4eb12e3ff51b310dbf7443c6087be
5ae836dcdead11d5c16815297c5a1ae6 249b2edaf3cc552a247667041bb5f015
449445be7a24ce51e119d57e9e255d3f))
(source_dir /FINDLIB/ppxlib)
(modules ())
Expand Down Expand Up @@ -296,8 +284,7 @@ not stable across different setups.
(uid f9851d3f8ae32391e7594cf97332a78c)
(local true)
(requires
(ba85adfb1c97e7d7af3df35b16b2fc0d
2c61db8e94cb08e0fe642152aee8121a
(ba85adfb1c97e7d7af3df35b16b2fc0d 2c61db8e94cb08e0fe642152aee8121a
6fb5d46437c55abca48c8b995f8afa51))
(source_dir _build/default/dummy_ppx)
(modules
Expand Down Expand Up @@ -349,14 +336,10 @@ not stable across different setups.
(uid 2c61db8e94cb08e0fe642152aee8121a)
(local false)
(requires
(ba85adfb1c97e7d7af3df35b16b2fc0d
2363fd46dac995a1c79679dfa1a9881b
5014e215e204cf8da6c32644cda1b31e
43b7cbe1f93f4f502ec614971027cff9
e68a558facd1546b51c7abdbf6aed1cb
24f4eb12e3ff51b310dbf7443c6087be
5ae836dcdead11d5c16815297c5a1ae6
249b2edaf3cc552a247667041bb5f015
(ba85adfb1c97e7d7af3df35b16b2fc0d 2363fd46dac995a1c79679dfa1a9881b
5014e215e204cf8da6c32644cda1b31e 43b7cbe1f93f4f502ec614971027cff9
e68a558facd1546b51c7abdbf6aed1cb 24f4eb12e3ff51b310dbf7443c6087be
5ae836dcdead11d5c16815297c5a1ae6 249b2edaf3cc552a247667041bb5f015
449445be7a24ce51e119d57e9e255d3f))
(source_dir /FINDLIB/ppxlib)
(modules ())
Expand Down
24 changes: 8 additions & 16 deletions test/blackbox-tests/test-cases/describe/describe.t
Original file line number Diff line number Diff line change
Expand Up @@ -546,14 +546,10 @@ not stable across different setups.
(uid 2c61db8e94cb08e0fe642152aee8121a)
(local false)
(requires
(ba85adfb1c97e7d7af3df35b16b2fc0d
2363fd46dac995a1c79679dfa1a9881b
5014e215e204cf8da6c32644cda1b31e
43b7cbe1f93f4f502ec614971027cff9
e68a558facd1546b51c7abdbf6aed1cb
24f4eb12e3ff51b310dbf7443c6087be
5ae836dcdead11d5c16815297c5a1ae6
249b2edaf3cc552a247667041bb5f015
(ba85adfb1c97e7d7af3df35b16b2fc0d 2363fd46dac995a1c79679dfa1a9881b
5014e215e204cf8da6c32644cda1b31e 43b7cbe1f93f4f502ec614971027cff9
e68a558facd1546b51c7abdbf6aed1cb 24f4eb12e3ff51b310dbf7443c6087be
5ae836dcdead11d5c16815297c5a1ae6 249b2edaf3cc552a247667041bb5f015
449445be7a24ce51e119d57e9e255d3f))
(source_dir /FINDLIB/ppxlib)
(modules ())
Expand Down Expand Up @@ -1120,14 +1116,10 @@ not stable across different setups.
(uid 2c61db8e94cb08e0fe642152aee8121a)
(local false)
(requires
(ba85adfb1c97e7d7af3df35b16b2fc0d
2363fd46dac995a1c79679dfa1a9881b
5014e215e204cf8da6c32644cda1b31e
43b7cbe1f93f4f502ec614971027cff9
e68a558facd1546b51c7abdbf6aed1cb
24f4eb12e3ff51b310dbf7443c6087be
5ae836dcdead11d5c16815297c5a1ae6
249b2edaf3cc552a247667041bb5f015
(ba85adfb1c97e7d7af3df35b16b2fc0d 2363fd46dac995a1c79679dfa1a9881b
5014e215e204cf8da6c32644cda1b31e 43b7cbe1f93f4f502ec614971027cff9
e68a558facd1546b51c7abdbf6aed1cb 24f4eb12e3ff51b310dbf7443c6087be
5ae836dcdead11d5c16815297c5a1ae6 249b2edaf3cc552a247667041bb5f015
449445be7a24ce51e119d57e9e255d3f))
(source_dir /FINDLIB/ppxlib)
(modules ())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ When a list is indented, there is no extra space at the end.
When there is a long list of atoms, quoted strings, templates and singletons,
it gets wrapped.

$ echo '(library (name dune) (libraries unix stdune fiber xdg dune_re threads opam_file_format dune_lang ocaml_config which_program) (synopsis "Internal Dune library, do not use!") (preprocess (action (run %{project_root}/src/let-syntax/pp.exe %{input-file}))))' | dune format-dune-file
$ echo '(library (name dune) (libraries unix stdune fiber xdg dune_re threads opam_file_format dune_lang ocaml_config which_program) (synopsis "Internal Dune library, do not use!") (preprocess (action (run %{project_root}/src/let-syntax/pp.exe %{input-file}))))' | dune format-dune-file --dune-version 3.16
(library
(name dune)
(libraries
Expand All @@ -68,6 +68,18 @@ it gets wrapped.
(action
(run %{project_root}/src/let-syntax/pp.exe %{input-file}))))

The same file, but in the current version:

$ echo '(library (name dune) (libraries unix stdune fiber xdg dune_re threads opam_file_format dune_lang ocaml_config which_program) (synopsis "Internal Dune library, do not use!") (preprocess (action (run %{project_root}/src/let-syntax/pp.exe %{input-file}))))' | dune format-dune-file
(library
(name dune)
(libraries unix stdune fiber xdg dune_re threads opam_file_format dune_lang
ocaml_config which_program)
(synopsis "Internal Dune library, do not use!")
(preprocess
(action
(run %{project_root}/src/let-syntax/pp.exe %{input-file}))))

In multi-line strings, newlines are escaped, but their syntax is not preserved.

$ dune format-dune-file <<EOF
Expand Down
40 changes: 10 additions & 30 deletions test/blackbox-tests/test-cases/github7034.t/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,8 @@ It builds on its own (with a warning) when lang dune is 3.2 or below:
$ dune printenv --root=inner --field flags
Entering directory 'inner'
(flags
(-w
@1..3@5..28@30..39@43@46..47@49..57@61..62-40
-strict-sequence
-strict-formats
-short-paths
-keep-locs))
(-w @1..3@5..28@30..39@43@46..47@49..57@61..62-40 -strict-sequence
-strict-formats -short-paths -keep-locs))
Leaving directory 'inner'
$ dune build --root=inner
Entering directory 'inner'
Expand All @@ -41,12 +37,8 @@ Note that in versions of lang dune above 3.2 this warning becomes an error:
$ dune printenv --root=inner --field flags
Entering directory 'inner'
(flags
(-w
@1..3@5..28@30..39@43@46..47@49..57@61..62@67@69-40
-strict-sequence
-strict-formats
-short-paths
-keep-locs))
(-w @1..3@5..28@30..39@43@46..47@49..57@61..62@67@69-40 -strict-sequence
-strict-formats -short-paths -keep-locs))
Leaving directory 'inner'
$ dune build --root=inner
Entering directory 'inner'
Expand All @@ -68,12 +60,8 @@ Building the outer project works when lang dune is 3.2 or below:
$ dune printenv --root=outer --field flags
Entering directory 'outer'
(flags
(-w
@1..3@5..28@30..39@43@46..47@49..57@61..62-40
-strict-sequence
-strict-formats
-short-paths
-keep-locs))
(-w @1..3@5..28@30..39@43@46..47@49..57@61..62-40 -strict-sequence
-strict-formats -short-paths -keep-locs))
Leaving directory 'outer'
$ dune build --root=outer
Expand All @@ -89,22 +77,14 @@ But when lang dune is 3.3 or higher the warning becomes an error:
$ dune printenv --root=outer --field flags
Entering directory 'outer'
(flags
(-w
@1..3@5..28@30..39@43@46..47@49..57@61..62@67@69-40
-strict-sequence
-strict-formats
-short-paths
-keep-locs))
(-w @1..3@5..28@30..39@43@46..47@49..57@61..62@67@69-40 -strict-sequence
-strict-formats -short-paths -keep-locs))
Leaving directory 'outer'
$ dune printenv outer/vendored/inner --root=outer --field flags
Entering directory 'outer'
(flags
(-w
@1..3@5..28@30..39@43@46..47@49..57@61..62@67@69-40
-strict-sequence
-strict-formats
-short-paths
-keep-locs))
(-w @1..3@5..28@30..39@43@46..47@49..57@61..62@67@69-40 -strict-sequence
-strict-formats -short-paths -keep-locs))
Leaving directory 'outer'
$ dune build --root=outer
Expand Down

0 comments on commit af95981

Please sign in to comment.