Skip to content

Commit a3602e6

Browse files
committed
fmt
1 parent 0b07604 commit a3602e6

File tree

4 files changed

+111
-93
lines changed

4 files changed

+111
-93
lines changed

lib/Ast.ml

Lines changed: 64 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1208,13 +1208,13 @@ end = struct
12081208
in
12091209
let check_cases = List.exists ~f:(fun c -> c.pc_lhs == pat) in
12101210
let check_binding {pvb_pat; pvb_body; _} =
1211-
check_subpat pvb_pat || match pvb_body with
1212-
| Pfunction_body _ -> false
1213-
| Pfunction_cases (cases, _, _) ->
1214-
check_cases cases in
1215-
let check_bindings l =
1216-
List.exists l ~f:check_binding
1211+
check_subpat pvb_pat
1212+
||
1213+
match pvb_body with
1214+
| Pfunction_body _ -> false
1215+
| Pfunction_cases (cases, _, _) -> check_cases cases
12171216
in
1217+
let check_bindings l = List.exists l ~f:check_binding in
12181218
let check_param_val (_, _, p) = p == pat in
12191219
let check_expr_function_param param =
12201220
match param.pparam_desc with
@@ -1389,7 +1389,8 @@ end = struct
13891389
assert (check_cases cases)
13901390
| Pexp_function (params, _, body) ->
13911391
assert (
1392-
List.exists ~f:check_expr_function_param params || check_fun_body body )
1392+
List.exists ~f:check_expr_function_param params
1393+
|| check_fun_body body )
13931394
| Pexp_indexop_access {pia_lhs; pia_kind= Builtin idx; pia_rhs; _} ->
13941395
assert (
13951396
pia_lhs == exp || idx == exp
@@ -1927,10 +1928,11 @@ end = struct
19271928
| Ppat_or _ | Ppat_alias _ ) ) ->
19281929
true
19291930
| Bo {pbop_typ= Some _; _}, (Ppat_any | Ppat_tuple _) -> true
1930-
| Exp {pexp_desc= (Pexp_function (_, _, Pfunction_body _)); _}, Ppat_or _
1931+
| Exp {pexp_desc= Pexp_function (_, _, Pfunction_body _); _}, Ppat_or _
19311932
|( Exp {pexp_desc= Pexp_function (_, _, Pfunction_body _); _}
19321933
, ( Ppat_construct _ | Ppat_cons _ | Ppat_lazy _ | Ppat_tuple _
1933-
| Ppat_variant _ ) ) -> true
1934+
| Ppat_variant _ ) ) ->
1935+
true
19341936
| _, Ppat_constraint _
19351937
|_, Ppat_unpack _
19361938
|( Pat
@@ -1944,8 +1946,7 @@ end = struct
19441946
( Ppat_construct _ | Ppat_exception _ | Ppat_or _
19451947
| Ppat_lazy _ | Ppat_tuple _ | Ppat_variant _ | Ppat_list _ )
19461948
; _ }
1947-
| Exp {pexp_desc= Pexp_function (_, _, Pfunction_body _); _}
1948-
)
1949+
| Exp {pexp_desc= Pexp_function (_, _, Pfunction_body _); _} )
19491950
, Ppat_alias _ )
19501951
|( Pat {ppat_desc= Ppat_lazy _; _}
19511952
, ( Ppat_construct _ | Ppat_cons _
@@ -1966,8 +1967,7 @@ end = struct
19661967
|Cl {pcl_desc= Pcl_fun _; _}, Ppat_construct _
19671968
|Cl {pcl_desc= Pcl_fun _; _}, Ppat_alias _
19681969
|Cl {pcl_desc= Pcl_fun _; _}, Ppat_lazy _
1969-
|(Exp {pexp_desc= Pexp_letop _; _} | Bo _), Ppat_exception _
1970-
->
1970+
|(Exp {pexp_desc= Pexp_letop _; _} | Bo _), Ppat_exception _ ->
19711971
true
19721972
| (Str _ | Exp _ | Lb _), Ppat_lazy _ -> true
19731973
| ( (Fpe _ | Fpc _)
@@ -1995,14 +1995,14 @@ end = struct
19951995
true
19961996
| _ -> false
19971997

1998-
(* Whether an expression in a let binding shouldn't be parenthesed, bypassing
1999-
the other Ast rules. *)
1998+
(* Whether an expression in a let binding shouldn't be parenthesed,
1999+
bypassing the other Ast rules. *)
20002000
let dont_parenze_exp_in_bindings bindings exp =
20012001
match exp.pexp_desc with
20022002
| Pexp_function ([], None, (Pfunction_cases _ as fun_body)) ->
20032003
(* [fun_body] is the body of the let binding and shouldn't be
2004-
parenthesed. [exp] is a synthetic expression constructed in
2005-
the formatting code. *)
2004+
parenthesed. [exp] is a synthetic expression constructed in the
2005+
formatting code. *)
20062006
List.exists bindings ~f:(fun {pvb_body; _} -> pvb_body == fun_body)
20072007
| _ -> false
20082008

@@ -2170,16 +2170,19 @@ end = struct
21702170
(* Whether to parenze an expr on the RHS of a let binding.
21712171
[dont_parenze_exp_in_bindings] must have been checked before. *)
21722172
and parenze_exp_in_bindings bindings exp =
2173-
List.exists bindings ~f:(fun {pvb_body; pvb_args; _} ->
2174-
match pvb_body with
2175-
| Pfunction_body ({pexp_desc = Pexp_function ([],None,Pfunction_cases _);_} as let_body) when let_body == exp ->
2176-
(* Function with cases and no 'fun' keyword is in the body of
2177-
a binding, parentheses are needed if the binding also
2178-
defines arguments. *)
2179-
not (List.is_empty pvb_args)
2180-
| Pfunction_cases (cases,_,_) -> parenze_exp_in_match_case cases exp
2181-
| _ -> false
2182-
)
2173+
List.exists bindings ~f:(fun {pvb_body; pvb_args; _} ->
2174+
match pvb_body with
2175+
| Pfunction_body
2176+
( {pexp_desc= Pexp_function ([], None, Pfunction_cases _); _} as
2177+
let_body )
2178+
when let_body == exp ->
2179+
(* Function with cases and no 'fun' keyword is in the body of a
2180+
binding, parentheses are needed if the binding also defines
2181+
arguments. *)
2182+
not (List.is_empty pvb_args)
2183+
| Pfunction_cases (cases, _, _) ->
2184+
parenze_exp_in_match_case cases exp
2185+
| _ -> false )
21832186

21842187
(** [parenze_exp {ctx; ast}] holds when expression [ast] should be
21852188
parenthesized in context [ctx]. *)
@@ -2235,18 +2238,19 @@ end = struct
22352238
||
22362239
match (ctx, exp) with
22372240
| Str {pstr_desc= Pstr_eval _; _}, _ -> false
2238-
| ( Lb pvb, _) when dont_parenze_exp_in_bindings [pvb] exp -> false
2239-
| ( Exp {pexp_desc=Pexp_let ({ pvbs_bindings; _ }, _, _);_}, _)
2240-
| ( Cl {pcl_desc= Pcl_let ({ pvbs_bindings; _ }, _,_);_}, _)
2241+
| Lb pvb, _ when dont_parenze_exp_in_bindings [pvb] exp -> false
2242+
| Exp {pexp_desc= Pexp_let ({pvbs_bindings; _}, _, _); _}, _
2243+
|Cl {pcl_desc= Pcl_let ({pvbs_bindings; _}, _, _); _}, _
22412244
when dont_parenze_exp_in_bindings pvbs_bindings exp ->
22422245
false
2243-
| ( Lb pvb, _) when parenze_exp_in_bindings [pvb] exp ->
2244-
true
2245-
| ( Exp {pexp_desc=Pexp_let ({ pvbs_bindings; _ }, _, _);_}, _)
2246-
| ( Cl {pcl_desc= Pcl_let ({ pvbs_bindings; _ }, _,_);_}, _)
2246+
| Lb pvb, _ when parenze_exp_in_bindings [pvb] exp -> true
2247+
| Exp {pexp_desc= Pexp_let ({pvbs_bindings; _}, _, _); _}, _
2248+
|Cl {pcl_desc= Pcl_let ({pvbs_bindings; _}, _, _); _}, _
22472249
when parenze_exp_in_bindings pvbs_bindings exp ->
22482250
true
2249-
| _, {pexp_desc= Pexp_infix _; pexp_attributes= _ :: _; _} when ctx_sensitive_to_trailing_attributes ctx -> true
2251+
| _, {pexp_desc= Pexp_infix _; pexp_attributes= _ :: _; _}
2252+
when ctx_sensitive_to_trailing_attributes ctx ->
2253+
true
22502254
| ( Str
22512255
{ pstr_desc=
22522256
Pstr_value
@@ -2336,24 +2340,27 @@ end = struct
23362340
, {pexp_desc= Pexp_function ([], None, Pfunction_cases _); _} )
23372341
when e == exp ->
23382342
true
2339-
| Exp {pexp_desc=
2340-
Pexp_extension
2341-
( _
2342-
, PStr
2343-
[ { pstr_desc=
2344-
Pstr_eval
2345-
( { pexp_desc=
2346-
( Pexp_function
2347-
(_, _, Pfunction_cases (cases, _, _))
2348-
| Pexp_match (_, cases)
2349-
| Pexp_try (_, cases) )
2350-
; _ }
2351-
, _ )
2352-
; _ } ] )
2353-
|Pexp_function (_, _, Pfunction_cases (cases, _, _))
2354-
|Pexp_match (_, cases)
2355-
|Pexp_try (_, cases)
2356-
; _}, _ -> parenze_exp_in_match_case cases exp
2343+
| ( Exp
2344+
{ pexp_desc=
2345+
( Pexp_extension
2346+
( _
2347+
, PStr
2348+
[ { pstr_desc=
2349+
Pstr_eval
2350+
( { pexp_desc=
2351+
( Pexp_function
2352+
(_, _, Pfunction_cases (cases, _, _))
2353+
| Pexp_match (_, cases)
2354+
| Pexp_try (_, cases) )
2355+
; _ }
2356+
, _ )
2357+
; _ } ] )
2358+
| Pexp_function (_, _, Pfunction_cases (cases, _, _))
2359+
| Pexp_match (_, cases)
2360+
| Pexp_try (_, cases) )
2361+
; _ }
2362+
, _ ) ->
2363+
parenze_exp_in_match_case cases exp
23572364
| Exp {pexp_desc; _}, _ -> (
23582365
match pexp_desc with
23592366
| Pexp_ifthenelse (eN, _)
@@ -2417,7 +2424,10 @@ end = struct
24172424
| _ -> Exp.has_trailing_attributes exp || parenze () ) )
24182425
| _, {pexp_desc= Pexp_list _; _} -> false
24192426
| _, {pexp_desc= Pexp_array _; _} -> false
2420-
| _, exp when ctx_sensitive_to_trailing_attributes ctx && Exp.has_trailing_attributes exp -> true
2427+
| _, exp
2428+
when ctx_sensitive_to_trailing_attributes ctx
2429+
&& Exp.has_trailing_attributes exp ->
2430+
true
24212431
| _ -> false
24222432

24232433
(** [parenze_cl {ctx; ast}] holds when class expr [ast] should be

lib/Fmt_ast.ml

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1256,8 +1256,7 @@ and fmt_pattern ?ext c ?pro ?parens ?(box = false)
12561256
$ close_box )
12571257
$ fmt_or nested
12581258
(fits_breaks (if parens then ")" else "") ~hint:(1, 2) ")")
1259-
(fits_breaks (if parens then ")" else "") "")
1260-
)
1259+
(fits_breaks (if parens then ")" else "") "") )
12611260
| Ppat_constraint (pat, typ) ->
12621261
hvbox 2
12631262
(Params.parens_if parens c.conf
@@ -1539,8 +1538,8 @@ and fmt_function ?(last_arg = false) ?force_closing_paren ~ctx ~ctx0
15391538
if Params.Exp.function_attrs_sp c.conf ~ctx0 ~ctx then Some Blank
15401539
else None
15411540
in
1542-
Cmts.fmt_before c function_loc $
1543-
str "function"
1541+
Cmts.fmt_before c function_loc
1542+
$ str "function"
15441543
$ fmt_extension_suffix c ext
15451544
$ fmt_attributes ?pre c spilled_attrs
15461545
$ fmt_attributes ?pre c cs_attrs
@@ -2074,7 +2073,8 @@ and fmt_expression c ?(box = true) ?(pro = noop) ?eol ?parens
20742073
; _ }
20752074
when not (Std_longident.is_infix id) ->
20762075
has_attr && parens
2077-
| Lb {pvb_body=Pfunction_body body;_} when phys_equal body exp -> has_attr && parens
2076+
| Lb {pvb_body= Pfunction_body body; _} when phys_equal body exp ->
2077+
has_attr && parens
20782078
| _ -> has_attr && not parens
20792079
in
20802080
let infix_op_args =
@@ -2389,14 +2389,14 @@ and fmt_expression c ?(box = true) ?(pro = noop) ?eol ?parens
23892389
in
23902390
let fmt_expr = fmt_expression c (sub_exp ~ctx body) in
23912391
pro
2392-
$ fmt_let_bindings c ~ctx0:ctx ~parens ~fmt_atrs ~fmt_expr ~has_attr ~loc_in
2393-
lbs.pvbs_rec bindings body
2392+
$ fmt_let_bindings c ~ctx0:ctx ~parens ~fmt_atrs ~fmt_expr ~has_attr
2393+
~loc_in lbs.pvbs_rec bindings body
23942394
| Pexp_letop {let_; ands; body; loc_in} ->
23952395
let bd = Sugar.Let_binding.of_binding_ops (let_ :: ands) in
23962396
let fmt_expr = fmt_expression c (sub_exp ~ctx body) in
23972397
pro
2398-
$ fmt_let_bindings c ~ctx0:ctx ~parens ~fmt_atrs ~fmt_expr ~has_attr ~loc_in
2399-
Nonrecursive bd body
2398+
$ fmt_let_bindings c ~ctx0:ctx ~parens ~fmt_atrs ~fmt_expr ~has_attr
2399+
~loc_in Nonrecursive bd body
24002400
| Pexp_letexception (ext_cstr, exp) ->
24012401
let pre =
24022402
str "let exception" $ fmt_extension_suffix c ext $ space_break
@@ -2864,8 +2864,8 @@ and fmt_expression c ?(box = true) ?(pro = noop) ?eol ?parens
28642864
(sub_exp ~ctx e) )
28652865
$ fmt_atrs
28662866

2867-
and fmt_let_bindings c ~ctx0 ~parens ~has_attr ~fmt_atrs ~fmt_expr ~loc_in rec_flag
2868-
bindings body =
2867+
and fmt_let_bindings c ~ctx0 ~parens ~has_attr ~fmt_atrs ~fmt_expr ~loc_in
2868+
rec_flag bindings body =
28692869
let indent_after_in =
28702870
match body.pexp_desc with
28712871
| Pexp_let _ | Pexp_letmodule _
@@ -2882,8 +2882,8 @@ and fmt_let_bindings c ~ctx0 ~parens ~has_attr ~fmt_atrs ~fmt_expr ~loc_in rec_f
28822882
0
28832883
| _ -> c.conf.fmt_opts.indent_after_in.v
28842884
in
2885-
fmt_let c ~ctx0 ~rec_flag ~bindings ~parens ~has_attr ~fmt_atrs ~fmt_expr ~loc_in
2886-
~body_loc:body.pexp_loc ~indent_after_in
2885+
fmt_let c ~ctx0 ~rec_flag ~bindings ~parens ~has_attr ~fmt_atrs ~fmt_expr
2886+
~loc_in ~body_loc:body.pexp_loc ~indent_after_in
28872887

28882888
and fmt_class_structure c ~ctx ?ext self_ fields =
28892889
let update_config c i =
@@ -3055,8 +3055,8 @@ and fmt_class_expr c ({ast= exp; ctx= ctx0} as xexp) =
30553055
in
30563056
let fmt_expr = fmt_class_expr c (sub_cl ~ctx body) in
30573057
let has_attr = not (List.is_empty pcl_attributes) in
3058-
fmt_let c ~ctx0:ctx ~rec_flag:lbs.pvbs_rec ~bindings ~parens ~loc_in ~has_attr
3059-
~fmt_atrs ~fmt_expr ~body_loc:body.pcl_loc ~indent_after_in
3058+
fmt_let c ~ctx0:ctx ~rec_flag:lbs.pvbs_rec ~bindings ~parens ~loc_in
3059+
~has_attr ~fmt_atrs ~fmt_expr ~body_loc:body.pcl_loc ~indent_after_in
30603060
| Pcl_constraint (e, t) ->
30613061
hvbox 2
30623062
(wrap_fits_breaks ~space:false c.conf "(" ")"
@@ -4525,7 +4525,7 @@ and fmt_value_constraint c vc_opt =
45254525
| None -> (noop, noop)
45264526

45274527
and fmt_value_binding c ~ctx0 ~rec_flag ?in_ ?epi
4528-
{lb_op; lb_pat; lb_args; lb_typ; lb_body; lb_attrs; lb_loc; lb_pun} =
4528+
{lb_op; lb_pat; lb_args; lb_typ; lb_body; lb_attrs; lb_loc; lb_pun} =
45294529
let in_, loc_in =
45304530
match in_ with
45314531
| None -> (None, None)
@@ -4552,10 +4552,9 @@ and fmt_value_binding c ~ctx0 ~rec_flag ?in_ ?epi
45524552
let fmt_newtypes, fmt_cstr = fmt_value_constraint c lb_typ in
45534553
let indent, intro_as_pro =
45544554
match lb_body.ast with
4555-
| Pfunction_cases _ ->
4556-
(c.conf.fmt_opts.function_indent.v, true)
4557-
| Pfunction_body { pexp_desc = Pexp_function (_, _, _); _ } when c.conf.fmt_opts.let_binding_deindent_fun.v
4558-
->
4555+
| Pfunction_cases _ -> (c.conf.fmt_opts.function_indent.v, true)
4556+
| Pfunction_body {pexp_desc= Pexp_function (_, _, _); _}
4557+
when c.conf.fmt_opts.let_binding_deindent_fun.v ->
45594558
(max (c.conf.fmt_opts.let_binding_indent.v - 1) 0, false)
45604559
| _ -> (c.conf.fmt_opts.let_binding_indent.v, false)
45614560
in
@@ -4608,15 +4607,15 @@ and fmt_value_binding c ~ctx0 ~rec_flag ?in_ ?epi
46084607
let wrap_intro intro =
46094608
hovbox 2 (fmt_opt pro $ intro) $ space_break
46104609
in
4611-
fmt_function ~ctx ~ctx0 ~wrap_intro ?box ~label:Nolabel ~attrs:[] ~loc:lb_loc c ([], None, body)
4612-
| Pfunction_body body -> fmt_expression c ?pro ?box (sub_exp ~ctx body)
4610+
fmt_function ~ctx ~ctx0 ~wrap_intro ?box ~label:Nolabel ~attrs:[]
4611+
~loc:lb_loc c ([], None, body)
4612+
| Pfunction_body body ->
4613+
fmt_expression c ?pro ?box (sub_exp ~ctx body)
46134614
in
46144615
let pro =
46154616
if c.conf.fmt_opts.ocp_indent_compat.v then
46164617
let box =
4617-
match lb_body.ast with
4618-
| Pfunction_cases _ -> false
4619-
| _ -> true
4618+
match lb_body.ast with Pfunction_cases _ -> false | _ -> true
46204619
in
46214620
hvbox_if box 2 (decl $ fits_breaks " =" ~hint:(1000, 0) "=")
46224621
$ space_break

lib/Params.ml

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -358,24 +358,28 @@ end
358358

359359
(* Whether [pat] appears in [ctx] as a match/function/try case. *)
360360
let get_or_pattern_is_nested ~ctx pat =
361-
let check_cases =List.exists ~f:(fun c -> phys_equal c.pc_lhs pat) in
361+
let check_cases = List.exists ~f:(fun c -> phys_equal c.pc_lhs pat) in
362362
match ctx with
363363
| _ when not (List.is_empty pat.ppat_attributes) -> true
364-
| Ast.Exp {pexp_desc= Pexp_function (_,_,Pfunction_cases (cases,_,_)) | Pexp_match (_,cases) | Pexp_try (_,cases); _}
365-
| Lb {pvb_body=Pfunction_cases (cases,_,_);_} ->
364+
| Ast.Exp
365+
{ pexp_desc=
366+
( Pexp_function (_, _, Pfunction_cases (cases, _, _))
367+
| Pexp_match (_, cases)
368+
| Pexp_try (_, cases) )
369+
; _ }
370+
|Lb {pvb_body= Pfunction_cases (cases, _, _); _} ->
366371
not (check_cases cases)
367-
| Exp {pexp_desc= Pexp_let (bindings, _,_); _}
368-
| Cl {pcl_desc= Pcl_let (bindings, _,_); _}
369-
| Str {pstr_desc= Pstr_value (bindings); _}
370-
->
371-
not (List.exists bindings.pvbs_bindings ~f:(function
372-
|{pvb_body=Pfunction_cases (cases,_,_);_} -> check_cases cases
373-
| _ -> false
374-
))
372+
| Exp {pexp_desc= Pexp_let (bindings, _, _); _}
373+
|Cl {pcl_desc= Pcl_let (bindings, _, _); _}
374+
|Str {pstr_desc= Pstr_value bindings; _} ->
375+
not
376+
(List.exists bindings.pvbs_bindings ~f:(function
377+
| {pvb_body= Pfunction_cases (cases, _, _); _} -> check_cases cases
378+
| _ -> false ))
375379
| _ -> true
376380

377381
let get_or_pattern_sep ?(cmts_before = false) ?(space = false) (c : Conf.t)
378-
~nested =
382+
~nested =
379383
let nspaces = if cmts_before then 1000 else 1 in
380384
match c.fmt_opts.break_cases.v with
381385
| _ when nested -> break nspaces 0 $ str "| "
@@ -411,7 +415,11 @@ let get_cases (c : Conf.t) ~ctx ~first ~last ~cmts_before
411415
let indent =
412416
match (c.fmt_opts.cases_matching_exp_indent.v, (ctx, ast.pexp_desc)) with
413417
| ( `Compact
414-
, (( Exp {pexp_desc= Pexp_function _ | Pexp_match _ | Pexp_try _ | Pexp_let _; _} | Lb {pvb_body = Pfunction_cases _;_})
418+
, ( ( Exp
419+
{ pexp_desc=
420+
Pexp_function _ | Pexp_match _ | Pexp_try _ | Pexp_let _
421+
; _ }
422+
| Lb {pvb_body= Pfunction_cases _; _} )
415423
, (Pexp_match _ | Pexp_try _ | Pexp_beginend _) ) ) ->
416424
2
417425
| _, _ -> c.fmt_opts.cases_exp_indent.v

lib/Params.mli

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,9 @@ module Pcty : sig
103103
val break_let_open : Conf.t -> rhs:class_type -> Fmt.t
104104
end
105105

106-
(** Whether an or-pattern should be disambiguated. *)
107106
val get_or_pattern_is_nested : ctx:Ast.t -> pattern -> bool
107+
(** Whether an or-pattern should be disambiguated. *)
108+
108109
val get_or_pattern_sep :
109110
?cmts_before:bool -> ?space:bool -> Conf.t -> nested:bool -> Fmt.t
110111

0 commit comments

Comments
 (0)