Skip to content

Commit 949b0d0

Browse files
allevatoahoppen
authored andcommitted
Fix multi-line string wrapping in @available attributes.
1 parent a061657 commit 949b0d0

File tree

2 files changed

+81
-2
lines changed

2 files changed

+81
-2
lines changed

Sources/SwiftFormat/PrettyPrint/TokenStreamCreator.swift

+27-2
Original file line numberDiff line numberDiff line change
@@ -1799,8 +1799,23 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
17991799

18001800
override func visit(_ node: AvailabilityLabeledArgumentSyntax) -> SyntaxVisitorContinueKind {
18011801
before(node.label, tokens: .open)
1802-
after(node.colon, tokens: .break(.continue, newlines: .elective(ignoresDiscretionary: true)))
1803-
after(node.value.lastToken(viewMode: .sourceAccurate), tokens: .close)
1802+
1803+
let tokensAfterColon: [Token]
1804+
let endTokens: [Token]
1805+
1806+
if case .string(let string) = node.value,
1807+
string.openingQuote.tokenKind == .multilineStringQuote
1808+
{
1809+
tokensAfterColon =
1810+
[.break(.open(kind: .block), newlines: .elective(ignoresDiscretionary: true))]
1811+
endTokens = [.break(.close(mustBreak: false), size: 0), .close]
1812+
} else {
1813+
tokensAfterColon = [.break(.continue, newlines: .elective(ignoresDiscretionary: true))]
1814+
endTokens = [.close]
1815+
}
1816+
1817+
after(node.colon, tokens: tokensAfterColon)
1818+
after(node.value.lastToken(viewMode: .sourceAccurate), tokens: endTokens)
18041819
return .visitChildren
18051820
}
18061821

@@ -2376,6 +2391,16 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
23762391
return .visitChildren
23772392
}
23782393

2394+
override func visit(_ node: SimpleStringLiteralExprSyntax) -> SyntaxVisitorContinueKind {
2395+
if node.openingQuote.tokenKind == .multilineStringQuote {
2396+
after(node.openingQuote, tokens: .break(.same, size: 0, newlines: .hard(count: 1)))
2397+
if !node.segments.isEmpty {
2398+
before(node.closingQuote, tokens: .break(.same, newlines: .hard(count: 1)))
2399+
}
2400+
}
2401+
return .visitChildren
2402+
}
2403+
23792404
override func visit(_ node: StringSegmentSyntax) -> SyntaxVisitorContinueKind {
23802405
// Looks up the correct break kind based on prior context.
23812406
func breakKind() -> BreakKind {

Tests/SwiftFormatTests/PrettyPrint/AttributeTests.swift

+54
Original file line numberDiff line numberDiff line change
@@ -356,4 +356,58 @@ final class AttributeTests: PrettyPrintTestCase {
356356

357357
assertPrettyPrintEqual(input: input, expected: expected, linelength: 32)
358358
}
359+
360+
func testMultilineStringLiteralInCustomAttribute() {
361+
let input =
362+
#"""
363+
@CustomAttribute(message: """
364+
This is a
365+
multiline
366+
string
367+
""")
368+
public func f() {}
369+
"""#
370+
371+
let expected =
372+
#"""
373+
@CustomAttribute(
374+
message: """
375+
This is a
376+
multiline
377+
string
378+
""")
379+
public func f() {}
380+
381+
"""#
382+
383+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 100)
384+
}
385+
386+
func testMultilineStringLiteralInAvailableAttribute() {
387+
let input =
388+
#"""
389+
@available(*, deprecated, message: """
390+
This is a
391+
multiline
392+
string
393+
""")
394+
public func f() {}
395+
"""#
396+
397+
let expected =
398+
#"""
399+
@available(
400+
*, deprecated,
401+
message: """
402+
This is a
403+
multiline
404+
string
405+
"""
406+
)
407+
public func f() {}
408+
409+
"""#
410+
411+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 100)
412+
}
359413
}

0 commit comments

Comments
 (0)