mirror of
https://github.com/apple/pkl.git
synced 2026-04-20 23:41:27 +02:00
Format lambda chains (#1235)
This changes the formatter to only force line on call chains with multiple lambdas With this change, this is kept as a single line: ``` foo.bar.map((it) -> it + 1) ```
This commit is contained in:
@@ -803,7 +803,7 @@ internal class Builder(sourceText: String) {
|
|||||||
private fun formatBinaryOpExpr(node: Node): FormatNode {
|
private fun formatBinaryOpExpr(node: Node): FormatNode {
|
||||||
val flat = flattenBinaryOperatorExprs(node)
|
val flat = flattenBinaryOperatorExprs(node)
|
||||||
val callChainSize = flat.count { it.isOperator(".", "?.") }
|
val callChainSize = flat.count { it.isOperator(".", "?.") }
|
||||||
val hasLambda = callChainSize > 1 && flat.any { hasFunctionLiteral(it, 2) }
|
val hasMultipleLambdas = callChainSize > 1 && flat.hasMoreThan(1) { hasFunctionLiteral(it, 2) }
|
||||||
val nodes =
|
val nodes =
|
||||||
formatGeneric(flat) { prev, next ->
|
formatGeneric(flat) { prev, next ->
|
||||||
if (prev.type == NodeType.OPERATOR) {
|
if (prev.type == NodeType.OPERATOR) {
|
||||||
@@ -816,7 +816,7 @@ internal class Builder(sourceText: String) {
|
|||||||
} else if (next.type == NodeType.OPERATOR) {
|
} else if (next.type == NodeType.OPERATOR) {
|
||||||
when (next.text()) {
|
when (next.text()) {
|
||||||
".",
|
".",
|
||||||
"?." -> if (hasLambda) ForceLine else Line
|
"?." -> if (hasMultipleLambdas) ForceLine else Line
|
||||||
"-" -> Space
|
"-" -> Space
|
||||||
else -> SpaceOrLine
|
else -> SpaceOrLine
|
||||||
}
|
}
|
||||||
@@ -1231,6 +1231,18 @@ internal class Builder(sourceText: String) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private inline fun <T> Iterable<T>.hasMoreThan(num: Int, predicate: (T) -> Boolean): Boolean {
|
||||||
|
if (this is Collection && isEmpty()) return false
|
||||||
|
var count = 0
|
||||||
|
for (element in this) {
|
||||||
|
if (predicate(element)) count++
|
||||||
|
if (count > num) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
private val ABSOLUTE_URL_REGEX = Regex("""\w+:.*""")
|
private val ABSOLUTE_URL_REGEX = Regex("""\w+:.*""")
|
||||||
|
|
||||||
|
|||||||
@@ -3,3 +3,5 @@ res = myList.map((it) -> it.partition).filter((it) -> someList.contains(it))
|
|||||||
res2 = myList.map(lambda1).filter(lambda2)
|
res2 = myList.map(lambda1).filter(lambda2)
|
||||||
|
|
||||||
res3 = myList.map((it) -> it.partition)
|
res3 = myList.map((it) -> it.partition)
|
||||||
|
|
||||||
|
res4 = myList.foo.map((it) -> it.partition)
|
||||||
|
|||||||
@@ -6,3 +6,5 @@ res =
|
|||||||
res2 = myList.map(lambda1).filter(lambda2)
|
res2 = myList.map(lambda1).filter(lambda2)
|
||||||
|
|
||||||
res3 = myList.map((it) -> it.partition)
|
res3 = myList.map((it) -> it.partition)
|
||||||
|
|
||||||
|
res4 = myList.foo.map((it) -> it.partition)
|
||||||
|
|||||||
Reference in New Issue
Block a user