mirror of
https://github.com/apple/pkl.git
synced 2026-03-28 11:51:58 +01:00
Fix multiline string stability (#1287)
This commit is contained in:
@@ -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()
|
||||
}
|
||||
|
||||
@@ -47,3 +47,12 @@ quux {
|
||||
|
||||
"""
|
||||
}
|
||||
|
||||
obj {
|
||||
data {
|
||||
["bar"] = """
|
||||
foo
|
||||
|
||||
"""
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,3 +51,13 @@ quux {
|
||||
|
||||
"""
|
||||
}
|
||||
|
||||
obj {
|
||||
data {
|
||||
["bar"] =
|
||||
"""
|
||||
foo
|
||||
|
||||
"""
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user