Skip to content

Commit 155b1cf

Browse files
also allow if then begin match
1 parent ecf1eab commit 155b1cf

File tree

11 files changed

+337
-64
lines changed

11 files changed

+337
-64
lines changed

lib/Fmt_ast.ml

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

lib/Params.ml

Lines changed: 29 additions & 7 deletions
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
@@ -535,18 +557,13 @@ let get_cases (c : Conf.t) ~fmt_infix_ext_attrs ~ctx ~first ~last
535557
else (parenze_exp xast && not body_has_parens, Some false)
536558
in
537559
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
543560
let open_paren_branch, close_paren_branch, branch_expr =
544561
match ast with
545562
| { pexp_desc= Pexp_beginend (nested_exp, infix_ext_attrs)
546563
; pexp_attributes= []
547564
; _ }
548565
when (not cmts_before)
549-
&& not (nested_exp_has_special_beginend nested_exp.pexp_desc) ->
566+
&& not (is_special_beginend nested_exp.pexp_desc) ->
550567
let close_paren =
551568
let offset =
552569
match c.fmt_opts.break_cases.v with `Nested -> 0 | _ -> -2
@@ -834,9 +851,14 @@ let get_if_then_else (c : Conf.t) ~pro ~first ~last ~parens_bch
834851
~parens_prev_bch ~xcond ~xbch ~expr_loc ~fmt_infix_ext_attrs
835852
~infix_ext_attrs ~fmt_cond ~cmts_before_kw ~cmts_after_kw =
836853
let imd = c.fmt_opts.indicate_multiline_delimiters.v in
837-
let beginend_loc, infix_ext_attrs_beginend, branch_expr =
854+
let ( beginend_loc
855+
, infix_ext_attrs_beginend
856+
, branch_expr ) =
838857
let ast = xbch.Ast.ast in
839858
match ast with
859+
| {pexp_desc= Pexp_beginend ({pexp_desc; _}, _); _}
860+
when is_special_beginend pexp_desc ->
861+
(None, None, xbch)
840862
| { pexp_desc= Pexp_beginend (nested_exp, infix_ext_attrs)
841863
; pexp_attributes= []
842864
; 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: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,3 +215,48 @@ let a =
215215
| A -> f aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
216216
| B -> bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
217217
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/source.ml.ref

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1708,24 +1708,24 @@ let rec ins : type n. int -> n avl -> (n avl, n succ avl) sum =
17081708
| Leaf -> Inr (Node (Same, Leaf, x, Leaf))
17091709
| Node (bal, a, y, b) ->
17101710
if x = y then Inl t
1711-
else if x < y then begin
1712-
match ins x a with
1711+
else if x < y then
1712+
begin match ins x a with
17131713
| Inl a -> Inl (Node (bal, a, y, b))
17141714
| Inr a ->
17151715
match bal with
17161716
| Less -> Inl (Node (Same, a, y, b))
17171717
| Same -> Inr (Node (More, a, y, b))
17181718
| More -> rotr a y b
1719-
end
1720-
else begin
1721-
match ins x b with
1719+
end
1720+
else
1721+
begin match ins x b with
17221722
| Inl b -> Inl (Node (bal, a, y, b) : n avl)
17231723
| Inr b ->
17241724
match bal with
17251725
| More -> Inl (Node (Same, a, y, b) : n avl)
17261726
| Same -> Inr (Node (Less, a, y, b) : n succ avl)
17271727
| Less -> rotl a y b
1728-
end
1728+
end
17291729

17301730
let insert x (Avl t) =
17311731
match ins x t with
@@ -1754,8 +1754,8 @@ let rec del : type n. int -> n avl -> n avl_del =
17541754
match t with
17551755
| Leaf -> Dsame Leaf
17561756
| Node (bal, l, x, r) ->
1757-
if x = y then begin
1758-
match r with
1757+
if x = y then
1758+
begin match r with
17591759
| Leaf ->
17601760
begin match bal with
17611761
| Same -> Ddecr (Eq, l)
@@ -1771,9 +1771,9 @@ let rec del : type n. int -> n avl -> n avl_del =
17711771
| Inl t -> Ddecr (Eq, t)
17721772
| Inr t -> Dsame t
17731773
end
1774-
end
1775-
else if y < x then begin
1776-
match del y l with
1774+
end
1775+
else if y < x then
1776+
begin match del y l with
17771777
| Dsame l -> Dsame (Node (bal, l, x, r))
17781778
| Ddecr (Eq, l) ->
17791779
begin match bal with
@@ -1784,9 +1784,9 @@ let rec del : type n. int -> n avl -> n avl_del =
17841784
| Inl t -> Ddecr (Eq, t)
17851785
| Inr t -> Dsame t
17861786
end
1787-
end
1788-
else begin
1789-
match del y r with
1787+
end
1788+
else
1789+
begin match del y r with
17901790
| Dsame r -> Dsame (Node (bal, l, x, r))
17911791
| Ddecr (Eq, r) ->
17921792
begin match bal with
@@ -1797,7 +1797,7 @@ let rec del : type n. int -> n avl -> n avl_del =
17971797
| Inl t -> Ddecr (Eq, t)
17981798
| Inr t -> Dsame t
17991799
end
1800-
end
1800+
end
18011801

18021802
let delete x (Avl t) =
18031803
match del x t with
@@ -6966,13 +6966,13 @@ module Bootstrap
69666966
| BE.E -> raise Not_found
69676967
| BE.H (x, p) ->
69686968
if PrimH.isEmpty p then BE.E
6969-
else begin
6970-
match PrimH.findMin p with
6969+
else
6970+
begin match PrimH.findMin p with
69716971
| BE.H (y, p1) ->
69726972
let p2 = PrimH.deleteMin p in
69736973
BE.H (y, PrimH.merge p1 p2)
69746974
| BE.E -> assert false
6975-
end
6975+
end
69766976
end
69776977

69786978
module LeftistHeap (Element : ORDERED) : HEAP with module Elem = Element =

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,3 +211,48 @@ let a =
211211
| A -> f aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
212212
| B -> bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
213213
end
214+
215+
let () =
216+
if true then
217+
begin match () with
218+
| () -> ()
219+
| aaaaaaaaaaaa -> aaaaaaaaaaa
220+
| bbbbbbbbbb -> bbbbbbbbbbbbbbbbb
221+
end
222+
else
223+
begin match () with
224+
| () -> ()
225+
| aaaaaaaaaaaa -> aaaaaaaaaaa
226+
| bbbbbbbbbb -> bbbbbbbbbbbbbbbbb
227+
end
228+
229+
let () =
230+
(* this is ugly but should never be used. *)
231+
if true then begin
232+
begin match () with
233+
| () -> ()
234+
| aaaaaaaaaaaa -> aaaaaaaaaaa
235+
| bbbbbbbbbb -> bbbbbbbbbbbbbbbbb
236+
end
237+
end
238+
239+
let () =
240+
(* Add a third one and it has indentation on `end`. *)
241+
if true then begin begin
242+
begin match () with
243+
| () -> ()
244+
| aaaaaaaaaaaa -> aaaaaaaaaaa
245+
| bbbbbbbbbb -> bbbbbbbbbbbbbbbbb
246+
end
247+
end
248+
end
249+
250+
let () =
251+
if true then begin
252+
();
253+
begin match () with
254+
| () -> ()
255+
| aaaaaaaaaaaa -> aaaaaaaaaaa
256+
| bbbbbbbbbb -> bbbbbbbbbbbbbbbbb
257+
end
258+
end

test/passing/refs.default/source.ml.ref

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1633,24 +1633,24 @@ let rec ins : type n. int -> n avl -> (n avl, n succ avl) sum =
16331633
| Leaf -> Inr (Node (Same, Leaf, x, Leaf))
16341634
| Node (bal, a, y, b) ->
16351635
if x = y then Inl t
1636-
else if x < y then begin
1637-
match ins x a with
1636+
else if x < y then
1637+
begin match ins x a with
16381638
| Inl a -> Inl (Node (bal, a, y, b))
16391639
| Inr a -> (
16401640
match bal with
16411641
| Less -> Inl (Node (Same, a, y, b))
16421642
| Same -> Inr (Node (More, a, y, b))
16431643
| More -> rotr a y b)
1644-
end
1645-
else begin
1646-
match ins x b with
1644+
end
1645+
else
1646+
begin match ins x b with
16471647
| Inl b -> Inl (Node (bal, a, y, b) : n avl)
16481648
| Inr b -> (
16491649
match bal with
16501650
| More -> Inl (Node (Same, a, y, b) : n avl)
16511651
| Same -> Inr (Node (Less, a, y, b) : n succ avl)
16521652
| Less -> rotl a y b)
1653-
end
1653+
end
16541654

16551655
let insert x (Avl t) = match ins x t with Inl t -> Avl t | Inr t -> Avl t
16561656

@@ -1676,8 +1676,8 @@ let rec del : type n. int -> n avl -> n avl_del =
16761676
match t with
16771677
| Leaf -> Dsame Leaf
16781678
| Node (bal, l, x, r) ->
1679-
if x = y then begin
1680-
match r with
1679+
if x = y then
1680+
begin match r with
16811681
| Leaf ->
16821682
begin match bal with Same -> Ddecr (Eq, l) | More -> Ddecr (Eq, l)
16831683
end
@@ -1691,9 +1691,9 @@ let rec del : type n. int -> n avl -> n avl_del =
16911691
| Inl t -> Ddecr (Eq, t)
16921692
| Inr t -> Dsame t)
16931693
end
1694-
end
1695-
else if y < x then begin
1696-
match del y l with
1694+
end
1695+
else if y < x then
1696+
begin match del y l with
16971697
| Dsame l -> Dsame (Node (bal, l, x, r))
16981698
| Ddecr (Eq, l) ->
16991699
begin match bal with
@@ -1704,9 +1704,9 @@ let rec del : type n. int -> n avl -> n avl_del =
17041704
| Inl t -> Ddecr (Eq, t)
17051705
| Inr t -> Dsame t)
17061706
end
1707-
end
1708-
else begin
1709-
match del y r with
1707+
end
1708+
else
1709+
begin match del y r with
17101710
| Dsame r -> Dsame (Node (bal, l, x, r))
17111711
| Ddecr (Eq, r) ->
17121712
begin match bal with
@@ -1717,7 +1717,7 @@ let rec del : type n. int -> n avl -> n avl_del =
17171717
| Inl t -> Ddecr (Eq, t)
17181718
| Inr t -> Dsame t)
17191719
end
1720-
end
1720+
end
17211721

17221722
let delete x (Avl t) =
17231723
match del x t with Dsame t -> Avl t | Ddecr (_, t) -> Avl t
@@ -7079,13 +7079,13 @@ module Bootstrap
70797079
| BE.E -> raise Not_found
70807080
| BE.H (x, p) ->
70817081
if PrimH.isEmpty p then BE.E
7082-
else begin
7083-
match PrimH.findMin p with
7082+
else
7083+
begin match PrimH.findMin p with
70847084
| BE.H (y, p1) ->
70857085
let p2 = PrimH.deleteMin p in
70867086
BE.H (y, PrimH.merge p1 p2)
70877087
| BE.E -> assert false
7088-
end
7088+
end
70897089
end
70907090

70917091
module LeftistHeap (Element : ORDERED) : HEAP with module Elem = Element =

0 commit comments

Comments
 (0)