Skip to content

Commit ecf1eab

Browse files
use special case beginend syntax in match cases when possible
1 parent 96cc58c commit ecf1eab

18 files changed

+373
-343
lines changed

lib/Params.ml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,12 +535,18 @@ let get_cases (c : Conf.t) ~fmt_infix_ext_attrs ~ctx ~first ~last
535535
else (parenze_exp xast && not body_has_parens, Some false)
536536
in
537537
let indent = if align_nested_match then 0 else indent in
538+
let nested_exp_has_special_beginend exp =
539+
match exp with
540+
| Pexp_match _ | Pexp_try _ | Pexp_function _ | Pexp_ifthenelse _ -> true
541+
| _ -> false
542+
in
538543
let open_paren_branch, close_paren_branch, branch_expr =
539544
match ast with
540545
| { pexp_desc= Pexp_beginend (nested_exp, infix_ext_attrs)
541546
; pexp_attributes= []
542547
; _ }
543-
when not cmts_before ->
548+
when (not cmts_before)
549+
&& not (nested_exp_has_special_beginend nested_exp.pexp_desc) ->
544550
let close_paren =
545551
let offset =
546552
match c.fmt_opts.break_cases.v with `Nested -> 0 | _ -> -2

test/passing/refs.ahrefs/cases_exp_grouping.ml.ref

Lines changed: 44 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,30 @@
11
let _ =
22
match x with
3-
| A -> begin match B with A -> fooooooooooooo end
4-
| A -> begin match B with A -> fooooooooooooo | B -> fooooooooooooo end
5-
| A -> begin
6-
match B with
3+
| A ->
4+
begin match B with A -> fooooooooooooo
5+
end
6+
| A ->
7+
begin match B with A -> fooooooooooooo | B -> fooooooooooooo
8+
end
9+
| A ->
10+
begin match B with
711
| A -> fooooooooooooo
812
| B -> fooooooooooooo
913
| C -> fooooooooooooo
1014
| D -> fooooooooooooo
11-
end
15+
end
1216
[@@ocamlformat "break-cases=fit"]
1317

1418
let _ =
1519
match x with
16-
| A -> begin
17-
match B with A -> fooooooooooooo
18-
end
19-
| A -> begin
20-
match B with A -> fooooooooooooo | B -> fooooooooooooo
21-
end
22-
| A -> begin
23-
match B with
20+
| A ->
21+
begin match B with A -> fooooooooooooo
22+
end
23+
| A ->
24+
begin match B with A -> fooooooooooooo | B -> fooooooooooooo
25+
end
26+
| A ->
27+
begin match B with
2428
| A ->
2529
fooooooooooooo
2630
| B ->
@@ -34,62 +38,62 @@ let _ =
3438

3539
let _ =
3640
match x with
37-
| A -> begin
38-
match B with
41+
| A ->
42+
begin match B with
3943
| A -> fooooooooooooo
40-
end
41-
| A -> begin
42-
match B with
44+
end
45+
| A ->
46+
begin match B with
4347
| A -> fooooooooooooo
4448
| B -> fooooooooooooo
45-
end
46-
| A -> begin
47-
match B with
49+
end
50+
| A ->
51+
begin match B with
4852
| A -> fooooooooooooo
4953
| B -> fooooooooooooo
5054
| C -> fooooooooooooo
5155
| D -> fooooooooooooo
52-
end
56+
end
5357
[@@ocamlformat "break-cases=toplevel"]
5458

5559
let _ =
5660
match x with
57-
| A -> begin
58-
match B with
61+
| A ->
62+
begin match B with
5963
| A -> fooooooooooooo
60-
end
61-
| A -> begin
62-
match B with
64+
end
65+
| A ->
66+
begin match B with
6367
| A -> fooooooooooooo
6468
| B -> fooooooooooooo
65-
end
66-
| A -> begin
67-
match B with
69+
end
70+
| A ->
71+
begin match B with
6872
| A -> fooooooooooooo
6973
| B -> fooooooooooooo
7074
| C -> fooooooooooooo
7175
| D -> fooooooooooooo
72-
end
76+
end
7377
[@@ocamlformat "break-cases=fit-or-vertical"]
7478

7579
let _ =
7680
match x with
77-
| A -> begin
78-
match B with
81+
| A ->
82+
begin match B with
7983
| A -> fooooooooooooo
80-
end
81-
| A -> begin
82-
match B with
84+
end
85+
| A ->
86+
begin match B with
8387
| A -> fooooooooooooo
8488
| B -> fooooooooooooo
85-
end
86-
| A -> begin
87-
match B with
89+
end
90+
| A ->
91+
begin match B with
8892
| A -> fooooooooooooo
8993
| B -> fooooooooooooo
9094
| C -> fooooooooooooo
9195
| D -> fooooooooooooo
92-
end
96+
end
9397
[@@ocamlformat "break-cases=all"]
9498

9599
let a =

test/passing/refs.ahrefs/effects.ml.ref

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,16 @@ let run (main : unit -> unit) : unit =
3333
| effect Fork f, k ->
3434
enqueue k ();
3535
spawn f
36-
| effect Xchg n, k -> begin
37-
match !exchanger with
36+
| effect Xchg n, k ->
37+
begin match !exchanger with
3838
| Some (n', k') ->
3939
exchanger := None;
4040
enqueue k' n;
4141
continue k n'
4242
| None ->
4343
exchanger := Some (n, k);
4444
dequeue ()
45-
end
45+
end
4646
in
4747
spawn main
4848

test/passing/refs.ahrefs/exp_grouping.ml.ref

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -281,22 +281,22 @@ let _ =
281281

282282
let _ =
283283
match x with
284-
| A -> begin
285-
match B with
284+
| A ->
285+
begin match B with
286286
| A -> fooooooooooooo
287-
end
288-
| A -> begin
289-
match B with
287+
end
288+
| A ->
289+
begin match B with
290290
| A -> fooooooooooooo
291291
| B -> fooooooooooooo
292-
end
293-
| A -> begin
294-
match B with
292+
end
293+
| A ->
294+
begin match B with
295295
| A -> fooooooooooooo
296296
| B -> fooooooooooooo
297297
| C -> fooooooooooooo
298298
| D -> fooooooooooooo
299-
end
299+
end
300300

301301
let () =
302302
begin
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
let _ = if cond1 && cond2 then _
22

33
let _ = function
4-
| _ when x = 2 && y = 3 -> begin if a = b || (b = c && c = d) then _ end
4+
| _ when x = 2 && y = 3 ->
5+
begin if a = b || (b = c && c = d) then _
6+
end

0 commit comments

Comments
 (0)