Respect line breaks in operator chains and argument lists (#1268)

If an operator chain or method call is multiline, keep those newlines
in the formatted output.

Help preserve code like:

```
foo
  |> (it) -> it + 2
  |> (it) -> it / 2
```
This commit is contained in:
Daniel Chao
2025-11-02 21:51:37 -08:00
committed by GitHub
parent 85529c9b7e
commit d8adb28dd1
6 changed files with 179 additions and 22 deletions

View File

@@ -29,3 +29,6 @@ res10 = foo.bar /* some comment */ (5)
res11 = foo.bar /* some comment */.baz(new { foooooooooooooooooooooooooo = 1; baaaaaaaaaaaaaaaaaaaaar = 2 }).buz
res12 = foo.biz
.bar((it) -> it + 2)
.qux((it) -> it + 1)

View File

@@ -0,0 +1,58 @@
// respect line breaks on:
// * operator chains
// * argument lists in method calls
//
// only look for line breaks on either side of the operator, or the comma.
res1 =
foo
* bar
* baz
res2 = foo
|> bar + bar
|> baz
res3 = foo
|> bar
+ baz
|> qux
res4 = foo(
(it) -> it + 1,
(it) -> it + 2
)
res5 = foo(
1,
2, 3, 4
)
// don't respect newlines right after opening paren and closing paren
res6 = foo.bar(
1, 2, 3
)
// only respect newlines on either side of a binary operator (we still want to inline `bar(3)` here).
res7 = foo + bar(
3
) + baz
// only the newlines after each "entry" are respected
res8 = Map(
1, 2,
3, 4
)
res9 = Map(
1,
2,
3, 4
)
res10 = foo(1
,2
,3)
res11 = foo +
bar +
baz

View File

@@ -63,3 +63,8 @@ res11 =
foo.bar /* some comment */
.baz(new { foooooooooooooooooooooooooo = 1; baaaaaaaaaaaaaaaaaaaaar = 2 })
.buz
res12 =
foo.biz
.bar((it) -> it + 2)
.qux((it) -> it + 1)

View File

@@ -0,0 +1,65 @@
// respect line breaks on:
// * operator chains
// * argument lists in method calls
//
// only look for line breaks on either side of the operator, or the comma.
res1 =
foo
* bar
* baz
res2 =
foo
|> bar + bar
|> baz
res3 =
foo
|> bar
+ baz
|> qux
res4 =
foo(
(it) -> it + 1,
(it) -> it + 2,
)
res5 =
foo(
1,
2,
3,
4,
)
// don't respect newlines right after opening paren and closing paren
res6 = foo.bar(1, 2, 3)
// only respect newlines on either side of a binary operator (we still want to inline `bar(3)` here).
res7 = foo + bar(3) + baz
// only the newlines after each "entry" are respected
res8 =
Map(
1, 2,
3, 4,
)
res9 =
Map(
1, 2,
3, 4,
)
res10 =
foo(
1,
2,
3,
)
res11 =
foo
+ bar
+ baz