@@ -145,7 +145,11 @@ module Exp = struct
145145 | Pexp_apply (_ , args ) -> (
146146 (* Rhs is an apply and it ends with a [fun]. *)
147147 match List. last_exn args with
148- | _ , {pexp_desc = Pexp_function _ ; _} -> true
148+ | _, {pexp_desc= Pexp_function _; _}
149+ | ( _
150+ , { pexp_desc= Pexp_beginend ({pexp_desc= Pexp_function _; _}, _)
151+ ; _ } ) ->
152+ true
149153 | _ -> false )
150154 | Pexp_match _ | Pexp_try _ -> true
151155 | _ -> false
@@ -172,10 +176,16 @@ module Exp = struct
172176 | Some ((Labelled _ | Optional _ ), _ , _ ) -> true
173177 | _ -> false
174178 in
179+ let is_ctx_beginend =
180+ match ctx0 with
181+ | Exp {pexp_desc = Pexp_beginend _ ; _} -> true
182+ | _ -> false
183+ in
175184 if Conf. (c.fmt_opts.ocp_indent_compat.v) then
176185 if last_arg || is_labelled_arg then break 1 2 else str " "
177186 else if is_labelled_arg then break 1 2
178187 else if last_arg then break 1 0
188+ else if is_ctx_beginend then break 1 0
179189 else str " "
180190
181191 let box_fun_decl_args ~ctx ~ctx0 ?(last_arg = false ) ?epi c ~parens ~kw
@@ -224,38 +234,42 @@ module Exp = struct
224234 $ hvbox_if should_box_args 0 (args $ fmt_opt annot $ fmt_opt epi) )
225235
226236 let box_fun_expr (c : Conf.t ) ~source ~ctx0 ~ctx =
227- let indent =
228- if ctx_is_rhs_of_infix ~ctx0 ~ctx then 0
229- else if Poly. equal c.fmt_opts.function_indent_nested.v `Always then
230- c.fmt_opts.function_indent.v
231- else if ctx_is_let_or_fun ~ctx ctx0 then
232- if c.fmt_opts.let_binding_deindent_fun.v then 1 else 0
233- else if ocp c then
234- let begins_line loc =
235- Source. begins_line ~ignore_spaces: true source loc
237+ match ctx0 with
238+ | Exp {pexp_desc = Pexp_beginend _ ; _} -> (Fn. id, 0 )
239+ | _ ->
240+ let indent =
241+ if ctx_is_rhs_of_infix ~ctx0 ~ctx then 0
242+ else if Poly. equal c.fmt_opts.function_indent_nested.v `Always then
243+ c.fmt_opts.function_indent.v
244+ else if ctx_is_let_or_fun ~ctx ctx0 then
245+ if c.fmt_opts.let_binding_deindent_fun.v then 1 else 0
246+ else if ocp c then
247+ let begins_line loc =
248+ Source. begins_line ~ignore_spaces: true source loc
249+ in
250+ match ctx_is_apply_and_exp_is_arg ~ctx ~ctx0 with
251+ | Some (Nolabel, fun_exp , is_last_arg ) ->
252+ if begins_line fun_exp.pexp_loc then
253+ if is_last_arg then 5 else 3
254+ else 2
255+ | Some ((Labelled x | Optional x ), fun_exp , is_last_arg ) ->
256+ if begins_line fun_exp.pexp_loc then
257+ (* The [fun] had to break after the label, nested boxes
258+ must be indented less. The last argument is special as
259+ the box structure is different. *)
260+ if is_last_arg then 4 else 2
261+ else if begins_line x.loc then 4
262+ else 2
263+ | None -> if ctx_is_apply_and_exp_is_func ~ctx ctx0 then 3 else 2
264+ else if
265+ ctx_is_apply_and_exp_is_last_arg_and_other_args_are_simple c ~ctx
266+ ~ctx0
267+ then 4
268+ else 2
236269 in
237- match ctx_is_apply_and_exp_is_arg ~ctx ~ctx0 with
238- | Some (Nolabel, fun_exp , is_last_arg ) ->
239- if begins_line fun_exp.pexp_loc then if is_last_arg then 5 else 3
240- else 2
241- | Some ((Labelled x | Optional x ), fun_exp , is_last_arg ) ->
242- if begins_line fun_exp.pexp_loc then
243- (* The [fun] had to break after the label, nested boxes must be
244- indented less. The last argument is special as the box
245- structure is different. *)
246- if is_last_arg then 4 else 2
247- else if begins_line x.loc then 4
248- else 2
249- | None -> if ctx_is_apply_and_exp_is_func ~ctx ctx0 then 3 else 2
250- else if
251- ctx_is_apply_and_exp_is_last_arg_and_other_args_are_simple c ~ctx
252- ~ctx0
253- then 4
254- else 2
255- in
256- let name = " Params.box_fun_expr" in
257- let mkbox = if ctx_is_let_or_fun ~ctx ctx0 then hvbox else hovbox in
258- (mkbox ~name indent, ~- indent)
270+ let name = " Params.box_fun_expr" in
271+ let mkbox = if ctx_is_let_or_fun ~ctx ctx0 then hvbox else hovbox in
272+ (mkbox ~name indent, ~- indent)
259273
260274 (* if the function is the last argument of an apply and no other arguments
261275 are "complex" (approximation). *)
@@ -317,6 +331,7 @@ module Exp = struct
317331
318332 let box_fun_decl ~ctx0 c k =
319333 match ctx0 with
334+ | Exp {pexp_desc = Pexp_beginend _ ; _} -> hovbox 2 k
320335 | _ when ocp c -> hvbox 2 k
321336 (* Avoid large indentation for [let _ = function]. *)
322337 | Lb
@@ -326,6 +341,30 @@ module Exp = struct
326341 | Str _ | Lb _ | Clf _ | Exp {pexp_desc = Pexp_let _ ; _} -> hovbox 4 k
327342 | _ -> hvbox 2 k
328343
344+ let box_fun_decl_after_pro ~ctx0 =
345+ match ctx0 with
346+ | Exp {pexp_desc = Pexp_beginend _ ; _} ->
347+ hvbox (2 - String. length " begin " )
348+ | _ -> Fn. id
349+
350+ let box_beginend c ~ctx0 ~ctx =
351+ let contains_fun =
352+ match ctx with
353+ | Exp {pexp_desc= Pexp_beginend ({pexp_desc= Pexp_function _; _}, _); _}
354+ ->
355+ true
356+ | _ -> false
357+ in
358+ contains_fun
359+ && not
360+ (ctx_is_apply_and_exp_is_last_arg_and_other_args_are_simple c ~ctx
361+ ~ctx0 )
362+
363+ let box_beginend_subexpr c ~ctx0 ~ctx =
364+ not
365+ (ctx_is_apply_and_exp_is_last_arg_and_other_args_are_simple c ~ctx
366+ ~ctx0 )
367+
329368 let match_inner_pro ~ctx0 ~parens =
330369 if parens then false
331370 else
@@ -854,7 +893,7 @@ let get_if_then_else (c : Conf.t) ~pro ~first ~last ~parens_bch
854893 ; box_keyword_and_expr= Fn. id
855894 ; branch_pro= branch_pro ()
856895 ; wrap_parens= wrap_parens ~wrap_breaks: (wrap (break 1000 2 ) noop)
857- ; box_expr= Some false
896+ ; box_expr= Some beginend
858897 ; expr_pro= None
859898 ; expr_eol= Some (break 1 2 )
860899 ; branch_expr
0 commit comments