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