Fix multiline string stability (#1287)

This commit is contained in:
Islon Scherer
2025-11-03 17:41:43 +01:00
committed by GitHub
parent 40c88930c5
commit 4226c21a42
3 changed files with 30 additions and 1 deletions

View File

@@ -89,6 +89,7 @@ internal class Generator {
}
is MultilineStringGroup -> {
val indentLength = indent * INDENT.length
val oldIndent = indentFor(node)
var previousNewline = false
for ((i, child) in node.nodes.withIndex()) {
when {
@@ -96,7 +97,7 @@ internal class Generator {
child is Text &&
previousNewline &&
child.text.isBlank() &&
child.text.length == indentLength &&
child.text.length == oldIndent &&
node.nodes[i + 1] is ForceLine -> {}
child is Text && previousNewline ->
text(reposition(child.text, node.endQuoteCol - 1, indentLength))
@@ -128,6 +129,15 @@ internal class Generator {
private fun reposition(text: String, originalOffset: Int, newOffset: Int): String =
" ".repeat(newOffset) + text.drop(originalOffset)
// Returns the indent of this multiline string, which is the size of the last node before the
// closing quotes, or 0 if the closing quotes have no indentation
private fun indentFor(multi: MultilineStringGroup): Int {
val nodes = multi.nodes
if (nodes.size < 2) return 0
val beforeLast = nodes[nodes.lastIndex - 1]
return if (beforeLast is Text) beforeLast.text.length else 0
}
override fun toString(): String {
return buf.toString()
}

View File

@@ -47,3 +47,12 @@ quux {
"""
}
obj {
data {
["bar"] = """
foo
"""
}
}

View File

@@ -51,3 +51,13 @@ quux {
"""
}
obj {
data {
["bar"] =
"""
foo
"""
}
}