Skip to content

Commit

Permalink
Fix arrow type indentation with break_separators=before (#2598)
Browse files Browse the repository at this point in the history
  • Loading branch information
Julow authored Oct 28, 2024
1 parent 8bc9f0f commit cd80d55
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 18 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ profile. This started with version 0.26.0.
The attribute is moved from `begin .. end [@attr]` to `begin [@attr] .. end`.
- Fix missing parentheses around `let .. in [@attr]` (#2564, @Julow)
- Display `a##b` instead of `a ## b` and similarly for operators that start with # (#2580, @v-gb)
- \* Fix arrow type indentation with `break-separators=before` (#2598, @Julow)
- Fix formatting of short `fun` expressions with the janestreet profile (#2593, @Julow)

### Changes
Expand Down
39 changes: 28 additions & 11 deletions lib/Fmt_ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -752,18 +752,19 @@ and fmt_type_cstr c ?(pro = ":") ?constraint_ctx xtyp =
let colon_before = Poly.(c.conf.fmt_opts.break_colon.v = `Before) in
let wrap, inner_pro, box =
match xtyp.ast.ptyp_desc with
| (Ptyp_poly (_, {ptyp_desc= Ptyp_arrow _; _}) | Ptyp_arrow _)
when colon_before ->
| (Ptyp_poly _ | Ptyp_arrow _) when colon_before ->
let outer_pro =
if c.conf.fmt_opts.ocp_indent_compat.v then
fits_breaks (pro ^ " ") (pro ^ " ")
else str pro $ str " "
match (xtyp.ast.ptyp_desc, c.conf.fmt_opts.break_separators.v) with
| ( (Ptyp_poly (_, {ptyp_desc= Ptyp_arrow _; _}) | Ptyp_arrow _)
, `Before ) ->
fits_breaks (pro ^ " ") (pro ^ " ")
| _ -> str pro $ str " "
in
let pre_break =
if colon_before then fits_breaks " " ~hint:(1000, 0) ""
else break 0 ~-1
in
let wrap x = pre_break $ cbox 0 (outer_pro $ x) in
let wrap x = pre_break $ hvbox 0 (outer_pro $ x) in
(wrap, None, false)
| _ ->
( (fun k ->
Expand Down Expand Up @@ -832,7 +833,7 @@ and fmt_arrow_type c ~ctx ?indent ~parens ~parent_has_parens args fmt_ret_typ
of the expression, i.e. if the expression is part of a `fun`
expression. *)
and fmt_core_type c ?(box = true) ?pro ?(pro_space = true) ?constraint_ctx
({ast= typ; ctx} as xtyp) =
({ast= typ; ctx= ctx0} as xtyp) =
protect c (Typ typ)
@@
let {ptyp_desc; ptyp_attributes; ptyp_loc; _} = typ in
Expand All @@ -857,7 +858,7 @@ and fmt_core_type c ?(box = true) ?pro ?(pro_space = true) ?constraint_ctx
c.conf
@@
let in_type_declaration =
match ctx with
match ctx0 with
| Td {ptype_manifest= Some t; _} -> phys_equal t typ
| _ -> false
in
Expand Down Expand Up @@ -921,10 +922,26 @@ and fmt_core_type c ?(box = true) ?pro ?(pro_space = true) ?constraint_ctx
| Ptyp_poly ([], _) ->
impossible "produced by the parser, handled elsewhere"
| Ptyp_poly (a1N, t) ->
let ctx_is_value_constraint = function Vc _ -> true | _ -> false in
let break, box_core_type =
match
(c.conf.fmt_opts.break_separators.v, c.conf.fmt_opts.break_colon.v)
with
| `Before, `Before when ctx_is_value_constraint ctx0 ->
(* Special formatting for leading [->] in let bindings. *)
let indent =
match t.ptyp_desc with Ptyp_arrow _ -> 3 | _ -> 2
in
(break 1 indent, Some false)
| _ -> (space_break, None)
in
hovbox_if box 0
( list a1N space_break (fun {txt; _} -> fmt_type_var txt)
$ str "." $ space_break
$ fmt_core_type c ~box:true (sub_typ ~ctx t) )
( hovbox 0
( list a1N space_break (fun {txt; _} -> fmt_type_var txt)
$ str "." )
$ break
$ fmt_core_type c ?box:box_core_type ~pro_space:false
(sub_typ ~ctx t) )
| Ptyp_tuple typs ->
hvbox 0
(wrap_if parenze_constraint_ctx (str "(") (str ")")
Expand Down
8 changes: 4 additions & 4 deletions test/passing/tests/break_colon-before.ml.ref
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ let ssmap
()

let ssmap
: (module MapT
with type key = string
and type data = string
and type map = SSMap.map )
: (module MapT
with type key = string
and type data = string
and type map = SSMap.map )
-> unit =
()

Expand Down
10 changes: 10 additions & 0 deletions test/passing/tests/js_source.ml
Original file line number Diff line number Diff line change
Expand Up @@ -8249,3 +8249,13 @@ let _ =
fun id -> not (Ident.HashQueue.mem q id)
| None -> fun id -> not (List.mem ~equal:Ident.equal ids id)
;;

type callbacks =
{ html_debug_new_node_session_f :
'a.
?kind:[ `ComputePre | `ExecNode | `ExecNodeNarrowing | `WTO ]
-> pp_name:(Format.formatter -> unit)
-> Procdesc.Node.t
-> f:(unit -> 'a)
-> 'a
}
10 changes: 10 additions & 0 deletions test/passing/tests/js_source.ml.ocp
Original file line number Diff line number Diff line change
Expand Up @@ -10537,3 +10537,13 @@ let _ =
fun id -> not (Ident.HashQueue.mem q id)
| None -> fun id -> not (List.mem ~equal:Ident.equal ids id)
;;

type callbacks =
{ html_debug_new_node_session_f :
'a.
?kind:[ `ComputePre | `ExecNode | `ExecNodeNarrowing | `WTO ]
-> pp_name:(Format.formatter -> unit)
-> Procdesc.Node.t
-> f:(unit -> 'a)
-> 'a
}
10 changes: 10 additions & 0 deletions test/passing/tests/js_source.ml.ref
Original file line number Diff line number Diff line change
Expand Up @@ -10537,3 +10537,13 @@ let _ =
fun id -> not (Ident.HashQueue.mem q id)
| None -> fun id -> not (List.mem ~equal:Ident.equal ids id)
;;

type callbacks =
{ html_debug_new_node_session_f :
'a.
?kind:[ `ComputePre | `ExecNode | `ExecNodeNarrowing | `WTO ]
-> pp_name:(Format.formatter -> unit)
-> Procdesc.Node.t
-> f:(unit -> 'a)
-> 'a
}
2 changes: 1 addition & 1 deletion test/passing/tests/polytypes-janestreet.ml.err
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Warning: tests/polytypes.ml:47 exceeds the margin
Warning: tests/polytypes.ml:48 exceeds the margin
5 changes: 3 additions & 2 deletions test/passing/tests/polytypes-janestreet.ml.ref
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ let t1 : 'a 'b. 'a t -> b t = ()

let t2
: 'a 'b.
'a t________________________________ -> 'b t_______________________________________
'a t________________________________
-> 'b t_______________________________________
=
()
;;

let t3
: 'long 'sequence 'of_ 'universally 'quantified 'type_ 'variables 'that 'must 'wrap.
'a t_________________________________________________
'a t_________________________________________________
-> 'b t______________________________________________________________
-> 'c t______________________________________________________________
=
Expand Down

0 comments on commit cd80d55

Please sign in to comment.