Skip to content

Fix multi-line string wrapping in @available attributes. #656

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions Sources/SwiftFormat/PrettyPrint/TokenStreamCreator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1801,8 +1801,23 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {

override func visit(_ node: AvailabilityLabeledArgumentSyntax) -> SyntaxVisitorContinueKind {
before(node.label, tokens: .open)
after(node.colon, tokens: .break(.continue, newlines: .elective(ignoresDiscretionary: true)))
after(node.value.lastToken(viewMode: .sourceAccurate), tokens: .close)

let tokensAfterColon: [Token]
let endTokens: [Token]

if case .string(let string) = node.value,
string.openingQuote.tokenKind == .multilineStringQuote
{
tokensAfterColon =
[.break(.open(kind: .block), newlines: .elective(ignoresDiscretionary: true))]
endTokens = [.break(.close(mustBreak: false), size: 0), .close]
} else {
tokensAfterColon = [.break(.continue, newlines: .elective(ignoresDiscretionary: true))]
endTokens = [.close]
}

after(node.colon, tokens: tokensAfterColon)
after(node.value.lastToken(viewMode: .sourceAccurate), tokens: endTokens)
return .visitChildren
}

Expand Down Expand Up @@ -2382,6 +2397,16 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
return .visitChildren
}

override func visit(_ node: SimpleStringLiteralExprSyntax) -> SyntaxVisitorContinueKind {
if node.openingQuote.tokenKind == .multilineStringQuote {
after(node.openingQuote, tokens: .break(.same, size: 0, newlines: .hard(count: 1)))
if !node.segments.isEmpty {
before(node.closingQuote, tokens: .break(.same, newlines: .hard(count: 1)))
}
}
return .visitChildren
}

override func visit(_ node: StringSegmentSyntax) -> SyntaxVisitorContinueKind {
// Looks up the correct break kind based on prior context.
func breakKind() -> BreakKind {
Expand Down
54 changes: 54 additions & 0 deletions Tests/SwiftFormatTests/PrettyPrint/AttributeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -356,4 +356,58 @@ final class AttributeTests: PrettyPrintTestCase {

assertPrettyPrintEqual(input: input, expected: expected, linelength: 32)
}

func testMultilineStringLiteralInCustomAttribute() {
let input =
#"""
@CustomAttribute(message: """
This is a
multiline
string
""")
public func f() {}
"""#

let expected =
#"""
@CustomAttribute(
message: """
This is a
multiline
string
""")
public func f() {}

"""#

assertPrettyPrintEqual(input: input, expected: expected, linelength: 100)
}

func testMultilineStringLiteralInAvailableAttribute() {
let input =
#"""
@available(*, deprecated, message: """
This is a
multiline
string
""")
public func f() {}
"""#

let expected =
#"""
@available(
*, deprecated,
message: """
This is a
multiline
string
"""
)
public func f() {}

"""#

assertPrettyPrintEqual(input: input, expected: expected, linelength: 100)
}
}