Skip to content

Commit 4a92203

Browse files
make the formating of map li begin fun the same as map li ( fun.
1 parent aa948be commit 4a92203

16 files changed

+699
-136
lines changed

lib/Fmt_ast.ml

Lines changed: 102 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1496,7 +1496,7 @@ and fmt_indexop_access c ctx ~fmt_atrs ~has_attr ~parens x =
14961496
responsible for breaking. *)
14971497
and 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 ~e1N1 ~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+
29302957
and 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 =

lib/Params.ml

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,33 @@ let ctx_is_rhs_of_infix ~ctx0 ~ctx =
3939
true
4040
| _ -> false
4141

42+
type ctx_is_apply_and_exp_is_arg_ret =
43+
{label: arg_label; exp: expression; is_last: bool; is_modulo_beginend: bool}
44+
4245
(** Return [None] if [ctx0] is not an application or [ctx] is not one of its
4346
argument. *)
4447
let ctx_is_apply_and_exp_is_arg ~ctx ctx0 =
4548
match (ctx, ctx0) with
4649
| Exp exp, Exp {pexp_desc= Pexp_apply (_, args); _} ->
4750
let last_lbl, last_arg = List.last_exn args in
48-
if phys_equal last_arg exp then Some (last_lbl, exp, true)
51+
let last_arg, is_modulo_beginend =
52+
match last_arg.pexp_desc with
53+
| Pexp_beginend last_arg -> (last_arg, true)
54+
| _ -> (last_arg, false)
55+
in
56+
if phys_equal last_arg exp then
57+
Some {label= last_lbl; exp; is_last= true; is_modulo_beginend}
4958
else
5059
List.find_map
51-
~f:(fun (lbl, x) ->
52-
if phys_equal x exp then Some (lbl, exp, false) else None )
60+
~f:(fun (label, arg) ->
61+
let arg, is_modulo_beginend =
62+
match arg.pexp_desc with
63+
| Pexp_beginend arg -> (arg, true)
64+
| _ -> (arg, false)
65+
in
66+
if phys_equal arg exp then
67+
Some {label; exp; is_last= false; is_modulo_beginend}
68+
else None )
5369
args
5470
| _ -> None
5571

@@ -70,6 +86,11 @@ let ctx_is_apply_and_exp_is_last_arg_and_other_args_are_simple c ~ctx ctx0 =
7086
List.for_all args_before ~f:(fun (_, eI) ->
7187
is_simple c (fun _ -> 0) (sub_exp ~ctx:ctx0 eI) )
7288
in
89+
let last_arg =
90+
match last_arg.pexp_desc with
91+
| Pexp_beginend last_arg -> last_arg
92+
| _ -> last_arg
93+
in
7394
Poly.equal last_arg exp && args_are_simple
7495
| _ -> false
7596

@@ -159,7 +180,7 @@ module Exp = struct
159180
let break_fun_kw c ~ctx ~ctx0 ~last_arg =
160181
let is_labelled_arg =
161182
match ctx_is_apply_and_exp_is_arg ~ctx ctx0 with
162-
| Some ((Labelled _ | Optional _), _, _) -> true
183+
| Some {label= Labelled _ | Optional _; _} -> true
163184
| _ -> false
164185
in
165186
if Conf.(c.fmt_opts.ocp_indent_compat.v) then
@@ -183,7 +204,7 @@ module Exp = struct
183204
if ocp c then
184205
let is_labelled_arg =
185206
match ctx_is_apply_and_exp_is_arg ~ctx ctx0 with
186-
| Some ((Labelled _ | Optional _), _, _) -> true
207+
| Some {label= Labelled _ | Optional _; _} -> true
187208
| _ -> false
188209
in
189210
if is_labelled_arg then (Fn.id, true)
@@ -195,12 +216,14 @@ module Exp = struct
195216
if is_let_func then if kw_in_box then hovbox ~name 4 else Fn.id
196217
else
197218
match ctx_is_apply_and_exp_is_arg ~ctx ctx0 with
198-
| Some (_, _, true) ->
199-
(* Is last arg. *) hvbox ~name (if parens then 0 else 2)
200-
| Some (Nolabel, _, false) ->
219+
| Some {is_last= true; is_modulo_beginend; _} ->
220+
(* Is last arg. *)
221+
hvbox ~name (if parens || is_modulo_beginend then 0 else 2)
222+
| Some {label= Nolabel; is_last= false; _} ->
201223
(* TODO: Inconsistent formatting of fun args. *)
202224
hovbox ~name 0
203-
| Some ((Labelled _ | Optional _), _, false) -> hvbox ~name 0
225+
| Some {label= Labelled _ | Optional _; is_last= false; _} ->
226+
hvbox ~name 0
204227
| None -> Fn.id
205228
in
206229
(box, not c.fmt_opts.wrap_fun_args.v)
@@ -225,15 +248,20 @@ module Exp = struct
225248
Source.begins_line ~ignore_spaces:true source loc
226249
in
227250
match ctx_is_apply_and_exp_is_arg ~ctx ctx0 with
228-
| Some (Nolabel, fun_exp, is_last_arg) ->
229-
if begins_line fun_exp.pexp_loc then if is_last_arg then 5 else 3
251+
| Some {label= Nolabel; exp= fun_exp; is_last; is_modulo_beginend= _}
252+
->
253+
if begins_line fun_exp.pexp_loc then if is_last then 5 else 3
230254
else 2
231-
| Some ((Labelled x | Optional x), fun_exp, is_last_arg) ->
255+
| Some
256+
{ label= Labelled x | Optional x
257+
; exp= fun_exp
258+
; is_last
259+
; is_modulo_beginend= _ } ->
232260
if begins_line fun_exp.pexp_loc then
233261
(* The [fun] had to break after the label, nested boxes must be
234262
indented less. The last argument is special as the box
235263
structure is different. *)
236-
if is_last_arg then 4 else 2
264+
if is_last then 4 else 2
237265
else if begins_line x.loc then 4
238266
else 2
239267
| None -> if ctx_is_apply_and_exp_is_func ~ctx ctx0 then 3 else 2
@@ -275,7 +303,7 @@ module Exp = struct
275303

276304
let single_line_function ~ctx ~ctx0 ~args =
277305
match ctx_is_apply_and_exp_is_arg ~ctx ctx0 with
278-
| Some (_, _, true) -> List.is_empty args
306+
| Some {is_last= true; _} -> List.is_empty args
279307
| _ -> false
280308

281309
let indent_function (c : Conf.t) ~ctx ~ctx0 ~parens =
@@ -1001,7 +1029,7 @@ module Indent = struct
10011029
| _ when ctx_is_infix ctx0 -> 0
10021030
| _ when ocp c -> (
10031031
match ctx_is_apply_and_exp_is_arg ~ctx ctx0 with
1004-
| Some (_, _, true) -> (* Last argument *) 2
1032+
| Some {is_last= true; _} -> (* Last argument *) 2
10051033
| _ -> if parens then 3 else 2 )
10061034
| _ -> 2
10071035

test/passing/refs.default/attributes.ml.ref

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -393,10 +393,8 @@ include [@foo] M [@boo]
393393
let () =
394394
let () =
395395
S.ntyp Cbor_type.Reserved
396-
@@ S.tok
397-
begin [@warning "-4"] fun ev ->
398-
match ev with Cbor_event.Reserved int -> Some int | _ -> None
399-
end
396+
@@ S.tok (fun [@warning "-4"] ev ->
397+
match ev with Cbor_event.Reserved int -> Some int | _ -> None)
400398
in
401399
()
402400

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Warning: exp_grouping-parens.ml:577 exceeds the margin

0 commit comments

Comments
 (0)