Skip to content

Commit cb9b356

Browse files
authored
Improvements to ocp-indent-compat and the Janestreet profile (#2314)
The following changes apply when ocp-indent-compat is enabled: - Indent extensions and attributes containing structure items according to `stritem_extension_indent`. - Consistent indentation of regular and polymorphic variants types in type exprs, patterns and expressions. - Don't align pack expressions. - Consistent indentation of methods. - Tweak indentation of comments in records. - Tweak indentation of fun arguments.
1 parent aa2ec82 commit cb9b356

39 files changed

+428
-197
lines changed

Diff for: CHANGES.md

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ profile. This started with version 0.26.0.
1717
- \* Reduce the indentation of (polytype) type constraints (#2437, @gpetiot)
1818
- \* Consistent indentation of polymorphic variant arguments (#2427, @Julow)
1919
- \* Don't align breaking module arguments (#2505, @Julow)
20+
- Improvements to ocp-indent-compat and the Janestreet profile (#2314, @Julow)
2021

2122
### Fixed
2223

Diff for: lib/Conf.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ let janestreet_profile from =
227227
; margin= elt 90
228228
; match_indent= elt 0
229229
; match_indent_nested= elt `Never
230-
; max_indent= elt @@ Some 2
230+
; max_indent= elt None
231231
; module_item_spacing= elt `Compact
232232
; nested_match= elt `Wrap
233233
; ocp_indent_compat= elt true

Diff for: lib/Fmt_ast.ml

+52-37
Original file line numberDiff line numberDiff line change
@@ -578,11 +578,21 @@ let rec fmt_extension_aux c ctx ~key (ext, pld) =
578578
when Source.extension_using_sugar ~name:ext ~payload:ppat_loc ->
579579
fmt_pattern c ~ext (sub_pat ~ctx pat)
580580
| _ ->
581-
wrap "[" "]"
582-
( str (Ext.Key.to_string key)
583-
$ fmt_str_loc c ext
584-
$ fmt_payload c (Pld pld) pld
585-
$ fmt_if (Exposed.Right.payload pld) " " )
581+
let box =
582+
if c.conf.fmt_opts.ocp_indent_compat.v then
583+
match pld with
584+
| PStr [{pstr_desc= Pstr_eval _; _}] | PTyp _ | PPat _ ->
585+
hvbox c.conf.fmt_opts.extension_indent.v
586+
| PSig _ | PStr _ ->
587+
hvbox c.conf.fmt_opts.stritem_extension_indent.v
588+
else Fn.id
589+
in
590+
box
591+
(wrap "[" "]"
592+
( str (Ext.Key.to_string key)
593+
$ fmt_str_loc c ext
594+
$ fmt_payload c (Pld pld) pld
595+
$ fmt_if (Exposed.Right.payload pld) " " ) )
586596

587597
and fmt_extension = fmt_extension_aux ~key:Ext.Key.Regular
588598

@@ -622,11 +632,7 @@ and fmt_attribute c ~key {attr_name; attr_payload; attr_loc} =
622632
and fmt_attributes_aux c ?pre ?suf ~key attrs =
623633
let num = List.length attrs in
624634
fmt_if_k (num > 0)
625-
( opt pre (function
626-
(* Breaking before an attribute can confuse ocp-indent that will
627-
produce a suboptimal indentation. *)
628-
| Space when c.conf.fmt_opts.ocp_indent_compat.v -> sp Blank
629-
| pre -> sp pre )
635+
( opt pre sp
630636
$ hvbox_if (num > 1) 0
631637
(hvbox 0 (list attrs "@ " (fmt_attribute c ~key)) $ opt suf str) )
632638

@@ -979,7 +985,7 @@ and fmt_row_field c ctx {prf_desc; prf_attributes; prf_loc} =
979985
| Rinherit typ -> fmt_core_type c (sub_typ ~ctx typ)
980986
in
981987
hvbox 0
982-
( hvbox 0 (Cmts.fmt c prf_loc row)
988+
( hvbox (Params.Indent.variant_type_arg c.conf) (Cmts.fmt c prf_loc row)
983989
$ fmt_attributes_and_docstrings c prf_attributes )
984990

985991
and fmt_pattern_attributes c xpat k =
@@ -1060,7 +1066,8 @@ and fmt_pattern ?ext c ?pro ?parens ?(box = false)
10601066
Cmts.fmt c ppat_loc
10611067
(hvbox 0 (fmt_pat_cons c ~parens (List.map lp ~f:(sub_pat ~ctx))))
10621068
| Ppat_construct (lid, Some (exists, pat)) ->
1063-
cbox 0
1069+
cbox
1070+
(Params.Indent.variant c.conf ~parens)
10641071
(Params.parens_if parens c.conf
10651072
(hvbox 2
10661073
( fmt_longident_loc c lid $ fmt "@ "
@@ -1074,7 +1081,8 @@ and fmt_pattern ?ext c ?pro ?parens ?(box = false)
10741081
$ fmt_pattern c (sub_pat ~ctx pat) ) ) )
10751082
| Ppat_variant (lbl, None) -> variant_var c lbl
10761083
| Ppat_variant (lbl, Some pat) ->
1077-
cbox 0
1084+
cbox
1085+
(Params.Indent.variant c.conf ~parens)
10781086
(Params.parens_if parens c.conf
10791087
(hvbox 2
10801088
( variant_var c lbl $ fmt "@ "
@@ -2182,7 +2190,8 @@ and fmt_expression c ?(box = true) ?(pro = noop) ?eol ?parens
21822190
| Pexp_variant (s, arg) ->
21832191
pro
21842192
$ Params.parens_if parens c.conf
2185-
(hvbox 2
2193+
(hvbox
2194+
(Params.Indent.variant c.conf ~parens)
21862195
( variant_var c s
21872196
$ opt arg (fmt "@ " >$ (sub_exp ~ctx >> fmt_expression c))
21882197
$ fmt_atrs ) )
@@ -2280,7 +2289,7 @@ and fmt_expression c ?(box = true) ?(pro = noop) ?eol ?parens
22802289
fmt_extension_suffix c ext ) )
22812290
~fmt_attributes:
22822291
(fmt_attributes c ~pre:Blank pexp_attributes)
2283-
~fmt_cond:(fmt_expression c)
2292+
~fmt_cond:(fmt_expression ~box:false c)
22842293
in
22852294
parens_prev_bch := parens_bch ;
22862295
p.box_branch
@@ -2461,8 +2470,8 @@ and fmt_expression c ?(box = true) ?(pro = noop) ?eol ?parens
24612470
| Pexp_pack (me, pt) ->
24622471
let outer_pro = pro in
24632472
let outer_parens = parens && has_attr in
2464-
let inner_parens = true in
24652473
let blk = fmt_module_expr c (sub_mod ~ctx me) in
2474+
let align = Params.Align.module_pack c.conf ~me in
24662475
let opn_paren =
24672476
match c.conf.fmt_opts.indicate_multiline_delimiters.v with
24682477
| `No | `Closing_on_separate_line -> str "("
@@ -2475,11 +2484,11 @@ and fmt_expression c ?(box = true) ?(pro = noop) ?eol ?parens
24752484
fits_breaks ~level "(" "( "
24762485
and cls_paren = closing_paren c ~offset:(-2) in
24772486
let pro =
2478-
fmt_if_k inner_parens opn_paren
2487+
fmt_if_k (not align) opn_paren
24792488
$ str "module"
24802489
$ fmt_extension_suffix c ext
24812490
$ char ' '
2482-
and epi = fmt_if_k inner_parens cls_paren in
2491+
and epi = cls_paren in
24832492
let fmt_mod m =
24842493
match pt with
24852494
| Some (id, cnstrs) ->
@@ -2491,7 +2500,9 @@ and fmt_expression c ?(box = true) ?(pro = noop) ?eol ?parens
24912500
outer_pro
24922501
$ hvbox 0
24932502
(Params.parens_if outer_parens c.conf
2494-
(compose_module ~pro ~epi blk ~f:fmt_mod $ fmt_atrs) )
2503+
( fmt_if_k align opn_paren
2504+
$ compose_module ~pro ~epi blk ~f:fmt_mod
2505+
$ fmt_atrs ) )
24952506
| Pexp_record (flds, default) ->
24962507
let fmt_field (lid, tc, exp) =
24972508
let typ1, typ2 =
@@ -3076,8 +3087,10 @@ and fmt_class_field c {ast= cf; _} =
30763087
hvbox 2
30773088
( hovbox 2
30783089
( hovbox 4
3079-
(box_fun_decl_args c 4
3080-
( box_fun_sig_args c 4
3090+
(box_fun_decl_args c
3091+
(Params.Indent.fun_args c.conf)
3092+
( box_fun_sig_args c
3093+
(Params.Indent.fun_type_annot c.conf)
30813094
( str "method" $ virtual_or_override kind
30823095
$ fmt_private_virtual_flag c pv
30833096
$ str " " $ fmt_str_loc c name $ typ )
@@ -4450,29 +4463,31 @@ and fmt_value_binding c ~rec_flag ?ext ?in_ ?epi
44504463
, Cmts.Toplevel.fmt_before c lb_loc
44514464
, Cmts.Toplevel.fmt_after c lb_loc )
44524465
in
4466+
let decl_args =
4467+
let decl =
4468+
fmt_str_loc c lb_op
4469+
$ fmt_extension_suffix c ext
4470+
$ fmt_attributes c at_attrs $ fmt_if rec_flag " rec"
4471+
$ fmt_or pat_has_cmt "@ " " "
4472+
and pattern = fmt_pattern c lb_pat
4473+
and args =
4474+
fmt_if_k
4475+
(not (List.is_empty lb_args))
4476+
(fmt "@ " $ wrap_fun_decl_args c (fmt_fun_args c lb_args))
4477+
$ fmt_newtypes
4478+
in
4479+
box_fun_decl_args c 4 (Params.Align.fun_decl c.conf ~decl ~pattern ~args)
4480+
in
44534481
fmt_docstring c ~epi:(fmt "@\n") doc1
44544482
$ cmts_before
44554483
$ hvbox 0
44564484
( hvbox indent
44574485
( hvbox_if toplevel 0
44584486
( hvbox_if toplevel indent
44594487
( hovbox 2
4460-
( hovbox 4
4461-
( box_fun_decl_args c 4
4462-
( hovbox 4
4463-
( fmt_str_loc c lb_op
4464-
$ fmt_extension_suffix c ext
4465-
$ fmt_attributes c at_attrs
4466-
$ fmt_if rec_flag " rec"
4467-
$ fmt_or pat_has_cmt "@ " " "
4468-
$ fmt_pattern c lb_pat )
4469-
$ fmt_if_k
4470-
(not (List.is_empty lb_args))
4471-
( fmt "@ "
4472-
$ wrap_fun_decl_args c
4473-
(fmt_fun_args c lb_args) )
4474-
$ fmt_newtypes )
4475-
$ fmt_cstr )
4488+
( hovbox
4489+
(Params.Indent.fun_type_annot c.conf)
4490+
(decl_args $ fmt_cstr)
44764491
$ fmt_if_k (not lb_pun)
44774492
(fmt_or_k c.conf.fmt_opts.ocp_indent_compat.v
44784493
(fits_breaks " =" ~hint:(1000, 0) "=")

Diff for: lib/Params.ml

+18-9
Original file line numberDiff line numberDiff line change
@@ -629,14 +629,6 @@ let comma_sep (c : Conf.t) : Fmt.s =
629629
| `After -> ",@;<1 2>"
630630

631631
module Align = struct
632-
(** Whether [exp] occurs in [args] as a labelled argument. *)
633-
let is_labelled_arg args exp =
634-
List.exists
635-
~f:(function
636-
| Nolabel, _ -> false
637-
| Labelled _, x | Optional _, x -> phys_equal x exp )
638-
args
639-
640632
let general (c : Conf.t) t =
641633
hvbox_if (not c.fmt_opts.align_symbol_open_paren.v) 0 t
642634

@@ -665,6 +657,19 @@ module Align = struct
665657
| _ -> parens && not c.fmt_opts.align_symbol_open_paren.v
666658
in
667659
hvbox_if align 0 t
660+
661+
let fun_decl (c : Conf.t) ~decl ~pattern ~args =
662+
if c.fmt_opts.ocp_indent_compat.v then
663+
hovbox 4 (decl $ hvbox 2 (pattern $ args))
664+
else hovbox 4 (decl $ pattern) $ args
665+
666+
let module_pack (c : Conf.t) ~me =
667+
if not c.fmt_opts.ocp_indent_compat.v then false
668+
else
669+
(* Align when the constraint is not desugared. *)
670+
match me.pmod_desc with
671+
| Pmod_structure _ | Pmod_ident _ -> false
672+
| _ -> true
668673
end
669674

670675
module Indent = struct
@@ -715,7 +720,7 @@ module Indent = struct
715720

716721
let record_docstring (c : Conf.t) =
717722
if ocp c then
718-
match c.fmt_opts.break_separators.v with `Before -> -2 | `After -> 0
723+
match c.fmt_opts.break_separators.v with `Before -> 0 | `After -> 2
719724
else 4
720725

721726
let constructor_docstring c = if ocp c then 0 else 4
@@ -733,4 +738,8 @@ module Indent = struct
733738
let mty_with c = if ocp c then 0 else 2
734739

735740
let type_constr c = if ocp c then 2 else 0
741+
742+
let variant c ~parens = if ocp c && parens then 3 else 2
743+
744+
let variant_type_arg c = if ocp c then 2 else 0
736745
end

Diff for: lib/Params.mli

+9
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,11 @@ module Align : sig
177177

178178
val function_ :
179179
Conf.t -> parens:bool -> ctx0:Ast.t -> self:expression -> Fmt.t -> Fmt.t
180+
181+
val fun_decl : Conf.t -> decl:Fmt.t -> pattern:Fmt.t -> args:Fmt.t -> Fmt.t
182+
183+
val module_pack : Conf.t -> me:module_expr -> bool
184+
(** Not implemented as a wrapper to work with the blk system. *)
180185
end
181186

182187
module Indent : sig
@@ -227,4 +232,8 @@ module Indent : sig
227232
(** Types *)
228233

229234
val type_constr : Conf.t -> int
235+
236+
val variant : Conf.t -> parens:bool -> int
237+
238+
val variant_type_arg : Conf.t -> int
230239
end

Diff for: test/cli/multiple_projects.t

+6-6
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ Second project formatted with the 'ocamlformat' profile:
1414

1515
$ cat project1/main.ml
1616
let _machin
17-
?aaaaaaaaaa:_
18-
?bbbbbbbbbbb:_
19-
?cccccccccccc:_
20-
?ddddddddddddd:_
21-
?eeeeeeeeeeee:_
22-
()
17+
?aaaaaaaaaa:_
18+
?bbbbbbbbbbb:_
19+
?cccccccccccc:_
20+
?ddddddddddddd:_
21+
?eeeeeeeeeeee:_
22+
()
2323
=
2424
()
2525
;;

Diff for: test/passing/tests/break_separators-after.ml.ref

+11-6
Original file line numberDiff line numberDiff line change
@@ -263,11 +263,11 @@ let x {aaaaaaaaaaaaaa; aaaaaaaaaaaaa; aaaaaaaaaa} =
263263
{aaaaaaaaaaaa= aaaaaaaaaaaaaaaaa; bbbbbbbbbbbbb= bbb bb bbbbbb}
264264

265265
let x
266-
{ aaaaaaaaaaaaaaaaaaaaaa;
267-
aaaaaaaaaaaaaaaaaaa;
268-
aaaaaaaaaaaaaa;
269-
aaaaaaaaaaaaaaaaaa;
270-
aaaaaaaaaa }
266+
{ aaaaaaaaaaaaaaaaaaaaaa;
267+
aaaaaaaaaaaaaaaaaaa;
268+
aaaaaaaaaaaaaa;
269+
aaaaaaaaaaaaaaaaaa;
270+
aaaaaaaaaa }
271271
=
272272
{ aaaaaaaaaaaa= aaaaaaaaaaaaaaaaa;
273273
bbbbbbbbbbbbb= bbb bb bbbbbb;
@@ -370,7 +370,12 @@ let g () =
370370
hhhhhhhhhh |] ->
371371
fooooooooo
372372

373-
let () = match x with _, (* line 1 line 2 *) Some _ -> x
373+
let () =
374+
match x with
375+
| ( _,
376+
(* line 1 line 2 *)
377+
Some _ ) ->
378+
x
374379

375380
let () =
376381
match x with

Diff for: test/passing/tests/break_separators-after_docked.ml.ref

+13-8
Original file line numberDiff line numberDiff line change
@@ -289,13 +289,13 @@ let x {aaaaaaaaaaaaaa; aaaaaaaaaaaaa; aaaaaaaaaa} =
289289
{aaaaaaaaaaaa= aaaaaaaaaaaaaaaaa; bbbbbbbbbbbbb= bbb bb bbbbbb}
290290

291291
let x
292-
{
293-
aaaaaaaaaaaaaaaaaaaaaa;
294-
aaaaaaaaaaaaaaaaaaa;
295-
aaaaaaaaaaaaaa;
296-
aaaaaaaaaaaaaaaaaa;
297-
aaaaaaaaaa;
298-
}
292+
{
293+
aaaaaaaaaaaaaaaaaaaaaa;
294+
aaaaaaaaaaaaaaaaaaa;
295+
aaaaaaaaaaaaaa;
296+
aaaaaaaaaaaaaaaaaa;
297+
aaaaaaaaaa;
298+
}
299299
=
300300
{
301301
aaaaaaaaaaaa= aaaaaaaaaaaaaaaaa;
@@ -419,7 +419,12 @@ let g () =
419419
|] ->
420420
fooooooooo
421421

422-
let () = match x with _, (* line 1 line 2 *) Some _ -> x
422+
let () =
423+
match x with
424+
| ( _,
425+
(* line 1 line 2 *)
426+
Some _ ) ->
427+
x
423428

424429
let () =
425430
match x with

Diff for: test/passing/tests/break_separators-before_docked.ml.ref

+13-8
Original file line numberDiff line numberDiff line change
@@ -289,13 +289,13 @@ let x {aaaaaaaaaaaaaa; aaaaaaaaaaaaa; aaaaaaaaaa} =
289289
{aaaaaaaaaaaa= aaaaaaaaaaaaaaaaa; bbbbbbbbbbbbb= bbb bb bbbbbb}
290290

291291
let x
292-
{
293-
aaaaaaaaaaaaaaaaaaaaaa
294-
; aaaaaaaaaaaaaaaaaaa
295-
; aaaaaaaaaaaaaa
296-
; aaaaaaaaaaaaaaaaaa
297-
; aaaaaaaaaa
298-
}
292+
{
293+
aaaaaaaaaaaaaaaaaaaaaa
294+
; aaaaaaaaaaaaaaaaaaa
295+
; aaaaaaaaaaaaaa
296+
; aaaaaaaaaaaaaaaaaa
297+
; aaaaaaaaaa
298+
}
299299
=
300300
{
301301
aaaaaaaaaaaa= aaaaaaaaaaaaaaaaa
@@ -419,7 +419,12 @@ let g () =
419419
|] ->
420420
fooooooooo
421421

422-
let () = match x with _, (* line 1 line 2 *) Some _ -> x
422+
let () =
423+
match x with
424+
| ( _
425+
, (* line 1 line 2 *)
426+
Some _ ) ->
427+
x
423428

424429
let () =
425430
match x with

0 commit comments

Comments
 (0)