Skip to content

Commit 5030061

Browse files
Fix ordering issue in formatter (#1289)
1 parent 219e766 commit 5030061

File tree

2 files changed

+13
-7
lines changed
  • docs/modules/release-notes/pages
  • pkl-formatter/src/main/kotlin/org/pkl/formatter

2 files changed

+13
-7
lines changed

docs/modules/release-notes/pages/0.30.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ To get started, follow xref:pkl-cli:index.adoc#installation[Installation].#
2525
[[formatter]]
2626
=== Formatter
2727

28-
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])!
28+
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])!
2929

3030
Pkl's formatter is _canonical_, which means that it has a single set of formatting rules, with (almost) no configuration options.
3131
The goal is to eliminate the possibility of formatting debates, which can lead to churn and bike-shedding.

pkl-formatter/src/main/kotlin/org/pkl/formatter/Builder.kt

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,10 @@ import org.pkl.parser.syntax.generic.NodeType
3636
internal class Builder(sourceText: String, private val grammarVersion: GrammarVersion) {
3737
private var id: Int = 0
3838
private val source: CharArray = sourceText.toCharArray()
39-
private var prevNode: Node? = null
4039
private var noNewlines = false
4140

42-
fun format(node: Node): FormatNode {
43-
prevNode = node
44-
return when (node.type) {
41+
fun format(node: Node): FormatNode =
42+
when (node.type) {
4543
NodeType.MODULE -> formatModule(node)
4644
NodeType.DOC_COMMENT -> Nodes(formatGeneric(node.children, null))
4745
NodeType.DOC_COMMENT_LINE -> formatDocComment(node)
@@ -164,7 +162,6 @@ internal class Builder(sourceText: String, private val grammarVersion: GrammarVe
164162
NodeType.PARENTHESIZED_TYPE_ELEMENTS -> formatParenthesizedTypeElements(node)
165163
else -> throw RuntimeException("Unknown node type: ${node.type}")
166164
}
167-
}
168165

169166
private fun formatModule(node: Node): FormatNode {
170167
val nodes =
@@ -617,6 +614,7 @@ internal class Builder(sourceText: String, private val grammarVersion: GrammarVe
617614
val trailingNode = if (endsWithClosingCurlyBrace(lastParam.last())) Empty else line()
618615
val lastNodes = formatGenericWithGen(lastParam, sep, null)
619616
if (normalParams.isEmpty()) {
617+
val lastNodes = formatGenericWithGen(lastParam, sep, null)
620618
group(Group(newId(), lastNodes), trailingNode)
621619
} else {
622620
val separator = getSeparator(normalParams.last(), lastParam[0], Space)
@@ -1289,7 +1287,7 @@ internal class Builder(sourceText: String, private val grammarVersion: GrammarVe
12891287

12901288
private fun getBaseSeparator(prev: Node, next: Node): FormatNode? {
12911289
return when {
1292-
prevNode?.type == NodeType.LINE_COMMENT -> {
1290+
endsInLineComment(prev) -> {
12931291
if (prev.linesBetween(next) > 1) {
12941292
TWO_NEWLINES
12951293
} else {
@@ -1325,6 +1323,14 @@ internal class Builder(sourceText: String, private val grammarVersion: GrammarVe
13251323
}
13261324
}
13271325

1326+
private tailrec fun endsInLineComment(node: Node): Boolean {
1327+
return when {
1328+
node.type == NodeType.LINE_COMMENT -> true
1329+
node.children.isEmpty() -> false
1330+
else -> endsInLineComment(node.children.last())
1331+
}
1332+
}
1333+
13281334
private fun line(): FormatNode {
13291335
return if (noNewlines) Empty else Line
13301336
}

0 commit comments

Comments
 (0)