Skip to content

Commit cdb8721

Browse files
fun after let-in is now consistent (#2705)
* showcase issue * fix issue * changelog
1 parent 184af25 commit cdb8721

24 files changed

+120
-22
lines changed

CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ profile. This started with version 0.26.0.
6767
keyword but `begin` `function`, and `fun` had attributes stuck to the keyword:
6868
`match[@a]`, but `fun [@a]`. Now its also `fun[@a]`. (#2676, @EmileTrotignon)
6969

70+
- \* The formatting of`let a = b in fun ...` is now consistent with other
71+
contexts like `a ; fun ...`. A check for the syntax `let a = fun ... in ...`
72+
was made more precise. (#2705, @EmileTrotignon)
73+
7074
### Changed
7175

7276
- `|> begin`, `~arg:begin`, `begin if`, `lazy begin`, `begin match`,

lib/Fmt_ast.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1664,7 +1664,7 @@ and fmt_function ?force_closing_paren ~ctx ~ctx0 ?pro ~wrap_intro
16641664
( pro_outer
16651665
$ hvbox_if has_cmts_outer 0
16661666
( cmts_outer
1667-
$ Params.Exp.box_fun_decl ~ctx0 c.conf
1667+
$ Params.Exp.box_fun_decl ~ctx0 ~ctx c.conf
16681668
( pro_inner $ cmts_inner
16691669
$ Params.Exp.box_fun_decl_after_pro ~ctx0
16701670
(opn_attr_paren $ opn_paren $ head) ) ) )

lib/Params.ml

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -329,16 +329,21 @@ module Exp = struct
329329
then Fn.id
330330
else hvbox indent
331331

332-
let box_fun_decl ~ctx0 c k =
333-
match ctx0 with
334-
| Exp {pexp_desc= Pexp_beginend _; _} -> hovbox 2 k
332+
let box_fun_decl ~ctx0 ~ctx c k =
333+
match (ctx0, ctx) with
334+
| Exp {pexp_desc= Pexp_beginend _; _}, _ -> hovbox 2 k
335335
| _ when ocp c -> hvbox 2 k
336336
(* Avoid large indentation for [let _ = function]. *)
337-
| Lb
338-
{ pvb_body= Pfunction_body {pexp_desc= Pexp_function ([], _, _, _); _}
339-
; _ } ->
337+
| ( Lb
338+
{ pvb_body=
339+
Pfunction_body {pexp_desc= Pexp_function ([], _, _, _); _}
340+
; _ }
341+
, _ ) ->
340342
hovbox 2 k
341-
| Str _ | Lb _ | Clf _ | Exp {pexp_desc= Pexp_let _; _} -> hovbox 4 k
343+
| (Str _ | Lb _ | Clf _), _ -> hovbox 4 k
344+
| Exp {pexp_desc= Pexp_let (_, e, _); _}, Exp e'
345+
when not (phys_equal e e') ->
346+
hovbox 4 k
342347
| _ -> hvbox 2 k
343348

344349
let box_fun_decl_after_pro ~ctx0 =

lib/Params.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ module Exp : sig
7878

7979
val single_line_function : ctx:Ast.t -> ctx0:Ast.t -> args:'a list -> bool
8080

81-
val box_fun_decl : ctx0:Ast.t -> Conf.t -> Fmt.t -> Fmt.t
81+
val box_fun_decl : ctx0:Ast.t -> ctx:Ast.t -> Conf.t -> Fmt.t -> Fmt.t
8282
(** Box a function decl from the label to the arrow. *)
8383

8484
val box_fun_decl_after_pro : ctx0:Ast.t -> Fmt.t -> Fmt.t

test/passing/refs.ahrefs/fun_decl-no-wrap-fun-args.ml.ref

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,14 @@ let _ =
100100
let _ = () in
101101
()
102102

103+
let _ =
104+
print_endline "foo";
105+
fun (context : Context.t)
106+
~(local_bins : origin Appendable_list.t Filename.Map.t Memo.Lazy.t)
107+
->
108+
let _ = () in
109+
()
110+
103111
class traverse_labels h =
104112
object
105113
method statement =

test/passing/refs.ahrefs/fun_decl.ml.ref

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,16 @@ let _ =
8383
let _ =
8484
let _ = () in
8585
fun (context : Context.t)
86-
~(local_bins : origin Appendable_list.t Filename.Map.t Memo.Lazy.t) ->
86+
~(local_bins : origin Appendable_list.t Filename.Map.t Memo.Lazy.t)
87+
->
88+
let _ = () in
89+
()
90+
91+
let _ =
92+
print_endline "foo";
93+
fun (context : Context.t)
94+
~(local_bins : origin Appendable_list.t Filename.Map.t Memo.Lazy.t)
95+
->
8796
let _ = () in
8897
()
8998

test/passing/refs.ahrefs/js_source.ml.err

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ Warning: js_source.ml:136 exceeds the margin
22
Warning: js_source.ml:170 exceeds the margin
33
Warning: js_source.ml:243 exceeds the margin
44
Warning: js_source.ml:255 exceeds the margin
5-
Warning: js_source.ml:857 exceeds the margin
5+
Warning: js_source.ml:862 exceeds the margin

test/passing/refs.ahrefs/js_source.ml.ref

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -587,8 +587,13 @@ let _ =
587587

588588
let _ =
589589
let x = x in
590-
fun foooooooooooooooooo foooooooooooooooooo foooooooooooooooooo
591-
foooooooooooooooooo foooooooooooooooooo foooooooooooooooooo -> ()
590+
fun foooooooooooooooooo
591+
foooooooooooooooooo
592+
foooooooooooooooooo
593+
foooooooooooooooooo
594+
foooooooooooooooooo
595+
foooooooooooooooooo
596+
-> ()
592597

593598
module type For_let_syntax_local =
594599
For_let_syntax_gen

test/passing/refs.default/fun_decl-no-wrap-fun-args.ml.ref

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,14 @@ let _ =
100100
let _ = () in
101101
()
102102

103+
let _ =
104+
print_endline "foo";
105+
fun (context : Context.t)
106+
~(local_bins : origin Appendable_list.t Filename.Map.t Memo.Lazy.t)
107+
->
108+
let _ = () in
109+
()
110+
103111
class traverse_labels h =
104112
object
105113
method statement =

test/passing/refs.default/fun_decl.ml.ref

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,16 @@ let _ =
8181
let _ =
8282
let _ = () in
8383
fun (context : Context.t)
84-
~(local_bins : origin Appendable_list.t Filename.Map.t Memo.Lazy.t) ->
84+
~(local_bins : origin Appendable_list.t Filename.Map.t Memo.Lazy.t)
85+
->
86+
let _ = () in
87+
()
88+
89+
let _ =
90+
print_endline "foo";
91+
fun (context : Context.t)
92+
~(local_bins : origin Appendable_list.t Filename.Map.t Memo.Lazy.t)
93+
->
8594
let _ = () in
8695
()
8796

0 commit comments

Comments
 (0)