@@ -1496,7 +1496,7 @@ and fmt_indexop_access c ctx ~fmt_atrs ~has_attr ~parens x =
14961496 responsible for breaking. *)
14971497and fmt_function ?(last_arg = false ) ?force_closing_paren ~ctx ~ctx0 ?pro
14981498 ~wrap_intro ?box :(should_box = true ) ~label ?(parens = false ) ?ext ~attrs
1499- ~loc c (args, typ, body) =
1499+ ~loc ?(epi = noop) c (args, typ, body) =
15001500 let should_box =
15011501 should_box
15021502 ||
@@ -1649,10 +1649,10 @@ and fmt_function ?(last_arg = false) ?force_closing_paren ~ctx ~ctx0 ?pro
16491649 $ hvbox_if has_cmts_outer 0
16501650 ( cmts_outer
16511651 $ Params.Exp. box_fun_decl ~ctx0 c.conf
1652- ( pro_inner $ fmt_label label label_sep $ cmts_inner
1652+ ( fmt_label label label_sep $ pro_inner $ cmts_inner
16531653 $ opn_paren $ head ) ) )
16541654 in
1655- body ~pro $ cls_paren
1655+ body ~pro $ epi $ cls_paren
16561656 in
16571657 let box k = if should_box then box k else k in
16581658 box (disambiguate_parens_wrap body) $ Cmts. fmt_after c loc
@@ -2188,78 +2188,9 @@ and fmt_expression c ?(box = true) ?(pro = noop) ?eol ?parens
21882188 ( fmt_str_loc c op $ fmt_if has_cmts cut_break
21892189 $ fmt_expression c ~box (sub_exp ~ctx e)
21902190 $ fmt_atrs ) )
2191- | Pexp_apply (e0 , e1N1 ) -> (
2192- let wrap =
2193- if c.conf.fmt_opts.wrap_fun_args.v then hovbox 2 else hvbox 2
2194- in
2195- let (lbl, last_arg), args_before =
2196- match List. rev e1N1 with
2197- | [] -> assert false
2198- | hd :: tl -> (hd, List. rev tl)
2199- in
2200- let intro_epi, expr_epi =
2201- (* [intro_epi] should be placed inside the inner most box but before
2202- anything. [expr_epi] is placed in the outermost box, outside of
2203- parenthesis. *)
2204- let dock_fun_arg =
2205- (* Do not dock the arguments when there's more than one. *)
2206- (not c.conf.fmt_opts.ocp_indent_compat.v)
2207- || Location. line_difference e0.pexp_loc last_arg.pexp_loc = 0
2208- in
2209- if parens || not dock_fun_arg then (noop, pro) else (pro, noop)
2210- in
2211- match last_arg.pexp_desc with
2212- | Pexp_function (largs, ltyp, lbody)
2213- when List. for_all args_before ~f: (fun (_ , eI ) ->
2214- is_simple c.conf (fun _ -> 0 ) (sub_exp ~ctx eI) ) ->
2215- let inner_ctx = Exp last_arg in
2216- let inner_parens, outer_parens =
2217- (* Don't disambiguate parentheses in some cases, also affect
2218- indentation. *)
2219- match lbody with
2220- | Pfunction_cases _ when not c.conf.fmt_opts.ocp_indent_compat.v
2221- ->
2222- (parens, false )
2223- | _ -> (false , parens)
2224- in
2225- let args =
2226- let wrap_intro x =
2227- fmt_if inner_parens (str " (" )
2228- $ hvbox 0
2229- ( intro_epi
2230- $ wrap
2231- ( fmt_args_grouped e0 args_before
2232- $ break 1 0 $ hvbox 0 x ) )
2233- $ break 1 0
2234- in
2235- let force_closing_paren =
2236- if Location. is_single_line pexp_loc c.conf.fmt_opts.margin.v
2237- then Fit
2238- else Break
2239- in
2240- fmt_function ~last_arg: true ~force_closing_paren ~ctx: inner_ctx
2241- ~ctx0: ctx ~wrap_intro ~label: lbl ~parens: true
2242- ~attrs: last_arg.pexp_attributes ~loc: last_arg.pexp_loc c
2243- (largs, ltyp, lbody)
2244- in
2245- hvbox_if has_attr 0
2246- ( expr_epi
2247- $ Params. parens_if outer_parens c.conf
2248- (args $ fmt_atrs $ fmt_if inner_parens (str " )" )) )
2249- | _ ->
2250- let fmt_atrs =
2251- fmt_attributes c ~pre: (Break (1 , - 2 )) pexp_attributes
2252- in
2253- let force =
2254- if Location. is_single_line pexp_loc c.conf.fmt_opts.margin.v then
2255- Fit
2256- else Break
2257- in
2258- pro
2259- $ fmt_if parens (str " (" )
2260- $ hvbox 2
2261- ( fmt_args_grouped ~epi: fmt_atrs e0 e1N1
2262- $ fmt_if parens (closing_paren c ~force ~offset: (- 3 )) ) )
2191+ | Pexp_apply (e0 , e1N1 ) ->
2192+ fmt_apply c ~e1 N1 ~parens ~pro ~ctx ~e0 ~pexp_loc ~pexp_attributes
2193+ ~has_attr ~fmt_atrs ~fmt_args_grouped
22632194 | Pexp_array [] ->
22642195 pro
22652196 $ hvbox 0
@@ -2927,6 +2858,102 @@ and fmt_expression c ?(box = true) ?(pro = noop) ?eol ?parens
29272858 (sub_exp ~ctx e) )
29282859 $ fmt_atrs
29292860
2861+ and fmt_apply c ~e1N1 ~parens ~pro ~ctx ~e0 ~pexp_loc ~pexp_attributes
2862+ ~has_attr ~fmt_atrs ~fmt_args_grouped =
2863+ let wrap = if c.conf.fmt_opts.wrap_fun_args.v then hovbox 2 else hvbox 2 in
2864+ let (lbl, last_arg), args_before =
2865+ match List. rev e1N1 with
2866+ | [] -> assert false
2867+ | hd :: tl -> (hd, List. rev tl)
2868+ in
2869+ let intro_epi, expr_epi =
2870+ (* [intro_epi] should be placed inside the inner most box but before
2871+ anything. [expr_epi] is placed in the outermost box, outside of
2872+ parenthesis. *)
2873+ let dock_fun_arg =
2874+ (* Do not dock the arguments when there's more than one. *)
2875+ (not c.conf.fmt_opts.ocp_indent_compat.v)
2876+ || Location. line_difference e0.pexp_loc last_arg.pexp_loc = 0
2877+ in
2878+ if parens || not dock_fun_arg then (noop, pro) else (pro, noop)
2879+ in
2880+ match last_arg.pexp_desc with
2881+ | Pexp_function (largs, ltyp, lbody)
2882+ when List. for_all args_before ~f: (fun (_ , eI ) ->
2883+ is_simple c.conf (fun _ -> 0 ) (sub_exp ~ctx eI) ) ->
2884+ fmt_apply_last_arg_function c ~last_arg ~lbody ~parens ~intro_epi
2885+ ~pexp_loc ~lbl ~ctx ~largs ~has_attr ~expr_epi ~fmt_atrs ~ltyp ~e0
2886+ ~fmt_args_grouped ~args_before ~wrap ~beginend: None
2887+ | Pexp_beginend
2888+ ( { pexp_desc= Pexp_function (largs, ltyp, lbody)
2889+ ; pexp_attributes= attrs_beginend
2890+ ; _ } as e_func ) ->
2891+ fmt_apply_last_arg_function c ~last_arg ~lbody ~parens ~intro_epi
2892+ ~pexp_loc ~lbl ~ctx ~largs ~has_attr ~expr_epi ~fmt_atrs ~ltyp ~e0
2893+ ~fmt_args_grouped ~args_before ~wrap
2894+ ~beginend: (Some (attrs_beginend, Exp e_func))
2895+ | _ ->
2896+ let fmt_atrs = fmt_attributes c ~pre: (Break (1 , - 2 )) pexp_attributes in
2897+ let force =
2898+ if Location. is_single_line pexp_loc c.conf.fmt_opts.margin.v then Fit
2899+ else Break
2900+ in
2901+ pro
2902+ $ fmt_if parens (str " (" )
2903+ $ hvbox 2
2904+ ( fmt_args_grouped ~epi: fmt_atrs e0 e1N1
2905+ $ fmt_if parens (closing_paren c ~force ~offset: (- 3 )) )
2906+
2907+ and fmt_apply_last_arg_function c ~last_arg ~lbody ~parens ~intro_epi
2908+ ~pexp_loc ~lbl ~ctx ~largs ~has_attr ~expr_epi ~fmt_atrs ~ltyp ~e0
2909+ ~fmt_args_grouped ~args_before ~wrap ~beginend =
2910+ let has_beginend = Option. is_some beginend in
2911+ let inner_ctx = Exp last_arg in
2912+ let inner_parens, outer_parens =
2913+ (* Don't disambiguate parentheses in some cases, also affect
2914+ indentation. *)
2915+ match lbody with
2916+ | Pfunction_cases _
2917+ when (not c.conf.fmt_opts.ocp_indent_compat.v) && not has_beginend ->
2918+ (parens, false )
2919+ | _ -> (false , parens)
2920+ in
2921+ let args =
2922+ let wrap_intro x =
2923+ fmt_if inner_parens (str " (" )
2924+ $ hvbox 0
2925+ ( intro_epi
2926+ $ wrap (fmt_args_grouped e0 args_before $ break 1 0 $ hvbox 0 x) )
2927+ $ break 1 0
2928+ in
2929+ let force_closing_paren =
2930+ if Location. is_single_line pexp_loc c.conf.fmt_opts.margin.v then Fit
2931+ else Break
2932+ in
2933+ let break_end =
2934+ let indent =
2935+ match (largs, lbody) with [] , Pfunction_cases _ -> 0 | _ -> - 2
2936+ in
2937+ break 1000 indent
2938+ in
2939+ let pro, inner_ctx, end_ =
2940+ match beginend with
2941+ | None -> (noop, inner_ctx, noop)
2942+ | Some (attrs , inner_ctx ) ->
2943+ let fmt_atrs = fmt_attributes c ~pre: Space attrs in
2944+ (str " begin" $ fmt_atrs $ str " " , inner_ctx, break_end $ str " end" )
2945+ in
2946+ (* bookmark *)
2947+ fmt_function ~pro ~last_arg: true ~force_closing_paren ~ctx: inner_ctx
2948+ ~ctx0: ctx ~wrap_intro ~label: lbl ~parens: (not has_beginend)
2949+ ~attrs: last_arg.pexp_attributes ~loc: last_arg.pexp_loc c ~epi: end_
2950+ (largs, ltyp, lbody)
2951+ in
2952+ hvbox_if has_attr 0
2953+ ( expr_epi
2954+ $ Params. parens_if outer_parens c.conf
2955+ (args $ fmt_atrs $ fmt_if inner_parens (str " )" )) )
2956+
29302957and fmt_lazy c ~ctx ?(pro = noop) ~fmt_atrs ~ext ~parens e =
29312958 let lazy_ = str " lazy" $ fmt_extension_suffix c ext in
29322959 let kw_outer, kw_inner =
0 commit comments