Skip to content

Commit 9cd93de

Browse files
committed
Fix to arrange attributeList for attributes starting with ifConfig
1 parent 0c71671 commit 9cd93de

File tree

2 files changed

+37
-10
lines changed

2 files changed

+37
-10
lines changed

Sources/SwiftFormat/PrettyPrint/TokenStreamCreator.swift

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2957,19 +2957,23 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
29572957
if let attributes = attributes {
29582958
let behavior: NewlineBehavior = separateByLineBreaks ? .hard : .elective
29592959
before(attributes.firstToken(viewMode: .sourceAccurate), tokens: .open)
2960-
for element in attributes.dropLast() {
2961-
if let ifConfig = element.as(IfConfigDeclSyntax.self) {
2960+
if let ifConfig = attributes.first?.as(IfConfigDeclSyntax.self) {
2961+
for clause in ifConfig.clauses {
2962+
if let nestedAttributes = AttributeListSyntax(clause.elements) {
2963+
arrangeAttributeList(nestedAttributes, suppressFinalBreak: true, separateByLineBreaks: separateByLineBreaks)
2964+
}
2965+
}
2966+
} else {
2967+
for element in attributes.dropLast() {
2968+
if let ifConfig = element.as(IfConfigDeclSyntax.self) {
29622969
for clause in ifConfig.clauses {
2963-
if let nestedAttributes = AttributeListSyntax(clause.elements) {
2964-
arrangeAttributeList(
2965-
nestedAttributes,
2966-
suppressFinalBreak: true,
2967-
separateByLineBreaks: separateByLineBreaks
2968-
)
2969-
}
2970+
if let nestedAttributes = AttributeListSyntax(clause.elements) {
2971+
arrangeAttributeList(nestedAttributes, suppressFinalBreak: true, separateByLineBreaks: separateByLineBreaks)
2972+
}
29702973
}
2971-
} else {
2974+
} else {
29722975
after(element.lastToken(viewMode: .sourceAccurate), tokens: .break(.same, newlines: behavior))
2976+
}
29732977
}
29742978
}
29752979
var afterAttributeTokens = [Token.close]

Tests/SwiftFormatTests/PrettyPrint/AttributeTests.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,4 +576,27 @@ final class AttributeTests: PrettyPrintTestCase {
576576
configuration.lineBreakBetweenDeclarationAttributes = true
577577
assertPrettyPrintEqual(input: input, expected: expected, linelength: 80, configuration: configuration)
578578
}
579+
580+
func testAttributesStartWithPoundIf() {
581+
let input =
582+
"""
583+
#if os(macOS)
584+
@available(macOS, unavailable)
585+
@_spi(Foo)
586+
#endif
587+
public let myVar = "Test"
588+
589+
"""
590+
let expected =
591+
"""
592+
#if os(macOS)
593+
@available(macOS, unavailable)
594+
@_spi(Foo)
595+
#endif
596+
public let myVar = "Test"
597+
598+
"""
599+
600+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 45)
601+
}
579602
}

0 commit comments

Comments
 (0)