Skip to content

Commit bc8bc38

Browse files
committed
Be more comprehensive
1 parent 0e99f6c commit bc8bc38

2 files changed

Lines changed: 42 additions & 5 deletions

File tree

Sources/SwiftFormat/PrettyPrint/TokenStreamCreator.swift

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1496,7 +1496,15 @@ private final class TokenStreamCreator: SyntaxVisitor {
14961496

14971497
override func visit(_ node: IfConfigDeclSyntax) -> SyntaxVisitorContinueKind {
14981498
// there has to be a break after an #endif
1499-
after(node.poundEndif, tokens: .break(.same, size: 0))
1499+
let newlines: NewlineBehavior
1500+
if node.parent?.is(AttributeListSyntax.self) == true {
1501+
newlines = .elective
1502+
} else if isNestedInPostfixIfConfig(node: Syntax(node)) {
1503+
newlines = config.respectsExistingLineBreaks ? .elective : .soft
1504+
} else {
1505+
newlines = .soft
1506+
}
1507+
after(node.poundEndif, tokens: .break(.same, size: 0, newlines: newlines))
15001508
return .visitChildren
15011509
}
15021510

@@ -4317,10 +4325,9 @@ private final class TokenStreamCreator: SyntaxVisitor {
43174325
after(postfixIfExpr.lastToken(viewMode: .sourceAccurate), tokens: .contextualBreakingEnd)
43184326

43194327
for clause in postfixIfExpr.config.clauses {
4320-
before(clause.poundKeyword, tokens: .break(.contextual, size: 0))
4328+
before(clause.poundKeyword, tokens: .break(.contextual, size: 0, newlines: .soft))
43214329
}
4322-
before(postfixIfExpr.config.poundEndif, tokens: .break(.contextual, size: 0))
4323-
after(postfixIfExpr.config.poundEndif, tokens: .break(.same, size: 0))
4330+
before(postfixIfExpr.config.poundEndif, tokens: .break(.contextual, size: 0, newlines: .soft))
43244331

43254332
return insertContextualBreaks(base, isTopLevel: false)
43264333
} else if let callingExpr = expr.asProtocol(CallingExprSyntaxProtocol.self) {

Tests/SwiftFormatTests/PrettyPrint/RespectsExistingLineBreaksTests.swift

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,14 +295,30 @@ final class RespectsExistingLineBreaksTests: PrettyPrintTestCase {
295295
)
296296
}
297297

298-
func testDeclarationAfterTrailingIfConfigAttributeKeepsRequiredLineBreak() {
298+
func testIfConfigKeepsRequiredLineBreakAfterEndif() {
299299
let input =
300300
"""
301301
#if FLAG
302302
@frozen
303303
#endif
304304
struct S {}
305305
306+
switch x {
307+
#if A
308+
case 1: f()
309+
#endif
310+
#if B
311+
case 2: g()
312+
#endif
313+
}
314+
315+
let x =
316+
base
317+
#if FLAG
318+
.foo()
319+
#endif
320+
.bar()
321+
306322
"""
307323

308324
let expected =
@@ -312,6 +328,20 @@ final class RespectsExistingLineBreaksTests: PrettyPrintTestCase {
312328
#endif
313329
struct S {}
314330
331+
switch x { #if A
332+
case 1: f()
333+
#endif
334+
#if B
335+
case 2: g()
336+
#endif
337+
}
338+
339+
let x = base
340+
#if FLAG
341+
.foo()
342+
#endif
343+
.bar()
344+
315345
"""
316346

317347
assertPrettyPrintEqual(

0 commit comments

Comments
 (0)