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

View File

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

View File

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