Skip to content

Commit e22e170

Browse files
also allow if then begin match
1 parent 4a211c1 commit e22e170

11 files changed

Lines changed: 337 additions & 64 deletions

File tree

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: 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
@@ -537,18 +559,13 @@ let get_cases (c : Conf.t) ~fmt_infix_ext_attrs ~ctx ~first ~last
537559
else (parenze_exp xast && not body_has_parens, Some false)
538560
in
539561
let indent = if align_nested_match then 0 else indent in
540-
let nested_exp_has_special_beginend exp =
541-
match exp with
542-
| Pexp_match _ | Pexp_try _ | Pexp_function _ | Pexp_ifthenelse _ -> true
543-
| _ -> false
544-
in
545562
let open_paren_branch, close_paren_branch, branch_expr =
546563
match ast with
547564
| { pexp_desc= Pexp_beginend (nested_exp, infix_ext_attrs)
548565
; pexp_attributes= []
549566
; _ }
550567
when (not cmts_before)
551-
&& not (nested_exp_has_special_beginend nested_exp.pexp_desc) ->
568+
&& not (is_special_beginend nested_exp.pexp_desc) ->
552569
let close_paren =
553570
let offset = if indent >= 2 then 2 - indent else 0 in
554571
fits_breaks " end" ~level:1 ~hint:(1000, offset) "end"
@@ -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
@@ -1706,24 +1706,24 @@ let rec ins : type n. int -> n avl -> (n avl, n succ avl) sum =
17061706
| Leaf -> Inr (Node (Same, Leaf, x, Leaf))
17071707
| Node (bal, a, y, b) ->
17081708
if x = y then Inl t
1709-
else if x < y then begin
1710-
match ins x a with
1709+
else if x < y then
1710+
begin match ins x a with
17111711
| Inl a -> Inl (Node (bal, a, y, b))
17121712
| Inr a ->
17131713
match bal with
17141714
| Less -> Inl (Node (Same, a, y, b))
17151715
| Same -> Inr (Node (More, a, y, b))
17161716
| More -> rotr a y b
1717-
end
1718-
else begin
1719-
match ins x b with
1717+
end
1718+
else
1719+
begin match ins x b with
17201720
| Inl b -> Inl (Node (bal, a, y, b) : n avl)
17211721
| Inr b ->
17221722
match bal with
17231723
| More -> Inl (Node (Same, a, y, b) : n avl)
17241724
| Same -> Inr (Node (Less, a, y, b) : n succ avl)
17251725
| Less -> rotl a y b
1726-
end
1726+
end
17271727

17281728
let insert x (Avl t) =
17291729
match ins x t with
@@ -1752,8 +1752,8 @@ let rec del : type n. int -> n avl -> n avl_del =
17521752
match t with
17531753
| Leaf -> Dsame Leaf
17541754
| Node (bal, l, x, r) ->
1755-
if x = y then begin
1756-
match r with
1755+
if x = y then
1756+
begin match r with
17571757
| Leaf ->
17581758
begin match bal with
17591759
| Same -> Ddecr (Eq, l)
@@ -1769,9 +1769,9 @@ let rec del : type n. int -> n avl -> n avl_del =
17691769
| Inl t -> Ddecr (Eq, t)
17701770
| Inr t -> Dsame t
17711771
end
1772-
end
1773-
else if y < x then begin
1774-
match del y l with
1772+
end
1773+
else if y < x then
1774+
begin match del y l with
17751775
| Dsame l -> Dsame (Node (bal, l, x, r))
17761776
| Ddecr (Eq, l) ->
17771777
begin match bal with
@@ -1782,9 +1782,9 @@ let rec del : type n. int -> n avl -> n avl_del =
17821782
| Inl t -> Ddecr (Eq, t)
17831783
| Inr t -> Dsame t
17841784
end
1785-
end
1786-
else begin
1787-
match del y r with
1785+
end
1786+
else
1787+
begin match del y r with
17881788
| Dsame r -> Dsame (Node (bal, l, x, r))
17891789
| Ddecr (Eq, r) ->
17901790
begin match bal with
@@ -1795,7 +1795,7 @@ let rec del : type n. int -> n avl -> n avl_del =
17951795
| Inl t -> Ddecr (Eq, t)
17961796
| Inr t -> Dsame t
17971797
end
1798-
end
1798+
end
17991799

18001800
let delete x (Avl t) =
18011801
match del x t with
@@ -6964,13 +6964,13 @@ module Bootstrap
69646964
| BE.E -> raise Not_found
69656965
| BE.H (x, p) ->
69666966
if PrimH.isEmpty p then BE.E
6967-
else begin
6968-
match PrimH.findMin p with
6967+
else
6968+
begin match PrimH.findMin p with
69696969
| BE.H (y, p1) ->
69706970
let p2 = PrimH.deleteMin p in
69716971
BE.H (y, PrimH.merge p1 p2)
69726972
| BE.E -> assert false
6973-
end
6973+
end
69746974
end
69756975

69766976
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
@@ -1631,24 +1631,24 @@ let rec ins : type n. int -> n avl -> (n avl, n succ avl) sum =
16311631
| Leaf -> Inr (Node (Same, Leaf, x, Leaf))
16321632
| Node (bal, a, y, b) ->
16331633
if x = y then Inl t
1634-
else if x < y then begin
1635-
match ins x a with
1634+
else if x < y then
1635+
begin match ins x a with
16361636
| Inl a -> Inl (Node (bal, a, y, b))
16371637
| Inr a -> (
16381638
match bal with
16391639
| Less -> Inl (Node (Same, a, y, b))
16401640
| Same -> Inr (Node (More, a, y, b))
16411641
| More -> rotr a y b)
1642-
end
1643-
else begin
1644-
match ins x b with
1642+
end
1643+
else
1644+
begin match ins x b with
16451645
| Inl b -> Inl (Node (bal, a, y, b) : n avl)
16461646
| Inr b -> (
16471647
match bal with
16481648
| More -> Inl (Node (Same, a, y, b) : n avl)
16491649
| Same -> Inr (Node (Less, a, y, b) : n succ avl)
16501650
| Less -> rotl a y b)
1651-
end
1651+
end
16521652

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

@@ -1674,8 +1674,8 @@ let rec del : type n. int -> n avl -> n avl_del =
16741674
match t with
16751675
| Leaf -> Dsame Leaf
16761676
| Node (bal, l, x, r) ->
1677-
if x = y then begin
1678-
match r with
1677+
if x = y then
1678+
begin match r with
16791679
| Leaf ->
16801680
begin match bal with Same -> Ddecr (Eq, l) | More -> Ddecr (Eq, l)
16811681
end
@@ -1689,9 +1689,9 @@ let rec del : type n. int -> n avl -> n avl_del =
16891689
| Inl t -> Ddecr (Eq, t)
16901690
| Inr t -> Dsame t)
16911691
end
1692-
end
1693-
else if y < x then begin
1694-
match del y l with
1692+
end
1693+
else if y < x then
1694+
begin match del y l with
16951695
| Dsame l -> Dsame (Node (bal, l, x, r))
16961696
| Ddecr (Eq, l) ->
16971697
begin match bal with
@@ -1702,9 +1702,9 @@ let rec del : type n. int -> n avl -> n avl_del =
17021702
| Inl t -> Ddecr (Eq, t)
17031703
| Inr t -> Dsame t)
17041704
end
1705-
end
1706-
else begin
1707-
match del y r with
1705+
end
1706+
else
1707+
begin match del y r with
17081708
| Dsame r -> Dsame (Node (bal, l, x, r))
17091709
| Ddecr (Eq, r) ->
17101710
begin match bal with
@@ -1715,7 +1715,7 @@ let rec del : type n. int -> n avl -> n avl_del =
17151715
| Inl t -> Ddecr (Eq, t)
17161716
| Inr t -> Dsame t)
17171717
end
1718-
end
1718+
end
17191719

17201720
let delete x (Avl t) =
17211721
match del x t with Dsame t -> Avl t | Ddecr (_, t) -> Avl t
@@ -7077,13 +7077,13 @@ module Bootstrap
70777077
| BE.E -> raise Not_found
70787078
| BE.H (x, p) ->
70797079
if PrimH.isEmpty p then BE.E
7080-
else begin
7081-
match PrimH.findMin p with
7080+
else
7081+
begin match PrimH.findMin p with
70827082
| BE.H (y, p1) ->
70837083
let p2 = PrimH.deleteMin p in
70847084
BE.H (y, PrimH.merge p1 p2)
70857085
| BE.E -> assert false
7086-
end
7086+
end
70877087
end
70887088

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

0 commit comments

Comments
 (0)