Fix ordering issue in formatter (#1289)

This commit is contained in:
Islon Scherer
2025-11-03 18:52:38 +01:00
committed by GitHub
parent 219e766003
commit 5030061412
2 changed files with 13 additions and 7 deletions

View File

@@ -25,7 +25,7 @@ To get started, follow xref:pkl-cli:index.adoc#installation[Installation].#
[[formatter]]
=== Formatter
Pkl now has a formatter (https://github.com/apple/pkl/pull/1107[#1107], https://github.com/apple/pkl/pull/1208[#1208], https://github.com/apple/pkl/pull/1211[#1211], https://github.com/apple/pkl/pull/1215[#1215], https://github.com/apple/pkl/pull/1217[#1217], https://github.com/apple/pkl/pull/1235[#1235], https://github.com/apple/pkl/pull/1246[#1246], https://github.com/apple/pkl/pull/1247[#1247], https://github.com/apple/pkl/pull/1252[#1252], https://github.com/apple/pkl/pull/1256[#1256], https://github.com/apple/pkl/pull/1259[#1259], https://github.com/apple/pkl/pull/1260[#1260], https://github.com/apple/pkl/pull/1263[#1263], https://github.com/apple/pkl/pull/1265[#1265], https://github.com/apple/pkl/pull/1266[#1266], https://github.com/apple/pkl/pull/1267[#1267], https://github.com/apple/pkl/pull/1268[#1268], https://github.com/apple/pkl/pull/1270[#1270], https://github.com/apple/pkl/pull/1271[#1271], https://github.com/apple/pkl/pull/1272[#1272], https://github.com/apple/pkl/pull/1273[#1273], https://github.com/apple/pkl/pull/1274[#1274], https://github.com/apple/pkl/pull/1280[#1280])!
Pkl now has a formatter (https://github.com/apple/pkl/pull/1107[#1107], https://github.com/apple/pkl/pull/1208[#1208], https://github.com/apple/pkl/pull/1211[#1211], https://github.com/apple/pkl/pull/1215[#1215], https://github.com/apple/pkl/pull/1217[#1217], https://github.com/apple/pkl/pull/1235[#1235], https://github.com/apple/pkl/pull/1246[#1246], https://github.com/apple/pkl/pull/1247[#1247], https://github.com/apple/pkl/pull/1252[#1252], https://github.com/apple/pkl/pull/1256[#1256], https://github.com/apple/pkl/pull/1259[#1259], https://github.com/apple/pkl/pull/1260[#1260], https://github.com/apple/pkl/pull/1263[#1263], https://github.com/apple/pkl/pull/1265[#1265], https://github.com/apple/pkl/pull/1266[#1266], https://github.com/apple/pkl/pull/1267[#1267], https://github.com/apple/pkl/pull/1268[#1268], https://github.com/apple/pkl/pull/1270[#1270], https://github.com/apple/pkl/pull/1271[#1271], https://github.com/apple/pkl/pull/1272[#1272], https://github.com/apple/pkl/pull/1273[#1273], https://github.com/apple/pkl/pull/1274[#1274], https://github.com/apple/pkl/pull/1280[#1280], https://github.com/apple/pkl/pull/1283[#1283], https://github.com/apple/pkl/pull/1289[#1289], https://github.com/apple/pkl/pull/1290[#1290])!
Pkl's formatter is _canonical_, which means that it has a single set of formatting rules, with (almost) no configuration options.
The goal is to eliminate the possibility of formatting debates, which can lead to churn and bike-shedding.

View File

@@ -36,12 +36,10 @@ import org.pkl.parser.syntax.generic.NodeType
internal class Builder(sourceText: String, private val grammarVersion: GrammarVersion) {
private var id: Int = 0
private val source: CharArray = sourceText.toCharArray()
private var prevNode: Node? = null
private var noNewlines = false
fun format(node: Node): FormatNode {
prevNode = node
return when (node.type) {
fun format(node: Node): FormatNode =
when (node.type) {
NodeType.MODULE -> formatModule(node)
NodeType.DOC_COMMENT -> Nodes(formatGeneric(node.children, null))
NodeType.DOC_COMMENT_LINE -> formatDocComment(node)
@@ -164,7 +162,6 @@ internal class Builder(sourceText: String, private val grammarVersion: GrammarVe
NodeType.PARENTHESIZED_TYPE_ELEMENTS -> formatParenthesizedTypeElements(node)
else -> throw RuntimeException("Unknown node type: ${node.type}")
}
}
private fun formatModule(node: Node): FormatNode {
val nodes =
@@ -617,6 +614,7 @@ internal class Builder(sourceText: String, private val grammarVersion: GrammarVe
val trailingNode = if (endsWithClosingCurlyBrace(lastParam.last())) Empty else line()
val lastNodes = formatGenericWithGen(lastParam, sep, null)
if (normalParams.isEmpty()) {
val lastNodes = formatGenericWithGen(lastParam, sep, null)
group(Group(newId(), lastNodes), trailingNode)
} else {
val separator = getSeparator(normalParams.last(), lastParam[0], Space)
@@ -1289,7 +1287,7 @@ internal class Builder(sourceText: String, private val grammarVersion: GrammarVe
private fun getBaseSeparator(prev: Node, next: Node): FormatNode? {
return when {
prevNode?.type == NodeType.LINE_COMMENT -> {
endsInLineComment(prev) -> {
if (prev.linesBetween(next) > 1) {
TWO_NEWLINES
} else {
@@ -1325,6 +1323,14 @@ internal class Builder(sourceText: String, private val grammarVersion: GrammarVe
}
}
private tailrec fun endsInLineComment(node: Node): Boolean {
return when {
node.type == NodeType.LINE_COMMENT -> true
node.children.isEmpty() -> false
else -> endsInLineComment(node.children.last())
}
}
private fun line(): FormatNode {
return if (noNewlines) Empty else Line
}