mirror of
https://github.com/apple/pkl.git
synced 2026-04-25 09:48:41 +02:00
Fix multiline string stability (#1287)
This commit is contained in:
@@ -89,6 +89,7 @@ internal class Generator {
|
|||||||
}
|
}
|
||||||
is MultilineStringGroup -> {
|
is MultilineStringGroup -> {
|
||||||
val indentLength = indent * INDENT.length
|
val indentLength = indent * INDENT.length
|
||||||
|
val oldIndent = indentFor(node)
|
||||||
var previousNewline = false
|
var previousNewline = false
|
||||||
for ((i, child) in node.nodes.withIndex()) {
|
for ((i, child) in node.nodes.withIndex()) {
|
||||||
when {
|
when {
|
||||||
@@ -96,7 +97,7 @@ internal class Generator {
|
|||||||
child is Text &&
|
child is Text &&
|
||||||
previousNewline &&
|
previousNewline &&
|
||||||
child.text.isBlank() &&
|
child.text.isBlank() &&
|
||||||
child.text.length == indentLength &&
|
child.text.length == oldIndent &&
|
||||||
node.nodes[i + 1] is ForceLine -> {}
|
node.nodes[i + 1] is ForceLine -> {}
|
||||||
child is Text && previousNewline ->
|
child is Text && previousNewline ->
|
||||||
text(reposition(child.text, node.endQuoteCol - 1, indentLength))
|
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 =
|
private fun reposition(text: String, originalOffset: Int, newOffset: Int): String =
|
||||||
" ".repeat(newOffset) + text.drop(originalOffset)
|
" ".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 {
|
override fun toString(): String {
|
||||||
return buf.toString()
|
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