mirror of
https://github.com/apple/pkl.git
synced 2026-04-22 16:28:34 +02:00
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:
@@ -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 {
|
private fun hasTrailingLambda(argList: Node): Boolean {
|
||||||
val children = argList.firstProperChild()?.children ?: return false
|
val children = argList.firstProperChild()?.children ?: return false
|
||||||
|
var seenArg = false
|
||||||
|
var ret = false
|
||||||
for (i in children.lastIndex downTo 0) {
|
for (i in children.lastIndex downTo 0) {
|
||||||
val child = children[i]
|
val child = children[i]
|
||||||
if (!child.isProper()) continue
|
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> {
|
private fun pairArguments(nodes: List<Node>): List<Node> {
|
||||||
|
|||||||
18
pkl-formatter/src/test/files/FormatterSnippetTests/input/method-call-trailing-lambdas.pkl
vendored
Normal file
18
pkl-formatter/src/test/files/FormatterSnippetTests/input/method-call-trailing-lambdas.pkl
vendored
Normal 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
|
||||||
|
)
|
||||||
|
|
||||||
19
pkl-formatter/src/test/files/FormatterSnippetTests/output/method-call-trailing-lambdas.pkl
vendored
Normal file
19
pkl-formatter/src/test/files/FormatterSnippetTests/output/method-call-trailing-lambdas.pkl
vendored
Normal 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
|
||||||
|
)
|
||||||
Reference in New Issue
Block a user