Skip to content

Commit f58791f

Browse files
authored
Merge pull request #551 from allevato/empty-multiline-string
Don't insert an extra break inside empty multiline strings.
2 parents e5e2fad + 4ddd702 commit f58791f

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2303,7 +2303,9 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
23032303
// Looks up the correct break kind based on prior context.
23042304
let breakKind = pendingMultilineStringBreakKinds[node, default: .same]
23052305
after(node.openQuote, tokens: .break(breakKind, size: 0, newlines: .hard(count: 1)))
2306-
before(node.closeQuote, tokens: .break(breakKind, newlines: .hard(count: 1)))
2306+
if !node.segments.isEmpty {
2307+
before(node.closeQuote, tokens: .break(breakKind, newlines: .hard(count: 1)))
2308+
}
23072309
}
23082310
return .visitChildren
23092311
}

Tests/SwiftFormatPrettyPrintTests/StringTests.swift

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,7 @@ final class StringTests: PrettyPrintTestCase {
350350

351351
assertPrettyPrintEqual(input: input, expected: expected, linelength: 20)
352352
}
353+
353354
func testLeadingMultilineStringsInOtherExpressions() {
354355
// The stacked indentation behavior needs to drill down into different node types to find the
355356
// leftmost multiline string literal. This makes sure that we cover various cases.
@@ -417,4 +418,44 @@ final class StringTests: PrettyPrintTestCase {
417418
"""#
418419
assertPrettyPrintEqual(input: input, expected: input + "\n", linelength: 100)
419420
}
421+
422+
func testEmptyMultilineStrings() {
423+
let input =
424+
##"""
425+
let x = """
426+
"""
427+
let y =
428+
"""
429+
"""
430+
let x = #"""
431+
"""#
432+
let y =
433+
#"""
434+
"""#
435+
"""##
436+
437+
assertPrettyPrintEqual(input: input, expected: input + "\n", linelength: 20)
438+
}
439+
440+
func testOnlyBlankLinesMultilineStrings() {
441+
let input =
442+
##"""
443+
let x = """
444+
445+
"""
446+
let y =
447+
"""
448+
449+
"""
450+
let x = #"""
451+
452+
"""#
453+
let y =
454+
#"""
455+
456+
"""#
457+
"""##
458+
459+
assertPrettyPrintEqual(input: input, expected: input + "\n", linelength: 20)
460+
}
420461
}

0 commit comments

Comments
 (0)