Skip to content

Commit cd80d55

Browse files
authored
Fix arrow type indentation with break_separators=before (#2598)
1 parent 8bc9f0f commit cd80d55

File tree

8 files changed

+67
-18
lines changed

8 files changed

+67
-18
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ profile. This started with version 0.26.0.
5858
The attribute is moved from `begin .. end [@attr]` to `begin [@attr] .. end`.
5959
- Fix missing parentheses around `let .. in [@attr]` (#2564, @Julow)
6060
- Display `a##b` instead of `a ## b` and similarly for operators that start with # (#2580, @v-gb)
61+
- \* Fix arrow type indentation with `break-separators=before` (#2598, @Julow)
6162
- Fix formatting of short `fun` expressions with the janestreet profile (#2593, @Julow)
6263

6364
### Changes

lib/Fmt_ast.ml

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -752,18 +752,19 @@ and fmt_type_cstr c ?(pro = ":") ?constraint_ctx xtyp =
752752
let colon_before = Poly.(c.conf.fmt_opts.break_colon.v = `Before) in
753753
let wrap, inner_pro, box =
754754
match xtyp.ast.ptyp_desc with
755-
| (Ptyp_poly (_, {ptyp_desc= Ptyp_arrow _; _}) | Ptyp_arrow _)
756-
when colon_before ->
755+
| (Ptyp_poly _ | Ptyp_arrow _) when colon_before ->
757756
let outer_pro =
758-
if c.conf.fmt_opts.ocp_indent_compat.v then
759-
fits_breaks (pro ^ " ") (pro ^ " ")
760-
else str pro $ str " "
757+
match (xtyp.ast.ptyp_desc, c.conf.fmt_opts.break_separators.v) with
758+
| ( (Ptyp_poly (_, {ptyp_desc= Ptyp_arrow _; _}) | Ptyp_arrow _)
759+
, `Before ) ->
760+
fits_breaks (pro ^ " ") (pro ^ " ")
761+
| _ -> str pro $ str " "
761762
in
762763
let pre_break =
763764
if colon_before then fits_breaks " " ~hint:(1000, 0) ""
764765
else break 0 ~-1
765766
in
766-
let wrap x = pre_break $ cbox 0 (outer_pro $ x) in
767+
let wrap x = pre_break $ hvbox 0 (outer_pro $ x) in
767768
(wrap, None, false)
768769
| _ ->
769770
( (fun k ->
@@ -832,7 +833,7 @@ and fmt_arrow_type c ~ctx ?indent ~parens ~parent_has_parens args fmt_ret_typ
832833
of the expression, i.e. if the expression is part of a `fun`
833834
expression. *)
834835
and fmt_core_type c ?(box = true) ?pro ?(pro_space = true) ?constraint_ctx
835-
({ast= typ; ctx} as xtyp) =
836+
({ast= typ; ctx= ctx0} as xtyp) =
836837
protect c (Typ typ)
837838
@@
838839
let {ptyp_desc; ptyp_attributes; ptyp_loc; _} = typ in
@@ -857,7 +858,7 @@ and fmt_core_type c ?(box = true) ?pro ?(pro_space = true) ?constraint_ctx
857858
c.conf
858859
@@
859860
let in_type_declaration =
860-
match ctx with
861+
match ctx0 with
861862
| Td {ptype_manifest= Some t; _} -> phys_equal t typ
862863
| _ -> false
863864
in
@@ -921,10 +922,26 @@ and fmt_core_type c ?(box = true) ?pro ?(pro_space = true) ?constraint_ctx
921922
| Ptyp_poly ([], _) ->
922923
impossible "produced by the parser, handled elsewhere"
923924
| Ptyp_poly (a1N, t) ->
925+
let ctx_is_value_constraint = function Vc _ -> true | _ -> false in
926+
let break, box_core_type =
927+
match
928+
(c.conf.fmt_opts.break_separators.v, c.conf.fmt_opts.break_colon.v)
929+
with
930+
| `Before, `Before when ctx_is_value_constraint ctx0 ->
931+
(* Special formatting for leading [->] in let bindings. *)
932+
let indent =
933+
match t.ptyp_desc with Ptyp_arrow _ -> 3 | _ -> 2
934+
in
935+
(break 1 indent, Some false)
936+
| _ -> (space_break, None)
937+
in
924938
hovbox_if box 0
925-
( list a1N space_break (fun {txt; _} -> fmt_type_var txt)
926-
$ str "." $ space_break
927-
$ fmt_core_type c ~box:true (sub_typ ~ctx t) )
939+
( hovbox 0
940+
( list a1N space_break (fun {txt; _} -> fmt_type_var txt)
941+
$ str "." )
942+
$ break
943+
$ fmt_core_type c ?box:box_core_type ~pro_space:false
944+
(sub_typ ~ctx t) )
928945
| Ptyp_tuple typs ->
929946
hvbox 0
930947
(wrap_if parenze_constraint_ctx (str "(") (str ")")

test/passing/tests/break_colon-before.ml.ref

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ let ssmap
7070
()
7171

7272
let ssmap
73-
: (module MapT
74-
with type key = string
75-
and type data = string
76-
and type map = SSMap.map )
73+
: (module MapT
74+
with type key = string
75+
and type data = string
76+
and type map = SSMap.map )
7777
-> unit =
7878
()
7979

test/passing/tests/js_source.ml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8249,3 +8249,13 @@ let _ =
82498249
fun id -> not (Ident.HashQueue.mem q id)
82508250
| None -> fun id -> not (List.mem ~equal:Ident.equal ids id)
82518251
;;
8252+
8253+
type callbacks =
8254+
{ html_debug_new_node_session_f :
8255+
'a.
8256+
?kind:[ `ComputePre | `ExecNode | `ExecNodeNarrowing | `WTO ]
8257+
-> pp_name:(Format.formatter -> unit)
8258+
-> Procdesc.Node.t
8259+
-> f:(unit -> 'a)
8260+
-> 'a
8261+
}

test/passing/tests/js_source.ml.ocp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10537,3 +10537,13 @@ let _ =
1053710537
fun id -> not (Ident.HashQueue.mem q id)
1053810538
| None -> fun id -> not (List.mem ~equal:Ident.equal ids id)
1053910539
;;
10540+
10541+
type callbacks =
10542+
{ html_debug_new_node_session_f :
10543+
'a.
10544+
?kind:[ `ComputePre | `ExecNode | `ExecNodeNarrowing | `WTO ]
10545+
-> pp_name:(Format.formatter -> unit)
10546+
-> Procdesc.Node.t
10547+
-> f:(unit -> 'a)
10548+
-> 'a
10549+
}

test/passing/tests/js_source.ml.ref

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10537,3 +10537,13 @@ let _ =
1053710537
fun id -> not (Ident.HashQueue.mem q id)
1053810538
| None -> fun id -> not (List.mem ~equal:Ident.equal ids id)
1053910539
;;
10540+
10541+
type callbacks =
10542+
{ html_debug_new_node_session_f :
10543+
'a.
10544+
?kind:[ `ComputePre | `ExecNode | `ExecNodeNarrowing | `WTO ]
10545+
-> pp_name:(Format.formatter -> unit)
10546+
-> Procdesc.Node.t
10547+
-> f:(unit -> 'a)
10548+
-> 'a
10549+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Warning: tests/polytypes.ml:47 exceeds the margin
1+
Warning: tests/polytypes.ml:48 exceeds the margin

test/passing/tests/polytypes-janestreet.ml.ref

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ let t1 : 'a 'b. 'a t -> b t = ()
22

33
let t2
44
: 'a 'b.
5-
'a t________________________________ -> 'b t_______________________________________
5+
'a t________________________________
6+
-> 'b t_______________________________________
67
=
78
()
89
;;
910

1011
let t3
1112
: 'long 'sequence 'of_ 'universally 'quantified 'type_ 'variables 'that 'must 'wrap.
12-
'a t_________________________________________________
13+
'a t_________________________________________________
1314
-> 'b t______________________________________________________________
1415
-> 'c t______________________________________________________________
1516
=

0 commit comments

Comments
 (0)