Skip to content

Commit d7b390f

Browse files
author
David Brunow
committed
Fix formatting of postfix pound ifs
1 parent 1361dd5 commit d7b390f

File tree

2 files changed

+48
-37
lines changed

2 files changed

+48
-37
lines changed

Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1308,11 +1308,22 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
13081308
}
13091309

13101310
if isNestedInPostfixIfConfig(node: Syntax(node)) {
1311+
let breakToken: Token
1312+
let previousToken = node.parent?.parent?.previousToken
1313+
1314+
if previousToken?.parent?.parent?.parent?.parent?.syntaxNodeType == IfConfigClauseSyntax.self ||
1315+
previousToken?.text == "}" {
1316+
breakToken = .break(.reset)
1317+
} else {
1318+
breakToken = .break
1319+
before(node.parent?.parent?.as(IfConfigDeclSyntax.self)?.poundEndif, tokens: [.break])
1320+
}
1321+
13111322
before(
13121323
node.firstToken,
13131324
tokens: [
13141325
.printerControl(kind: .enableBreaking),
1315-
.break(.reset),
1326+
breakToken,
13161327
]
13171328
)
13181329
} else if let condition = node.condition {

Tests/SwiftFormatPrettyPrintTests/IfConfigTests.swift

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -247,10 +247,10 @@ final class IfConfigTests: PrettyPrintTestCase {
247247
"""
248248
VStack {
249249
Text("something")
250-
#if os(iOS)
251-
.iOSSpecificModifier()
252-
#endif
253-
.commonModifier()
250+
#if os(iOS)
251+
.iOSSpecificModifier()
252+
#endif
253+
.commonModifier()
254254
}
255255
256256
"""
@@ -277,13 +277,13 @@ final class IfConfigTests: PrettyPrintTestCase {
277277
"""
278278
VStack {
279279
Text("something")
280-
#if os(iOS)
281-
.iOSSpecificModifier()
282-
.anotherModifier()
283-
.anotherAnotherModifier()
284-
#endif
285-
.commonModifier()
286-
.anotherCommonModifier()
280+
#if os(iOS)
281+
.iOSSpecificModifier()
282+
.anotherModifier()
283+
.anotherAnotherModifier()
284+
#endif
285+
.commonModifier()
286+
.anotherCommonModifier()
287287
}
288288
289289
"""
@@ -311,14 +311,14 @@ final class IfConfigTests: PrettyPrintTestCase {
311311
"""
312312
VStack {
313313
Text("something")
314-
#if os(iOS) || os(watchOS)
315-
#if os(iOS)
316-
.iOSModifier()
317-
#else
318-
.watchOSModifier()
314+
#if os(iOS) || os(watchOS)
315+
#if os(iOS)
316+
.iOSModifier()
317+
#else
318+
.watchOSModifier()
319+
#endif
320+
.iOSAndWatchOSModifier()
319321
#endif
320-
.iOSAndWatchOSModifier()
321-
#endif
322322
}
323323
324324
"""
@@ -343,10 +343,10 @@ final class IfConfigTests: PrettyPrintTestCase {
343343
"""
344344
VStack {
345345
textView
346-
#if os(iOS)
347-
.iOSSpecificModifier()
348-
#endif
349-
.commonModifier()
346+
#if os(iOS)
347+
.iOSSpecificModifier()
348+
#endif
349+
.commonModifier()
350350
}
351351
352352
"""
@@ -406,9 +406,9 @@ final class IfConfigTests: PrettyPrintTestCase {
406406
"""
407407
EmptyView()
408408
.padding([.vertical])
409-
#if os(iOS)
410-
.iOSSpecificModifier()
411-
#endif
409+
#if os(iOS)
410+
.iOSSpecificModifier()
411+
#endif
412412
.commonModifier()
413413
414414
"""
@@ -438,17 +438,17 @@ final class IfConfigTests: PrettyPrintTestCase {
438438
"""
439439
EmptyView()
440440
.padding([.vertical])
441-
#if os(iOS)
442-
.iOSSpecificModifier(
443-
SpecificType()
444-
.onChanged { _ in
445-
// do things
446-
}
447-
.onEnded { _ in
448-
// do things
449-
}
450-
)
451-
#endif
441+
#if os(iOS)
442+
.iOSSpecificModifier(
443+
SpecificType()
444+
.onChanged { _ in
445+
// do things
446+
}
447+
.onEnded { _ in
448+
// do things
449+
}
450+
)
451+
#endif
452452
453453
"""
454454

0 commit comments

Comments
 (0)