Adjust formatting of argument lists (#1260)

This changes code so that multiple lambda arguments makes the whole
argument list wrap.

Improves the readability of code like:

```
foo
  .toMap(
    (it) -> makeSomeKey(it),
    (it) -> makeSomeValue(it)
  )
```
This commit is contained in:
Daniel Chao
2025-10-28 10:46:19 -07:00
committed by GitHub
parent be0142d46b
commit fbcf83aa76
3 changed files with 59 additions and 2 deletions

View File

@@ -625,14 +625,34 @@ internal class Builder(sourceText: String, private val grammarVersion: GrammarVe
}
}
/**
* Only considered trailing lamdba if there is only one lambda/new expr/amends expr in the list.
*
* E.g. avoid formatting `toMap()` weirdly:
* ```
* foo.toMap(
* (it) -> makeSomeKey(it),
* (it) -> makeSomeValue(it),
* )
* ```
*/
private fun hasTrailingLambda(argList: Node): Boolean {
val children = argList.firstProperChild()?.children ?: return false
var seenArg = false
var ret = false
for (i in children.lastIndex downTo 0) {
val child = children[i]
if (!child.isProper()) continue
return child.type in SAME_LINE_EXPRS
if (child.type in SAME_LINE_EXPRS) {
if (seenArg) {
return false
} else {
seenArg = true
ret = true
}
}
}
return false
return ret
}
private fun pairArguments(nodes: List<Node>): List<Node> {

View File

@@ -0,0 +1,18 @@
// multiple lambda arguments means argument list wraps
res1 = foo.toMap(
(elem) -> elem.toString(),
(elem) -> elem.toString().split("").map((str) -> str + "hello").toSet()
)
// new object and trailing lambda means argument list wraps
res2 = foo.doFoo(
new Listing { 1; 2; 3 },
(elem) -> elem.toString().split("").map((str) -> str + "hello").toSet()
)
// single trailing lambda is broken on its own
res4 = foo.foldLeft(
"heeeeeeelloooooooooooooooooooooooooooooooooooooooooooooo",
(elem) -> elem.foooooooooooooooooooooooooooooooooooooooooooooooooooo
)

View File

@@ -0,0 +1,19 @@
// multiple lambda arguments means argument list wraps
res1 =
foo.toMap(
(elem) -> elem.toString(),
(elem) -> elem.toString().split("").map((str) -> str + "hello").toSet(),
)
// new object and trailing lambda means argument list wraps
res2 =
foo.doFoo(
new Listing { 1; 2; 3 },
(elem) -> elem.toString().split("").map((str) -> str + "hello").toSet(),
)
// single trailing lambda is broken on its own
res4 =
foo.foldLeft("heeeeeeelloooooooooooooooooooooooooooooooooooooooooooooo", (elem) ->
elem.foooooooooooooooooooooooooooooooooooooooooooooooooooo
)