Skip to content

Commit 0ea1e86

Browse files
EmileTrotignondavesnx
authored andcommitted
promote ocp docking for infix apply. (ocaml-ppx#2694)
* promote ocp docking ofr infix apply. * fmt * changelog
1 parent 47017ec commit 0ea1e86

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+467
-425
lines changed

CHANGES.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,22 @@ profile. This started with version 0.26.0.
104104
let a = 3
105105
```
106106

107+
- \* Infix apply docking behaviour from --ocp-indent-compat is promoted to
108+
everyone. The most common effect is that `|> map (fun` is now indented from
109+
`|>` and not from `map`:
110+
```ocaml
111+
(* before *)
112+
v
113+
|>>>>>> map (fun x ->
114+
x )
115+
(* after *)
116+
v
117+
|>>>>>> map (fun x ->
118+
x )
119+
```
120+
`@@ match` can now also be on one line.
121+
(#2694, @EmileTrotignon)
122+
107123
## 0.27.0
108124

109125
### Highlight

bench/bench.ml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,14 @@ let json_of_ols_results ?name (results : Bechamel.Analyze.OLS.t results) :
9797
let results =
9898
metrics_by_test |> Hashtbl.to_seq
9999
|> Seq.map (fun (test_name, metrics) ->
100-
let metrics =
101-
metrics |> Hashtbl.to_seq
102-
|> Seq.map (fun (metric_name, ols) ->
103-
(metric_name, json_of_ols ols) )
104-
|> List.of_seq
105-
|> fun bindings -> `Assoc bindings
106-
in
107-
`Assoc [("name", `String test_name); ("metrics", metrics)] )
100+
let metrics =
101+
metrics |> Hashtbl.to_seq
102+
|> Seq.map (fun (metric_name, ols) ->
103+
(metric_name, json_of_ols ols) )
104+
|> List.of_seq
105+
|> fun bindings -> `Assoc bindings
106+
in
107+
`Assoc [("name", `String test_name); ("metrics", metrics)] )
108108
|> List.of_seq
109109
|> fun items -> `List items
110110
in

lib/Ast.ml

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -995,14 +995,14 @@ end = struct
995995
assert (
996996
List.exists ptype_params ~f:fst_f
997997
|| List.exists ptype_cstrs ~f:(fun (t1, t2, _) ->
998-
typ == t1 || typ == t2 )
998+
typ == t1 || typ == t2 )
999999
|| ( match ptype_kind with
1000-
| Ptype_variant cd1N ->
1001-
List.exists cd1N ~f:(fun {pcd_args; pcd_res; _} ->
1002-
check_cstr pcd_args || Option.exists pcd_res ~f )
1003-
| Ptype_record ld1N ->
1004-
List.exists ld1N ~f:(fun {pld_type; _} -> typ == pld_type)
1005-
| _ -> false )
1000+
| Ptype_variant cd1N ->
1001+
List.exists cd1N ~f:(fun {pcd_args; pcd_res; _} ->
1002+
check_cstr pcd_args || Option.exists pcd_res ~f )
1003+
| Ptype_record ld1N ->
1004+
List.exists ld1N ~f:(fun {pld_type; _} -> typ == pld_type)
1005+
| _ -> false )
10061006
|| Option.exists ptype_manifest ~f )
10071007
| Cty {pcty_desc; _} ->
10081008
assert (
@@ -1535,13 +1535,13 @@ end = struct
15351535
| Pexp_record (e1N, e0) ->
15361536
Option.for_all e0 ~f:Exp.is_trivial
15371537
&& List.for_all e1N ~f:(fun (_, c, eo) ->
1538-
Option.is_none c && Option.for_all eo ~f:Exp.is_trivial )
1538+
Option.is_none c && Option.for_all eo ~f:Exp.is_trivial )
15391539
&& fit_margin c (width xexp)
15401540
| Pexp_indexop_access {pia_lhs; pia_kind; pia_rhs= None; _} ->
15411541
Exp.is_trivial pia_lhs
15421542
&& ( match pia_kind with
1543-
| Builtin idx -> Exp.is_trivial idx
1544-
| Dotop (_, _, idx) -> List.for_all idx ~f:Exp.is_trivial )
1543+
| Builtin idx -> Exp.is_trivial idx
1544+
| Dotop (_, _, idx) -> List.for_all idx ~f:Exp.is_trivial )
15451545
&& fit_margin c (width xexp)
15461546
| Pexp_prefix (_, e) -> Exp.is_trivial e && fit_margin c (width xexp)
15471547
| Pexp_infix ({txt= ":="; _}, _, _) -> false
@@ -2219,7 +2219,7 @@ end = struct
22192219
| Pexp_infix (_, _, e2)
22202220
when e2 == exp
22212221
&& Option.value_map ~default:false (prec_ast ctx) ~f:(fun p ->
2222-
Prec.compare p Apply < 0 ) ->
2222+
Prec.compare p Apply < 0 ) ->
22232223
true
22242224
| Pexp_tuple e1N -> List.last_exn e1N == xexp.ast
22252225
| _ -> false

lib/Cmts.ml

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -595,19 +595,19 @@ let fmt_cmts_aux t (conf : Conf.t) cmts ~fmt_code pos =
595595
vbox 0 ~name:"cmts"
596596
(list_pn groups (fun ~prev:_ group ~next ->
597597
( match group with
598-
| [] -> impossible "previous match"
599-
| [cmt] ->
600-
let break =
601-
fmt_if
602-
( conf.fmt_opts.ocp_indent_compat.v
603-
&& Poly.(pos = Cmt.After)
604-
&& String.contains (Cmt.txt cmt) '\n' )
605-
(break_unless_newline 1000 0)
606-
in
607-
break $ fmt_cmt conf cmt ~fmt_code
608-
| group ->
609-
list group force_break (fun cmt ->
610-
wrap (str "(*") (str "*)") (str (Cmt.txt cmt)) ) )
598+
| [] -> impossible "previous match"
599+
| [cmt] ->
600+
let break =
601+
fmt_if
602+
( conf.fmt_opts.ocp_indent_compat.v
603+
&& Poly.(pos = Cmt.After)
604+
&& String.contains (Cmt.txt cmt) '\n' )
605+
(break_unless_newline 1000 0)
606+
in
607+
break $ fmt_cmt conf cmt ~fmt_code
608+
| group ->
609+
list group force_break (fun cmt ->
610+
wrap (str "(*") (str "*)") (str (Cmt.txt cmt)) ) )
611611
$
612612
match next with
613613
| Some (next :: _) ->

lib/Fmt_ast.ml

Lines changed: 66 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -222,10 +222,10 @@ let fmt_item_list c ctx0 update_config ast fmt_item items =
222222
let loc = Ast.location ctx in
223223
maybe_disabled c loc [] (fun c -> fmt_item c ctx ~prev ~next itm)
224224
$ opt next (fun (i_n, c_n) ->
225-
fmt_or
226-
(break_between c (ctx, c.conf) (ast i_n, c_n.conf))
227-
(str "\n" $ force_break)
228-
(fmt_or break_struct force_break space_break) )
225+
fmt_or
226+
(break_between c (ctx, c.conf) (ast i_n, c_n.conf))
227+
(str "\n" $ force_break)
228+
(fmt_or break_struct force_break space_break) )
229229

230230
let fmt_recmodule c ctx items fmt_item ast sub =
231231
let update_config c i = update_config c (Ast.attributes (ast i)) in
@@ -462,7 +462,7 @@ let fmt_docstring_around_item' ?(is_val = false) ?(force_before = false)
462462
let floating_doc, doc =
463463
doc
464464
|> List.map ~f:(fun (({txt; loc}, _) as doc) ->
465-
(Docstring.parse ~loc txt, doc) )
465+
(Docstring.parse ~loc txt, doc) )
466466
|> List.partition_tf ~f:(fun (_, (_, floating)) -> floating)
467467
in
468468
let placement =
@@ -849,8 +849,8 @@ and fmt_core_type c ?(box = true) ?pro ?(pro_space = true) ?constraint_ctx
849849
update_config_maybe_disabled c ptyp_loc ptyp_attributes
850850
@@ fun c ->
851851
( match pro with
852-
| Some pro -> fmt_constraint_sep ~pro_space c pro
853-
| None -> noop )
852+
| Some pro -> fmt_constraint_sep ~pro_space c pro
853+
| None -> noop )
854854
$
855855
let doc, atrs = doc_atrs ptyp_attributes in
856856
Cmts.fmt c ptyp_loc
@@ -1116,8 +1116,8 @@ and fmt_pattern ?ext c ?pro ?parens ?(box = false)
11161116
let parens = match parens with Some b -> b | None -> parenze_pat xpat in
11171117
(match ctx0 with Pat {ppat_desc= Ppat_tuple _; _} -> hvbox 0 | _ -> Fn.id)
11181118
@@ ( match ppat_desc with
1119-
| Ppat_or _ -> fun k -> Cmts.fmt c ppat_loc @@ k
1120-
| _ -> fun k -> Cmts.fmt c ppat_loc @@ (fmt_opt pro $ k) )
1119+
| Ppat_or _ -> fun k -> Cmts.fmt c ppat_loc @@ k
1120+
| _ -> fun k -> Cmts.fmt c ppat_loc @@ (fmt_opt pro $ k) )
11211121
@@ hovbox_if box 0
11221122
@@ fmt_pattern_attributes c xpat
11231123
@@
@@ -1495,7 +1495,7 @@ and fmt_indexop_access c ctx ~fmt_atrs ~has_attr ~parens x =
14951495
(str ";" $ space_break)
14961496
(sub_exp ~ctx >> fmt_expression c) ) )
14971497
$ opt pia_rhs (fun e ->
1498-
fmt_assign_arrow c $ fmt_expression c (sub_exp ~ctx e) ) )
1498+
fmt_assign_arrow c $ fmt_expression c (sub_exp ~ctx e) ) )
14991499
$ fmt_atrs ) )
15001500

15011501
(** Format a [Pexp_function]. [wrap_intro] wraps up to after the [->] and is
@@ -1873,7 +1873,7 @@ and fmt_infix_op_args c ~parens xexp op_args =
18731873
((not very_last) && exposed_right_exp Ast.Non_apply xarg.ast)
18741874
|| parenze_exp xarg
18751875
in
1876-
if Params.Exp.Infix_op_arg.dock c.conf xarg then
1876+
if Params.Exp.Infix_op_arg.dock xarg then
18771877
(* Indentation of docked fun or function start before the operator. *)
18781878
hovbox ~name:"Infix_op_arg docked" 2
18791879
(fmt_expression c ~parens ~box:false ~pro xarg)
@@ -2311,8 +2311,7 @@ and fmt_expression c ?(box = true) ?(pro = noop) ?eol ?parens
23112311
parenthesis. *)
23122312
let dock_fun_arg =
23132313
(* Do not dock the arguments when there's more than one. *)
2314-
(not c.conf.fmt_opts.ocp_indent_compat.v)
2315-
|| Location.line_difference e0.pexp_loc last_arg.pexp_loc = 0
2314+
Location.line_difference e0.pexp_loc last_arg.pexp_loc = 0
23162315
in
23172316
if parens || not dock_fun_arg then (noop, pro) else (pro, noop)
23182317
in
@@ -2757,8 +2756,8 @@ and fmt_expression c ?(box = true) ?(pro = noop) ?eol ?parens
27572756
( fmt_pattern c ~pro:(if_newline "| ")
27582757
(sub_pat ~ctx pc_lhs)
27592758
$ opt pc_guard (fun g ->
2760-
space_break $ str "when "
2761-
$ fmt_expression c (sub_exp ~ctx g) )
2759+
space_break $ str "when "
2760+
$ fmt_expression c (sub_exp ~ctx g) )
27622761
$ space_break $ str "->"
27632762
$ fmt_if parens_here (str " (") ) )
27642763
$ break 1 2
@@ -3445,8 +3444,8 @@ and fmt_case c ctx ~first ~last case =
34453444
( hvbox 0
34463445
( fmt_pattern c ~pro:p.bar ~parens:paren_lhs xlhs
34473446
$ opt pc_guard (fun g ->
3448-
break 1 2 $ str "when "
3449-
$ fmt_expression c (sub_exp ~ctx g) ) )
3447+
break 1 2 $ str "when " $ fmt_expression c (sub_exp ~ctx g) )
3448+
)
34503449
$ p.break_before_arrow $ str "->" $ p.break_after_arrow
34513450
$ p.open_paren_branch )
34523451
$ p.break_after_opening_paren
@@ -3778,9 +3777,9 @@ and fmt_type_extension c ctx
37783777
$ str " +="
37793778
$ fmt_private_flag c ptyext_private
37803779
$ list_fl ptyext_constructors (fun ~first ~last:_ x ->
3781-
let bar_fits = if first then "" else "| " in
3782-
cbreak ~fits:("", 1, bar_fits) ~breaks:("", 0, "| ")
3783-
$ fmt_ctor x ) )
3780+
let bar_fits = if first then "" else "| " in
3781+
cbreak ~fits:("", 1, bar_fits) ~breaks:("", 0, "| ")
3782+
$ fmt_ctor x ) )
37843783
$ fmt_item_attributes c ~pre:(Break (1, 0)) attrs_after )
37853784

37863785
and fmt_type_exception ~pre c ctx
@@ -4092,46 +4091,46 @@ and fmt_class_types c ~pre ~sep cls =
40924091
and fmt_class_exprs c cls =
40934092
hvbox 0
40944093
@@ list_fl cls (fun ~first ~last:_ cl ->
4095-
update_config_maybe_disabled_attrs c cl.pci_loc cl.pci_attributes
4096-
@@ fun c ->
4097-
let ctx = Cd cl in
4098-
let xargs = cl.pci_args in
4099-
let ext = cl.pci_attributes.attrs_extension in
4100-
let doc_before, doc_after, attrs_before, attrs_after =
4101-
let force_before = not (Cl.is_simple cl.pci_expr) in
4102-
fmt_docstring_around_item_attrs ~force_before c cl.pci_attributes
4103-
in
4104-
let class_expr =
4105-
let pro =
4106-
box_fun_decl_args c 2
4107-
( hovbox 2
4108-
( str (if first then "class" else "and")
4109-
$ fmt_if first (fmt_extension_suffix c ext)
4110-
$ fmt_attributes c ~pre:(Break (1, 0)) attrs_before
4111-
$ fmt_virtual_flag c cl.pci_virt
4112-
$ space_break
4113-
$ fmt_class_params c ctx cl.pci_params
4114-
$ fmt_str_loc c cl.pci_name )
4115-
$ fmt_if (not (List.is_empty xargs)) space_break
4116-
$ wrap_fun_decl_args c (fmt_class_fun_args c xargs) )
4117-
in
4118-
let intro =
4119-
match cl.pci_constraint with
4120-
| Some ty ->
4121-
fmt_class_type c
4122-
~pro:(pro $ str " :" $ space_break)
4123-
(sub_cty ~ctx ty)
4124-
| None -> pro
4125-
in
4126-
hovbox 2
4127-
( hovbox 2 (intro $ space_break $ str "=")
4128-
$ space_break
4129-
$ fmt_class_expr c (sub_cl ~ctx cl.pci_expr) )
4130-
$ fmt_item_attributes c ~pre:(Break (1, 0)) attrs_after
4131-
in
4132-
fmt_if (not first) (str "\n" $ force_break)
4133-
$ hovbox 0
4134-
@@ Cmts.fmt c cl.pci_loc (doc_before $ class_expr $ doc_after) )
4094+
update_config_maybe_disabled_attrs c cl.pci_loc cl.pci_attributes
4095+
@@ fun c ->
4096+
let ctx = Cd cl in
4097+
let xargs = cl.pci_args in
4098+
let ext = cl.pci_attributes.attrs_extension in
4099+
let doc_before, doc_after, attrs_before, attrs_after =
4100+
let force_before = not (Cl.is_simple cl.pci_expr) in
4101+
fmt_docstring_around_item_attrs ~force_before c cl.pci_attributes
4102+
in
4103+
let class_expr =
4104+
let pro =
4105+
box_fun_decl_args c 2
4106+
( hovbox 2
4107+
( str (if first then "class" else "and")
4108+
$ fmt_if first (fmt_extension_suffix c ext)
4109+
$ fmt_attributes c ~pre:(Break (1, 0)) attrs_before
4110+
$ fmt_virtual_flag c cl.pci_virt
4111+
$ space_break
4112+
$ fmt_class_params c ctx cl.pci_params
4113+
$ fmt_str_loc c cl.pci_name )
4114+
$ fmt_if (not (List.is_empty xargs)) space_break
4115+
$ wrap_fun_decl_args c (fmt_class_fun_args c xargs) )
4116+
in
4117+
let intro =
4118+
match cl.pci_constraint with
4119+
| Some ty ->
4120+
fmt_class_type c
4121+
~pro:(pro $ str " :" $ space_break)
4122+
(sub_cty ~ctx ty)
4123+
| None -> pro
4124+
in
4125+
hovbox 2
4126+
( hovbox 2 (intro $ space_break $ str "=")
4127+
$ space_break
4128+
$ fmt_class_expr c (sub_cl ~ctx cl.pci_expr) )
4129+
$ fmt_item_attributes c ~pre:(Break (1, 0)) attrs_after
4130+
in
4131+
fmt_if (not first) (str "\n" $ force_break)
4132+
$ hovbox 0
4133+
@@ Cmts.fmt c cl.pci_loc (doc_before $ class_expr $ doc_after) )
41354134

41364135
and fmt_module c ctx ?rec_ ?epi ?(can_sparse = false) keyword ?(eqty = "=")
41374136
name xargs xbody xmty ~attrs ~rec_flag =
@@ -4229,13 +4228,13 @@ and fmt_module c ctx ?rec_ ?epi ?(can_sparse = false) keyword ?(eqty = "=")
42294228
$ fmt_item_attributes c ~pre:(Break (1, 0)) attrs_after
42304229
$ doc_after
42314230
$ opt epi (fun epi ->
4232-
fmt_or compact
4233-
(fmt_or
4234-
( Option.is_some blk_b.epi
4235-
&& not c.conf.fmt_opts.ocp_indent_compat.v )
4236-
(str " ") space_break )
4237-
(break 1 (-2))
4238-
$ epi ) )
4231+
fmt_or compact
4232+
(fmt_or
4233+
( Option.is_some blk_b.epi
4234+
&& not c.conf.fmt_opts.ocp_indent_compat.v )
4235+
(str " ") space_break )
4236+
(break 1 (-2))
4237+
$ epi ) )
42394238

42404239
and fmt_module_declaration c ~rec_flag ~first {ast= pmd; _} =
42414240
protect c (Md pmd)

lib/Fmt_odoc.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -494,8 +494,8 @@ let fmt_tag_args ?arg ?txt c tag =
494494
at $ str tag
495495
$ opt arg (fun x -> char ' ' $ x)
496496
$ opt txt (function
497-
| [] -> noop
498-
| x -> space_break $ hovbox 0 (fmt_nestable_block_elements c x) )
497+
| [] -> noop
498+
| x -> space_break $ hovbox 0 (fmt_nestable_block_elements c x) )
499499

500500
let wrap_see = function
501501
| `Url -> wrap (str "<") (str ">")

lib/Non_overlapping_interval_tree.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ module Make (Itv : IN) = struct
5555
if Itv.contains root elt then
5656
let ancestors = root :: ancestors in
5757
( match Map.find map root with
58-
| Some children -> parents map children ~ancestors elt
59-
| None -> ancestors )
58+
| Some children -> parents map children ~ancestors elt
59+
| None -> ancestors )
6060
|> Option.some
6161
else None ) )
6262

lib/Normalize_extended_ast.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ let dedup_cmts fragment ast comments =
4343
let normalize_comments ~normalize_cmt dedup fmt comments =
4444
dedup comments
4545
|> List.sort ~compare:(fun a b ->
46-
Migrate_ast.Location.compare (Cmt.loc a) (Cmt.loc b) )
46+
Migrate_ast.Location.compare (Cmt.loc a) (Cmt.loc b) )
4747
|> List.iter ~f:(fun cmt -> Format.fprintf fmt "%s," (normalize_cmt cmt))
4848

4949
let normalize_parse_result ~normalize_cmt ast_kind ast comments =

0 commit comments

Comments
 (0)