@@ -135,6 +135,7 @@ type cases =
135135 ; open_paren_branch : Fmt .t
136136 ; break_after_opening_paren : Fmt .t
137137 ; expr_parens : bool option
138+ ; branch_expr : expression Ast .xt
138139 ; close_paren_branch : Fmt .t }
139140
140141let get_cases (c : Conf.t ) ~ctx ~first ~last ~xbch :({ast; _} as xast ) =
@@ -165,24 +166,25 @@ let get_cases (c : Conf.t) ~ctx ~first ~last ~xbch:({ast; _} as xast) =
165166 else (parenze_exp xast && not body_has_parens, Some false )
166167 in
167168 let indent = if align_nested_match then 0 else indent in
168- let beginend =
169- match ast with {pexp_desc = Pexp_beginend _ ; _} -> true | _ -> false
170- in
171- let open_paren_branch =
172- if beginend then fmt " @;<1 0>begin" else fmt_if parens_branch " ("
173- in
174- let close_paren_branch =
175- if beginend then
176- let offset =
177- match c.fmt_opts.break_cases.v with `Nested -> 0 | _ -> - 2
178- in
179- fits_breaks " end" ~level: 1 ~hint: (1000 , offset) " end"
180- else
181- fmt_if_k parens_branch
182- ( match c.fmt_opts.indicate_multiline_delimiters.v with
183- | `Space -> fmt " @ )"
184- | `No -> fmt " @,)"
185- | `Closing_on_separate_line -> fmt " @;<1000 -2>)" )
169+ let open_paren_branch, close_paren_branch, branch_expr =
170+ match ast with
171+ | {pexp_desc = Pexp_beginend nested_exp ; pexp_attributes = [] ; _} ->
172+ let close_paren =
173+ let offset =
174+ match c.fmt_opts.break_cases.v with `Nested -> 0 | _ -> - 2
175+ in
176+ fits_breaks " end" ~level: 1 ~hint: (1000 , offset) " end"
177+ in
178+ (fmt " @;<1 0>begin" , close_paren, sub_exp ~ctx: (Exp ast) nested_exp)
179+ | _ ->
180+ let close_paren =
181+ fmt_if_k parens_branch
182+ ( match c.fmt_opts.indicate_multiline_delimiters.v with
183+ | `Space -> fmt " @ )"
184+ | `No -> fmt " @,)"
185+ | `Closing_on_separate_line -> fmt " @;<1000 -2>)" )
186+ in
187+ (fmt_if parens_branch " (" , close_paren, xast)
186188 in
187189 match c.fmt_opts.break_cases.v with
188190 | `Fit ->
@@ -195,6 +197,7 @@ let get_cases (c : Conf.t) ~ctx ~first ~last ~xbch:({ast; _} as xast) =
195197 ; open_paren_branch
196198 ; break_after_opening_paren= fmt " @ "
197199 ; expr_parens
200+ ; branch_expr
198201 ; close_paren_branch }
199202 | `Nested ->
200203 { leading_space= fmt_if (not first) " @ "
@@ -206,6 +209,7 @@ let get_cases (c : Conf.t) ~ctx ~first ~last ~xbch:({ast; _} as xast) =
206209 ; open_paren_branch
207210 ; break_after_opening_paren= fmt_or (indent > 2 ) " @;<1 4>" " @;<1 2>"
208211 ; expr_parens
212+ ; branch_expr
209213 ; close_paren_branch }
210214 | `Fit_or_vertical ->
211215 { leading_space= break_unless_newline 1000 0
@@ -217,6 +221,7 @@ let get_cases (c : Conf.t) ~ctx ~first ~last ~xbch:({ast; _} as xast) =
217221 ; open_paren_branch
218222 ; break_after_opening_paren= fmt " @ "
219223 ; expr_parens
224+ ; branch_expr
220225 ; close_paren_branch }
221226 | `Toplevel | `All ->
222227 { leading_space= break_unless_newline 1000 0
@@ -228,6 +233,7 @@ let get_cases (c : Conf.t) ~ctx ~first ~last ~xbch:({ast; _} as xast) =
228233 ; open_paren_branch
229234 ; break_after_opening_paren= fmt " @ "
230235 ; expr_parens
236+ ; branch_expr
231237 ; close_paren_branch }
232238 | `Vertical ->
233239 { leading_space= break_unless_newline 1000 0
@@ -239,6 +245,7 @@ let get_cases (c : Conf.t) ~ctx ~first ~last ~xbch:({ast; _} as xast) =
239245 ; open_paren_branch
240246 ; break_after_opening_paren= break 1000 0
241247 ; expr_parens
248+ ; branch_expr
242249 ; close_paren_branch }
243250
244251let wrap_collec c ~space_around opn cls =
0 commit comments