Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrap short fun after a let with the janestreet profile #2593

Merged
merged 6 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 formatting of short `fun` expressions with the janestreet profile (#2593, @Julow)

### Changes
- The location of attributes for structure items is now tracked and preserved. (#2247, @EmileTrotignon)
Expand Down
2 changes: 1 addition & 1 deletion lib/Fmt_ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1509,7 +1509,7 @@ and fmt_function ?(last_arg = false) ?force_closing_paren ~ctx ~ctx0
let head = fmt_fun_args_typ args typ in
let body ~pro = pro $ fmt_expression c (sub_exp ~ctx body) in
let box, closing_paren_offset =
Params.Exp.box_fun_expr c.conf ~source:c.source ~ctx0 ~ctx ~parens
Params.Exp.box_fun_expr c.conf ~source:c.source ~ctx0 ~ctx
in
let closing_paren_offset =
if should_box then closing_paren_offset else ~-2
Expand Down
11 changes: 2 additions & 9 deletions lib/Params.ml
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ module Exp = struct
( kw_in_box
$ hvbox_if should_box_args 0 (args $ fmt_opt annot $ fmt_opt epi) )

let box_fun_expr (c : Conf.t) ~source ~ctx0 ~ctx ~parens =
let box_fun_expr (c : Conf.t) ~source ~ctx0 ~ctx =
let indent =
if ctx_is_rhs_of_infix ~ctx0 ~ctx then 0
else if Poly.equal c.fmt_opts.function_indent_nested.v `Always then
Expand Down Expand Up @@ -244,14 +244,7 @@ module Exp = struct
else 2
in
let name = "Params.box_fun_expr" in
let mkbox =
match ctx0 with
| Str _ | Lb _ -> hvbox
| _ ->
(* JS: The body of a [fun] must break if the intro is too large,
except if the [fun] is small and parenthesed. *)
if ocp c && not parens then hvbox else hovbox
in
let mkbox = if ctx_is_let_or_fun ~ctx ctx0 then hvbox else hovbox in
(mkbox ~name indent, ~-indent)

(* if the function is the last argument of an apply and no other arguments
Expand Down
1 change: 0 additions & 1 deletion lib/Params.mli
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ module Exp : sig
-> source:Source.t
-> ctx0:Ast.t
-> ctx:Ast.t
-> parens:bool
-> (Fmt.t -> Fmt.t) * int
(** return a box with an indent and minus the value of the indent to be used for a closing parenthesis *)

Expand Down
8 changes: 8 additions & 0 deletions test/passing/tests/js_source.ml
Original file line number Diff line number Diff line change
Expand Up @@ -8241,3 +8241,11 @@ let _ =
(* foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo *)
else f
;;

let _ =
match ids_queue with
| Some q ->
(* this is more efficient than a linear scan of [ids] *)
fun id -> not (Ident.HashQueue.mem q id)
| None -> fun id -> not (List.mem ~equal:Ident.equal ids id)
;;
2 changes: 1 addition & 1 deletion test/passing/tests/js_source.ml.err
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ Warning: tests/js_source.ml:9564 exceeds the margin
Warning: tests/js_source.ml:9668 exceeds the margin
Warning: tests/js_source.ml:9727 exceeds the margin
Warning: tests/js_source.ml:9810 exceeds the margin
Warning: tests/js_source.ml:10309 exceeds the margin
Warning: tests/js_source.ml:10308 exceeds the margin
11 changes: 9 additions & 2 deletions test/passing/tests/js_source.ml.ocp
Original file line number Diff line number Diff line change
Expand Up @@ -10235,8 +10235,7 @@ let _ =
foooooooooooooooooo
foooooooooooooooooo
foooooooooooooooooo
foooooooooooooooooo ->
()
foooooooooooooooooo -> ()
;;

module type For_let_syntax_local =
Expand Down Expand Up @@ -10530,3 +10529,11 @@ let _ =
(* foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo *)
else f
;;

let _ =
match ids_queue with
| Some q ->
(* this is more efficient than a linear scan of [ids] *)
fun id -> not (Ident.HashQueue.mem q id)
| None -> fun id -> not (List.mem ~equal:Ident.equal ids id)
;;
11 changes: 9 additions & 2 deletions test/passing/tests/js_source.ml.ref
Original file line number Diff line number Diff line change
Expand Up @@ -10235,8 +10235,7 @@ let _ =
foooooooooooooooooo
foooooooooooooooooo
foooooooooooooooooo
foooooooooooooooooo ->
()
foooooooooooooooooo -> ()
;;

module type For_let_syntax_local =
Expand Down Expand Up @@ -10530,3 +10529,11 @@ let _ =
(* foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo *)
else f
;;

let _ =
match ids_queue with
| Some q ->
(* this is more efficient than a linear scan of [ids] *)
fun id -> not (Ident.HashQueue.mem q id)
| None -> fun id -> not (List.mem ~equal:Ident.equal ids id)
;;
Loading