Skip to content

Commit 7bca483

Browse files
authored
Merge pull request #800 from TTOzzi/attributes-merged-without-space
Fix to arrange attributeList for attributes starting with ifConfig
2 parents 0c71671 + 8411c07 commit 7bca483

File tree

2 files changed

+38
-10
lines changed

2 files changed

+38
-10
lines changed

Sources/SwiftFormat/PrettyPrint/TokenStreamCreator.swift

+15-10
Original file line numberDiff line numberDiff line change
@@ -2957,19 +2957,24 @@ 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 attributes.dropLast().isEmpty,
2961+
let ifConfig = attributes.first?.as(IfConfigDeclSyntax.self) {
2962+
for clause in ifConfig.clauses {
2963+
if let nestedAttributes = AttributeListSyntax(clause.elements) {
2964+
arrangeAttributeList(nestedAttributes, suppressFinalBreak: true, separateByLineBreaks: separateByLineBreaks)
2965+
}
2966+
}
2967+
} else {
2968+
for element in attributes.dropLast() {
2969+
if let ifConfig = element.as(IfConfigDeclSyntax.self) {
29622970
for clause in ifConfig.clauses {
2963-
if let nestedAttributes = AttributeListSyntax(clause.elements) {
2964-
arrangeAttributeList(
2965-
nestedAttributes,
2966-
suppressFinalBreak: true,
2967-
separateByLineBreaks: separateByLineBreaks
2968-
)
2969-
}
2971+
if let nestedAttributes = AttributeListSyntax(clause.elements) {
2972+
arrangeAttributeList(nestedAttributes, suppressFinalBreak: true, separateByLineBreaks: separateByLineBreaks)
2973+
}
29702974
}
2971-
} else {
2975+
} else {
29722976
after(element.lastToken(viewMode: .sourceAccurate), tokens: .break(.same, newlines: behavior))
2977+
}
29732978
}
29742979
}
29752980
var afterAttributeTokens = [Token.close]

Tests/SwiftFormatTests/PrettyPrint/AttributeTests.swift

+23
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)