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