diff --git a/Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift b/Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift index 7302fb659..807214d16 100644 --- a/Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift +++ b/Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift @@ -2287,7 +2287,9 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor { // Looks up the correct break kind based on prior context. let breakKind = pendingMultilineStringBreakKinds[node, default: .same] after(node.openQuote, tokens: .break(breakKind, size: 0, newlines: .hard(count: 1))) - before(node.closeQuote, tokens: .break(breakKind, newlines: .hard(count: 1))) + if !node.segments.isEmpty { + before(node.closeQuote, tokens: .break(breakKind, newlines: .hard(count: 1))) + } } return .visitChildren } diff --git a/Tests/SwiftFormatPrettyPrintTests/StringTests.swift b/Tests/SwiftFormatPrettyPrintTests/StringTests.swift index 271a83606..c31572648 100644 --- a/Tests/SwiftFormatPrettyPrintTests/StringTests.swift +++ b/Tests/SwiftFormatPrettyPrintTests/StringTests.swift @@ -350,6 +350,7 @@ final class StringTests: PrettyPrintTestCase { assertPrettyPrintEqual(input: input, expected: expected, linelength: 20) } + func testLeadingMultilineStringsInOtherExpressions() { // The stacked indentation behavior needs to drill down into different node types to find the // leftmost multiline string literal. This makes sure that we cover various cases. @@ -417,4 +418,44 @@ final class StringTests: PrettyPrintTestCase { """# assertPrettyPrintEqual(input: input, expected: input + "\n", linelength: 100) } + + func testEmptyMultilineStrings() { + let input = + ##""" + let x = """ + """ + let y = + """ + """ + let x = #""" + """# + let y = + #""" + """# + """## + + assertPrettyPrintEqual(input: input, expected: input + "\n", linelength: 20) + } + + func testOnlyBlankLinesMultilineStrings() { + let input = + ##""" + let x = """ + + """ + let y = + """ + + """ + let x = #""" + + """# + let y = + #""" + + """# + """## + + assertPrettyPrintEqual(input: input, expected: input + "\n", linelength: 20) + } }