Skip to content

Commit 0a805f2

Browse files
use shortcut begin end syntax inside match and if then else (#2744)
* use special case beginend syntax in match cases when possible * also allow `if then begin match` * changelog * changelog * fmt
1 parent 6592d7f commit 0a805f2

25 files changed

+679
-363
lines changed

CHANGES.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,21 @@ profile. This started with version 0.26.0.
8181
| () -> begin
8282
match () with
8383
| () -> ()
84+
85+
- \* use shortcut `begin end` in `match` cases and `if then else` body. (#2744, @EmileTrotignon)
86+
```ocaml
87+
(* before *)
88+
match () with
89+
| () -> begin
90+
match () with
91+
| () ->
92+
end
93+
end
94+
(* after *)
95+
match () with
96+
| () ->
97+
begin match () with
98+
| () ->
8499
end
85100
end
86101
```

lib/Fmt_ast.ml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3053,8 +3053,7 @@ and fmt_beginend c ~loc ?(box = true) ?(pro = noop) ~ctx ~ctx0 ~fmt_atrs
30533053
let cmts_before = Cmts.fmt_before c ?eol loc in
30543054
let begin_ = fmt_infix_ext_attrs c ~pro:(str "begin") infix_ext_attrs
30553055
and end_ =
3056-
(if not box then break 1000 (-2) else break 1000 0)
3057-
$ str "end" $ fmt_atrs
3056+
Params.Exp.end_break_beginend ~ctx0 ~box $ str "end" $ fmt_atrs
30583057
in
30593058
let box_beginend_sb = Params.Exp.box_beginend_subexpr c.conf ~ctx ~ctx0 in
30603059
let beginend_box =

lib/Params.ml

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,13 @@ module Exp = struct
354354
hvbox (2 - String.length "begin ")
355355
| _ -> Fn.id
356356

357+
let end_break_beginend ~ctx0 ~box =
358+
if box then break 1000 0
359+
else
360+
match ctx0 with
361+
| Exp {pexp_desc= Pexp_ifthenelse _; _} -> break 1000 0
362+
| _ -> break 1000 (-2)
363+
357364
let box_beginend c ~ctx0 ~ctx =
358365
let contains_fun =
359366
match ctx with
@@ -488,6 +495,21 @@ let get_or_pattern_sep ?(cmts_before = false) ?(space = false) (c : Conf.t)
488495
~breaks:("", 0, if space then " | " else " |")
489496
| `Unsafe_no -> break nspaces 0 $ str "| " )
490497

498+
(** [is_special_beginend exp] returns true if [begin `exp` end] can be formatted
499+
as
500+
{[begin abc
501+
...
502+
end]}
503+
instead of
504+
{[begin
505+
abc
506+
...
507+
end]}*)
508+
let is_special_beginend exp =
509+
match exp with
510+
| Pexp_match _ | Pexp_try _ | Pexp_function _ | Pexp_ifthenelse _ -> true
511+
| _ -> false
512+
491513
type cases =
492514
{ leading_space: Fmt.t
493515
; bar: Fmt.t
@@ -542,7 +564,8 @@ let get_cases (c : Conf.t) ~fmt_infix_ext_attrs ~ctx ~first ~last
542564
| { pexp_desc= Pexp_beginend (nested_exp, infix_ext_attrs)
543565
; pexp_attributes= []
544566
; _ }
545-
when not cmts_before ->
567+
when (not cmts_before)
568+
&& not (is_special_beginend nested_exp.pexp_desc) ->
546569
let close_paren =
547570
let offset = if indent >= 2 then 2 - indent else 0 in
548571
fits_breaks " end" ~level:1 ~hint:(1000, offset) "end"
@@ -831,6 +854,9 @@ let get_if_then_else (c : Conf.t) ~pro ~first ~last ~parens_bch
831854
let beginend_loc, infix_ext_attrs_beginend, branch_expr =
832855
let ast = xbch.Ast.ast in
833856
match ast with
857+
| {pexp_desc= Pexp_beginend ({pexp_desc; _}, _); _}
858+
when is_special_beginend pexp_desc ->
859+
(None, None, xbch)
834860
| { pexp_desc= Pexp_beginend (nested_exp, infix_ext_attrs)
835861
; pexp_attributes= []
836862
; pexp_loc

lib/Params.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ module Exp : sig
8484
val box_fun_decl_after_pro : ctx0:Ast.t -> Fmt.t -> Fmt.t
8585
(** Box a function decl from after the [pro] to the arrow. *)
8686

87+
val end_break_beginend : ctx0:Ast.t -> box:bool -> Fmt.t
88+
8789
val box_beginend : Conf.t -> ctx0:Ast.t -> ctx:Ast.t -> bool
8890

8991
val box_beginend_subexpr : Conf.t -> ctx0:Ast.t -> ctx:Ast.t -> bool

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

Lines changed: 79 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
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
@@ -13,14 +17,14 @@ let _ =
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,17 +38,17 @@ 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
4044
end
41-
| A -> begin
42-
match B with
45+
| A ->
46+
begin match B with
4347
| A -> fooooooooooooo
4448
| B -> fooooooooooooo
4549
end
46-
| A -> begin
47-
match B with
50+
| A ->
51+
begin match B with
4852
| A -> fooooooooooooo
4953
| B -> fooooooooooooo
5054
| C -> fooooooooooooo
@@ -54,17 +58,17 @@ let _ =
5458

5559
let _ =
5660
match x with
57-
| A -> begin
58-
match B with
61+
| A ->
62+
begin match B with
5963
| A -> fooooooooooooo
6064
end
61-
| A -> begin
62-
match B with
65+
| A ->
66+
begin match B with
6367
| A -> fooooooooooooo
6468
| B -> fooooooooooooo
6569
end
66-
| A -> begin
67-
match B with
70+
| A ->
71+
begin match B with
6872
| A -> fooooooooooooo
6973
| B -> fooooooooooooo
7074
| C -> fooooooooooooo
@@ -74,17 +78,17 @@ let _ =
7478

7579
let _ =
7680
match x with
77-
| A -> begin
78-
match B with
81+
| A ->
82+
begin match B with
7983
| A -> fooooooooooooo
8084
end
81-
| A -> begin
82-
match B with
85+
| A ->
86+
begin match B with
8387
| A -> fooooooooooooo
8488
| B -> fooooooooooooo
8589
end
86-
| A -> begin
87-
match B with
90+
| A ->
91+
begin match B with
8892
| A -> fooooooooooooo
8993
| B -> fooooooooooooo
9094
| C -> fooooooooooooo
@@ -211,3 +215,48 @@ let a =
211215
| A -> f aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
212216
| B -> bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
213217
end
218+
219+
let () =
220+
if true then
221+
begin match () with
222+
| () -> ()
223+
| aaaaaaaaaaaa -> aaaaaaaaaaa
224+
| bbbbbbbbbb -> bbbbbbbbbbbbbbbbb
225+
end
226+
else
227+
begin match () with
228+
| () -> ()
229+
| aaaaaaaaaaaa -> aaaaaaaaaaa
230+
| bbbbbbbbbb -> bbbbbbbbbbbbbbbbb
231+
end
232+
233+
let () =
234+
(* this is ugly but should never be used. *)
235+
if true then begin
236+
begin match () with
237+
| () -> ()
238+
| aaaaaaaaaaaa -> aaaaaaaaaaa
239+
| bbbbbbbbbb -> bbbbbbbbbbbbbbbbb
240+
end
241+
end
242+
243+
let () =
244+
(* Add a third one and it has indentation on `end`. *)
245+
if true then begin begin
246+
begin match () with
247+
| () -> ()
248+
| aaaaaaaaaaaa -> aaaaaaaaaaa
249+
| bbbbbbbbbb -> bbbbbbbbbbbbbbbbb
250+
end
251+
end
252+
end
253+
254+
let () =
255+
if true then begin
256+
();
257+
begin match () with
258+
| () -> ()
259+
| aaaaaaaaaaaa -> aaaaaaaaaaa
260+
| bbbbbbbbbb -> bbbbbbbbbbbbbbbbb
261+
end
262+
end

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ let () =
99
| () ->
1010
aaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaa aaaaaaaaaaaaaa aaaaaaaaaaaa
1111
aaaaaaaaaaaaa ()
12-
| () -> begin
13-
match () with
12+
| () ->
13+
begin match () with
1414
| () -> ()
15-
end
15+
end
1616
| () ->
1717
(match () with
1818
| () -> ())
19-
| () -> begin
20-
match () with
19+
| () ->
20+
begin match () with
2121
| () -> ()
22-
end
22+
end
2323
end

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ 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;

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -281,17 +281,17 @@ 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
287287
end
288-
| A -> begin
289-
match B with
288+
| A ->
289+
begin match B with
290290
| A -> fooooooooooooo
291291
| B -> fooooooooooooo
292292
end
293-
| A -> begin
294-
match B with
293+
| A ->
294+
begin match B with
295295
| A -> fooooooooooooo
296296
| B -> fooooooooooooo
297297
| C -> fooooooooooooo
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)