mirror of
https://github.com/apple/pkl.git
synced 2026-04-20 23:41:27 +02:00
Normalize mutli-line string indentation (#1271)
This commit is contained in:
@@ -89,7 +89,6 @@ internal class Generator {
|
|||||||
}
|
}
|
||||||
is MultilineStringGroup -> {
|
is MultilineStringGroup -> {
|
||||||
val indentLength = indent * INDENT.length
|
val indentLength = indent * INDENT.length
|
||||||
val offset = (indentLength + 1) - node.endQuoteCol
|
|
||||||
var previousNewline = false
|
var previousNewline = false
|
||||||
for ((i, child) in node.nodes.withIndex()) {
|
for ((i, child) in node.nodes.withIndex()) {
|
||||||
when {
|
when {
|
||||||
@@ -99,7 +98,8 @@ internal class Generator {
|
|||||||
child.text.isBlank() &&
|
child.text.isBlank() &&
|
||||||
child.text.length == indentLength &&
|
child.text.length == indentLength &&
|
||||||
node.nodes[i + 1] is ForceLine -> {}
|
node.nodes[i + 1] is ForceLine -> {}
|
||||||
child is Text && previousNewline && offset != 0 -> text(reposition(child.text, offset))
|
child is Text && previousNewline ->
|
||||||
|
text(reposition(child.text, node.endQuoteCol - 1, indentLength))
|
||||||
else -> node(child, Wrap.DETECT) // always detect wrapping
|
else -> node(child, Wrap.DETECT) // always detect wrapping
|
||||||
}
|
}
|
||||||
previousNewline = child is ForceLine
|
previousNewline = child is ForceLine
|
||||||
@@ -123,13 +123,10 @@ internal class Generator {
|
|||||||
shouldAddIndent = shouldIndent
|
shouldAddIndent = shouldIndent
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun reposition(text: String, offset: Int): String {
|
// accept text indented by originalOffset characters (tabs or spaces)
|
||||||
return if (offset > 0) {
|
// and return it indented by newOffset characters (spaces only)
|
||||||
" ".repeat(offset) + text
|
private fun reposition(text: String, originalOffset: Int, newOffset: Int): String =
|
||||||
} else {
|
" ".repeat(newOffset) + text.drop(originalOffset)
|
||||||
text.drop(-offset)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun toString(): String {
|
override fun toString(): String {
|
||||||
return buf.toString()
|
return buf.toString()
|
||||||
|
|||||||
@@ -27,3 +27,23 @@ qux = """
|
|||||||
|
|
||||||
\(foo)
|
\(foo)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
// mixed tabs and spaces
|
||||||
|
quux {
|
||||||
|
// space tab
|
||||||
|
"""
|
||||||
|
bar
|
||||||
|
|
||||||
|
baz
|
||||||
|
\(1)
|
||||||
|
|
||||||
|
"""
|
||||||
|
// tab tab
|
||||||
|
"""
|
||||||
|
bar
|
||||||
|
|
||||||
|
baz
|
||||||
|
\(1)
|
||||||
|
|
||||||
|
"""
|
||||||
|
}
|
||||||
|
|||||||
@@ -29,3 +29,23 @@ qux =
|
|||||||
|
|
||||||
\(foo)
|
\(foo)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
// mixed tabs and spaces
|
||||||
|
quux {
|
||||||
|
// space tab
|
||||||
|
"""
|
||||||
|
bar
|
||||||
|
|
||||||
|
baz
|
||||||
|
\(1)
|
||||||
|
|
||||||
|
"""
|
||||||
|
// tab tab
|
||||||
|
"""
|
||||||
|
bar
|
||||||
|
|
||||||
|
baz
|
||||||
|
\(1)
|
||||||
|
|
||||||
|
"""
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user