Normalize mutli-line string indentation (#1271)

This commit is contained in:
Jen Basch
2025-10-30 08:30:30 -07:00
committed by GitHub
parent 9469dd885d
commit 08c414f3ac
3 changed files with 46 additions and 9 deletions

View File

@@ -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()

View File

@@ -27,3 +27,23 @@ qux = """
\(foo)
"""
// mixed tabs and spaces
quux {
// space tab
"""
bar
baz
\(1)
"""
// tab tab
"""
bar
baz
\(1)
"""
}

View File

@@ -29,3 +29,23 @@ qux =
\(foo)
"""
// mixed tabs and spaces
quux {
// space tab
"""
bar
baz
\(1)
"""
// tab tab
"""
bar
baz
\(1)
"""
}