Skip to content

Commit

Permalink
Fix indentation of tuples in payloads (#2488)
Browse files Browse the repository at this point in the history
  • Loading branch information
Julow authored Dec 15, 2023
1 parent 3b3f88a commit 5f5e9f7
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 23 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ profile. This started with version 0.26.0.
- \* Fix cinaps comment formatting to not change multiline string contents (#2463, @tdelvecchio-jsc)
- Fix position of comments around function parameters (#2471, @gpetiot)
- \* Force a break around comments following an infix operator (fix non-stabilizing comments) (#2478, @gpetiot)
- \* Fix the indentation of tuples in attributes and extensions (#2488, @Julow)

## 0.26.1 (2023-09-15)

Expand Down
6 changes: 2 additions & 4 deletions lib/Fmt_ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1048,8 +1048,7 @@ and fmt_pattern ?ext c ?pro ?parens ?(box = false)
in
hvbox 0
(Params.wrap_tuple ~parens ~no_parens_if_break:false c.conf
(list pats (Params.comma_sep c.conf)
(sub_pat ~ctx >> fmt_pattern c) ) )
(List.map pats ~f:(sub_pat ~ctx >> fmt_pattern c)) )
| Ppat_construct ({txt= Lident (("()" | "[]") as txt); loc}, None) ->
let opn = txt.[0] and cls = txt.[1] in
Cmts.fmt c loc
Expand Down Expand Up @@ -2585,8 +2584,7 @@ and fmt_expression c ?(box = true) ?(pro = noop) ?eol ?parens
( hvbox 0
(Params.wrap_tuple ~parens:inner_wrap ~no_parens_if_break
c.conf
(list es (Params.comma_sep c.conf)
(sub_exp ~ctx >> fmt_expression c) ) )
(List.map es ~f:(sub_exp ~ctx >> fmt_expression c)) )
$ fmt_atrs ) )
| Pexp_lazy e ->
pro
Expand Down
14 changes: 10 additions & 4 deletions lib/Params.ml
Original file line number Diff line number Diff line change
Expand Up @@ -300,10 +300,16 @@ let wrap_collec c ~space_around opn cls =
let wrap_record (c : Conf.t) =
wrap_collec c ~space_around:c.fmt_opts.space_around_records.v "{" "}"

let wrap_tuple (c : Conf.t) ~parens ~no_parens_if_break =
if parens then wrap_fits_breaks c "(" ")"
else if no_parens_if_break then Fn.id
else wrap_k (fits_breaks "" "( ") (fits_breaks "" ~hint:(1, 0) ")")
let wrap_tuple (c : Conf.t) ~parens ~no_parens_if_break items =
let tuple_sep =
match c.fmt_opts.break_separators.v with
| `Before -> fits_breaks ", " ~hint:(1000, -2) ", "
| `After -> fmt ",@ "
in
let k = list_k items tuple_sep Fn.id in
if parens then wrap_fits_breaks c "(" ")" (hvbox 0 k)
else if no_parens_if_break then k
else fits_breaks "" "( " $ hvbox 0 k $ fits_breaks "" ~hint:(1, 0) ")"

type record_type =
{ docked_before: Fmt.t
Expand Down
3 changes: 2 additions & 1 deletion lib/Params.mli
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ val get_cases :
-> cases

val wrap_tuple :
Conf.t -> parens:bool -> no_parens_if_break:bool -> Fmt.t -> Fmt.t
Conf.t -> parens:bool -> no_parens_if_break:bool -> Fmt.t list -> Fmt.t
(** Format a tuple given a list of items. *)

type record_type =
{ docked_before: Fmt.t
Expand Down
10 changes: 5 additions & 5 deletions test/passing/tests/js_source.ml.ref
Original file line number Diff line number Diff line change
Expand Up @@ -9931,11 +9931,11 @@ let _ =
type t
[@@deriving
some_deriver_name
, another_deriver_name
, another_deriver_name
, another_deriver_name
, yet_another_such_name
, such_that_they_line_wrap]
, another_deriver_name
, another_deriver_name
, another_deriver_name
, yet_another_such_name
, such_that_they_line_wrap]

type t
[@@deriving
Expand Down
2 changes: 1 addition & 1 deletion test/passing/tests/string.ml.ref
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ let f = function
[%sexp
"Xxxx \036 \036 \036 \036 \036 \036 \036 xxx xxxx xx xxxxxx xx \
xxx xxxxxxx xxxxxx, xxxxxxx xxxxxxxxxx xx xxxx. Xxxx."
, 0]
, 0]

let _ = "\010\xFFa\o123\n\\\u{12345}aa🐪🐪🐪🐪🐪\n"

Expand Down
8 changes: 4 additions & 4 deletions test/passing/tests/tuple.ml
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ let _ = [%ext 1, 2, 3]
let _ =
[%ext
loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong
, 2
, 3]
, 2
, 3]

type t = int [@@deriving 1, 2, 3]

type t = int
[@@deriving
sexp
, compare
, loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong]
, compare
, loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong]

let _ =
( 1
Expand Down
8 changes: 4 additions & 4 deletions test/passing/tests/tuple_less_parens.ml
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ let _ = [%ext 1, 2, 3]
let _ =
[%ext
loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong
, 2
, 3]
, 2
, 3]

type t = int [@@deriving 1, 2, 3]

type t = int
[@@deriving
sexp
, compare
, loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong]
, compare
, loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong]

let _ =
( 1
Expand Down

0 comments on commit 5f5e9f7

Please sign in to comment.