Skip to content

Commit f07b27d

Browse files
committed
Support "// swift-format-ignore" for multi-line strings.
1 parent 7e315b3 commit f07b27d

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

Sources/SwiftFormat/PrettyPrint/TokenStreamCreator.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2446,6 +2446,13 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
24462446
if !node.segments.isEmpty {
24472447
before(node.closingQuote, tokens: .break(breakKind, newlines: .hard(count: 1)))
24482448
}
2449+
if shouldFormatterIgnore(node: Syntax(node)) {
2450+
appendFormatterIgnored(node: Syntax(node))
2451+
// Mirror the tokens we'd normally append on '"""'
2452+
appendTrailingTrivia(node.closingQuote)
2453+
appendAfterTokensAndTrailingComments(node.closingQuote)
2454+
return .skipChildren
2455+
}
24492456
}
24502457
return .visitChildren
24512458
}

Tests/SwiftFormatTests/PrettyPrint/StringTests.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,33 @@ final class StringTests: PrettyPrintTestCase {
4848
linelength: 30)
4949
}
5050

51+
func testMultilineStringIsNotReformattedWithIgnore() {
52+
let input =
53+
#"""
54+
let someString =
55+
// swift-format-ignore
56+
"""
57+
lines \
58+
are \
59+
short.
60+
"""
61+
"""#
62+
63+
let expected =
64+
#"""
65+
let someString =
66+
// swift-format-ignore
67+
"""
68+
lines \
69+
are \
70+
short.
71+
"""
72+
73+
"""#
74+
75+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 30)
76+
}
77+
5178
func testMultilineStringWithInterpolations() {
5279
let input =
5380
#"""

0 commit comments

Comments
 (0)