Skip to content

Commit 21da774

Browse files
committed
Merge branch 'MAIN' into ast-class-expr-let
2 parents 3e670bb + cd80d55 commit 21da774

File tree

11 files changed

+98
-34
lines changed

11 files changed

+98
-34
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ 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)
62+
- Fix formatting of short `fun` expressions with the janestreet profile (#2593, @Julow)
6163
- Fix missing parentheses around a let in class expressions (#2599, @Julow)
6264

6365
### Changes

lib/Fmt_ast.ml

Lines changed: 29 additions & 12 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 ")")
@@ -1509,7 +1526,7 @@ and fmt_function ?(last_arg = false) ?force_closing_paren ~ctx ~ctx0
15091526
let head = fmt_fun_args_typ args typ in
15101527
let body ~pro = pro $ fmt_expression c (sub_exp ~ctx body) in
15111528
let box, closing_paren_offset =
1512-
Params.Exp.box_fun_expr c.conf ~source:c.source ~ctx0 ~ctx ~parens
1529+
Params.Exp.box_fun_expr c.conf ~source:c.source ~ctx0 ~ctx
15131530
in
15141531
let closing_paren_offset =
15151532
if should_box then closing_paren_offset else ~-2

lib/Params.ml

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ module Exp = struct
213213
( kw_in_box
214214
$ hvbox_if should_box_args 0 (args $ fmt_opt annot $ fmt_opt epi) )
215215

216-
let box_fun_expr (c : Conf.t) ~source ~ctx0 ~ctx ~parens =
216+
let box_fun_expr (c : Conf.t) ~source ~ctx0 ~ctx =
217217
let indent =
218218
if ctx_is_rhs_of_infix ~ctx0 ~ctx then 0
219219
else if Poly.equal c.fmt_opts.function_indent_nested.v `Always then
@@ -244,14 +244,7 @@ module Exp = struct
244244
else 2
245245
in
246246
let name = "Params.box_fun_expr" in
247-
let mkbox =
248-
match ctx0 with
249-
| Str _ | Lb _ -> hvbox
250-
| _ ->
251-
(* JS: The body of a [fun] must break if the intro is too large,
252-
except if the [fun] is small and parenthesed. *)
253-
if ocp c && not parens then hvbox else hovbox
254-
in
247+
let mkbox = if ctx_is_let_or_fun ~ctx ctx0 then hvbox else hovbox in
255248
(mkbox ~name indent, ~-indent)
256249

257250
(* if the function is the last argument of an apply and no other arguments

lib/Params.mli

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ module Exp : sig
5858
-> source:Source.t
5959
-> ctx0:Ast.t
6060
-> ctx:Ast.t
61-
-> parens:bool
6261
-> (Fmt.t -> Fmt.t) * int
6362
(** return a box with an indent and minus the value of the indent to be used for a closing parenthesis *)
6463

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: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8241,3 +8241,21 @@ let _ =
82418241
(* foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo *)
82428242
else f
82438243
;;
8244+
8245+
let _ =
8246+
match ids_queue with
8247+
| Some q ->
8248+
(* this is more efficient than a linear scan of [ids] *)
8249+
fun id -> not (Ident.HashQueue.mem q id)
8250+
| None -> fun id -> not (List.mem ~equal:Ident.equal ids id)
8251+
;;
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.err

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ Warning: tests/js_source.ml:9564 exceeds the margin
33
Warning: tests/js_source.ml:9668 exceeds the margin
44
Warning: tests/js_source.ml:9727 exceeds the margin
55
Warning: tests/js_source.ml:9810 exceeds the margin
6-
Warning: tests/js_source.ml:10309 exceeds the margin
6+
Warning: tests/js_source.ml:10308 exceeds the margin

test/passing/tests/js_source.ml.ocp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10235,8 +10235,7 @@ let _ =
1023510235
foooooooooooooooooo
1023610236
foooooooooooooooooo
1023710237
foooooooooooooooooo
10238-
foooooooooooooooooo ->
10239-
()
10238+
foooooooooooooooooo -> ()
1024010239
;;
1024110240

1024210241
module type For_let_syntax_local =
@@ -10530,3 +10529,21 @@ let _ =
1053010529
(* foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo *)
1053110530
else f
1053210531
;;
10532+
10533+
let _ =
10534+
match ids_queue with
10535+
| Some q ->
10536+
(* this is more efficient than a linear scan of [ids] *)
10537+
fun id -> not (Ident.HashQueue.mem q id)
10538+
| None -> fun id -> not (List.mem ~equal:Ident.equal ids id)
10539+
;;
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: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10235,8 +10235,7 @@ let _ =
1023510235
foooooooooooooooooo
1023610236
foooooooooooooooooo
1023710237
foooooooooooooooooo
10238-
foooooooooooooooooo ->
10239-
()
10238+
foooooooooooooooooo -> ()
1024010239
;;
1024110240

1024210241
module type For_let_syntax_local =
@@ -10530,3 +10529,21 @@ let _ =
1053010529
(* foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo *)
1053110530
else f
1053210531
;;
10532+
10533+
let _ =
10534+
match ids_queue with
10535+
| Some q ->
10536+
(* this is more efficient than a linear scan of [ids] *)
10537+
fun id -> not (Ident.HashQueue.mem q id)
10538+
| None -> fun id -> not (List.mem ~equal:Ident.equal ids id)
10539+
;;
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

0 commit comments

Comments
 (0)