Skip to content

Commit 85bf6cf

Browse files
committed
Merge pull request #551 from allevato/empty-multiline-string
Don't insert an extra break inside empty multiline strings.
1 parent 189b4d5 commit 85bf6cf

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
@@ -2286,7 +2286,9 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
22862286
// Looks up the correct break kind based on prior context.
22872287
let breakKind = pendingMultilineStringBreakKinds[node, default: .same]
22882288
after(node.openQuote, tokens: .break(breakKind, size: 0, newlines: .hard(count: 1)))
2289-
before(node.closeQuote, tokens: .break(breakKind, newlines: .hard(count: 1)))
2289+
if !node.segments.isEmpty {
2290+
before(node.closeQuote, tokens: .break(breakKind, newlines: .hard(count: 1)))
2291+
}
22902292
}
22912293
return .visitChildren
22922294
}

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)