Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ profile. This started with version 0.26.0.

## unreleased

### Changed

- Empty bodies of while loops will be formatted on one line: `do () done`.
(#2812, @v-gb)

### Fixed

- Fix formatting oscillation with `if-then-else=fit-or-vertical` and
Expand Down
12 changes: 10 additions & 2 deletions lib/Fmt_ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2910,6 +2910,14 @@ and fmt_expression c ?(box = true) ?(pro = noop) ?eol ?parens
$ fmt_core_type c (sub_typ ~ctx t2) )
$ fmt_atrs ) )
| Pexp_while (e1, e2, infix_ext_attrs) ->
let break_around_body =
match e2 with
| { pexp_desc= Pexp_construct ({txt= Lident "()"; _}, None)
; pexp_attributes= []
; _ } ->
str " "
| _ -> force_break
in
pro
$ hvbox 0
(Params.Exp.wrap c.conf ~parens
Expand All @@ -2921,9 +2929,9 @@ and fmt_expression c ?(box = true) ?(pro = noop) ?eol ?parens
$ break 1 2
$ fmt_expression c (sub_exp ~ctx e1)
$ space_break $ str "do" )
$ force_break
$ break_around_body
$ fmt_expression c (sub_exp ~ctx e2) )
$ force_break $ str "done" )
$ break_around_body $ str "done" )
$ fmt_atrs ) )
| Pexp_unreachable -> pro $ str "."
| Pexp_send (exp, meth) ->
Expand Down
2 changes: 1 addition & 1 deletion test/passing/refs.ahrefs/extensions-indent.ml.err
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Warning: extensions-indent.ml:545 exceeds the margin
Warning: extensions-indent.ml:541 exceeds the margin
8 changes: 2 additions & 6 deletions test/passing/refs.ahrefs/extensions-indent.ml.ref
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,13 @@ let _ =
(for i = 0 to 1 do
()
done)
(while true do
()
done)
(while true do () done)
let _ =
f
(for%ext i = 0 to 1 do
()
done)
(while%ext true do
()
done)
(while%ext true do () done)

let _ = function%ext
| x -> x
Expand Down
2 changes: 1 addition & 1 deletion test/passing/refs.ahrefs/extensions.ml.err
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Warning: extensions.ml:545 exceeds the margin
Warning: extensions.ml:541 exceeds the margin
8 changes: 2 additions & 6 deletions test/passing/refs.ahrefs/extensions.ml.ref
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,13 @@ let _ =
(for i = 0 to 1 do
()
done)
(while true do
()
done)
(while true do () done)
let _ =
f
(for%ext i = 0 to 1 do
()
done)
(while%ext true do
()
done)
(while%ext true do () done)

let _ = function%ext
| x -> x
Expand Down
10 changes: 8 additions & 2 deletions test/passing/refs.ahrefs/for_while.ml.ref
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
let () =
foo
(for i = 1 to 10 do
()
print_endline ()
done)

let () =
foo
(while true do
()
print_endline ()
done)

let _ =
Expand Down Expand Up @@ -48,3 +48,9 @@ let _ =
do
test this
done

let _ =
while
some biggggggggggggggggggggggggggggggggg
expressionnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn
do () done
4 changes: 1 addition & 3 deletions test/passing/refs.ahrefs/kw_extentions.ml.ref
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ let () =
for%ext i = 1 to 10 do
()
done;
while%ext false do
()
done;
while%ext false do () done;
match%ext x with
| _ -> ()

Expand Down
6 changes: 1 addition & 5 deletions test/passing/refs.ahrefs/shortcut_ext_attr.ml.ref
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,7 @@ let () =
| x -> ()];
[%foo try[@foo] () with _ -> ()];
[%foo if[@foo] () then () else ()];
[%foo
while () do
()
done
[@foo]];
[%foo while () do () done [@foo]];
[%foo
for x = () to () do
()
Expand Down
8 changes: 4 additions & 4 deletions test/passing/refs.ahrefs/source.ml.err
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Warning: source.ml:3417 exceeds the margin
Warning: source.ml:6551 exceeds the margin
Warning: source.ml:6985 exceeds the margin
Warning: source.ml:7817 exceeds the margin
Warning: source.ml:3415 exceeds the margin
Warning: source.ml:6549 exceeds the margin
Warning: source.ml:6983 exceeds the margin
Warning: source.ml:7815 exceeds the margin
4 changes: 1 addition & 3 deletions test/passing/refs.ahrefs/source.ml.ref
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,7 @@ let () =
| x -> ());
(try%foo[@foo] () with _ -> ());
if%foo[@foo] () then () else ();
while%foo[@foo] () do
()
done;
while%foo[@foo] () do () done;
for%foo[@foo] x = () to () do
()
done;
Expand Down
2 changes: 1 addition & 1 deletion test/passing/refs.default/extensions-indent.ml.err
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Warning: extensions-indent.ml:459 exceeds the margin
Warning: extensions-indent.ml:455 exceeds the margin
8 changes: 2 additions & 6 deletions test/passing/refs.default/extensions-indent.ml.ref
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,14 @@ let _ =
(for i = 0 to 1 do
()
done)
(while true do
()
done)
(while true do () done)

let _ =
f
(for%ext i = 0 to 1 do
()
done)
(while%ext true do
()
done)
(while%ext true do () done)

let _ = function%ext x -> x
let _ = f (function%ext x -> x)
Expand Down
2 changes: 1 addition & 1 deletion test/passing/refs.default/extensions.ml.err
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Warning: extensions.ml:459 exceeds the margin
Warning: extensions.ml:455 exceeds the margin
8 changes: 2 additions & 6 deletions test/passing/refs.default/extensions.ml.ref
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,14 @@ let _ =
(for i = 0 to 1 do
()
done)
(while true do
()
done)
(while true do () done)

let _ =
f
(for%ext i = 0 to 1 do
()
done)
(while%ext true do
()
done)
(while%ext true do () done)

let _ = function%ext x -> x
let _ = f (function%ext x -> x)
Expand Down
10 changes: 8 additions & 2 deletions test/passing/refs.default/for_while.ml.ref
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
let () =
foo
(for i = 1 to 10 do
()
print_endline ()
done)

let () =
foo
(while true do
()
print_endline ()
done)

let _ =
Expand Down Expand Up @@ -48,3 +48,9 @@ let _ =
do
test this
done

let _ =
while
some biggggggggggggggggggggggggggggggggg
expressionnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn
do () done
4 changes: 1 addition & 3 deletions test/passing/refs.default/kw_extentions.ml.ref
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ let () =
for%ext i = 1 to 10 do
()
done;
while%ext false do
()
done;
while%ext false do () done;
match%ext x with _ -> ()

let () =
Expand Down
6 changes: 1 addition & 5 deletions test/passing/refs.default/shortcut_ext_attr.ml.ref
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@ let () =
[%foo function[@foo] x -> ()];
[%foo try[@foo] () with _ -> ()];
[%foo if[@foo] () then () else ()];
[%foo
while () do
()
done
[@foo]];
[%foo while () do () done [@foo]];
[%foo
for x = () to () do
()
Expand Down
8 changes: 4 additions & 4 deletions test/passing/refs.default/source.ml.err
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Warning: source.ml:917 exceeds the margin
Warning: source.ml:992 exceeds the margin
Warning: source.ml:6638 exceeds the margin
Warning: source.ml:7097 exceeds the margin
Warning: source.ml:915 exceeds the margin
Warning: source.ml:990 exceeds the margin
Warning: source.ml:6636 exceeds the margin
Warning: source.ml:7095 exceeds the margin
4 changes: 1 addition & 3 deletions test/passing/refs.default/source.ml.ref
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,7 @@ let () =
(function%foo[@foo] x -> ());
(try%foo[@foo] () with _ -> ());
if%foo[@foo] () then () else ();
while%foo[@foo] () do
()
done;
while%foo[@foo] () do () done;
for%foo[@foo] x = () to () do
()
done;
Expand Down
8 changes: 2 additions & 6 deletions test/passing/refs.janestreet/extensions-indent.ml.ref
Original file line number Diff line number Diff line change
Expand Up @@ -97,19 +97,15 @@ let _ =
(for i = 0 to 1 do
()
done)
(while true do
()
done)
(while true do () done)
;;

let _ =
f
(for%ext i = 0 to 1 do
()
done)
(while%ext true do
()
done)
(while%ext true do () done)
;;

let _ = function%ext
Expand Down
8 changes: 2 additions & 6 deletions test/passing/refs.janestreet/extensions.ml.ref
Original file line number Diff line number Diff line change
Expand Up @@ -97,19 +97,15 @@ let _ =
(for i = 0 to 1 do
()
done)
(while true do
()
done)
(while true do () done)
;;

let _ =
f
(for%ext i = 0 to 1 do
()
done)
(while%ext true do
()
done)
(while%ext true do () done)
;;

let _ = function%ext
Expand Down
12 changes: 10 additions & 2 deletions test/passing/refs.janestreet/for_while.ml.ref
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
let () =
foo
(for i = 1 to 10 do
()
print_endline ()
done)
;;

let () =
foo
(while true do
()
print_endline ()
done)
;;

Expand Down Expand Up @@ -53,3 +53,11 @@ let _ =
test this
done
;;

let _ =
while
some
biggggggggggggggggggggggggggggggggg
expressionnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn
do () done
;;
4 changes: 1 addition & 3 deletions test/passing/refs.janestreet/kw_extentions.ml.ref
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ let () =
for%ext i = 1 to 10 do
()
done;
while%ext false do
()
done;
while%ext false do () done;
match%ext x with
| _ -> ()
;;
Expand Down
6 changes: 1 addition & 5 deletions test/passing/refs.janestreet/shortcut_ext_attr.ml.ref
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@ let () =
try[@foo] () with
| _ -> ()];
[%foo if[@foo] () then () else ()];
[%foo
while () do
()
done
[@foo]];
[%foo while () do () done [@foo]];
[%foo
for x = () to () do
()
Expand Down
4 changes: 1 addition & 3 deletions test/passing/refs.janestreet/source.ml.ref
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,7 @@ let () =
(try%foo[@foo] () with
| _ -> ());
if%foo[@foo] () then () else ();
while%foo[@foo] () do
()
done;
while%foo[@foo] () do () done;
for%foo[@foo] x = () to () do
()
done;
Expand Down
2 changes: 1 addition & 1 deletion test/passing/refs.ocamlformat/extensions-indent.ml.err
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Warning: extensions-indent.ml:503 exceeds the margin
Warning: extensions-indent.ml:499 exceeds the margin
Loading
Loading