Skip to content

Commit 4cad9a8

Browse files
author
David Brunow
committed
Fix more postfix pound if scenarios
1 parent 475313f commit 4cad9a8

File tree

2 files changed

+94
-8
lines changed

2 files changed

+94
-8
lines changed

Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1307,7 +1307,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
13071307
before(tokenToOpenWith.nextToken(viewMode: .all), tokens: .break(breakKindClose, newlines: .soft), .close)
13081308
}
13091309

1310-
if isNestedInPostfixIfConfig(node: Syntax(node)) {
1310+
if node.parent?.parent?.parent?.is(PostfixIfConfigExprSyntax.self) == true {
13111311
before(
13121312
node.firstToken,
13131313
tokens: [
@@ -3481,7 +3481,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
34813481

34823482
if let calledMemberAccessExpr = calledExpression.as(MemberAccessExprSyntax.self) {
34833483
if calledMemberAccessExpr.base != nil {
3484-
if isNestedInPostfixIfConfig(node: Syntax(calledMemberAccessExpr)) {
3484+
if isNestedInPostfixIfConfig(node: calledMemberAccessExpr) {
34853485
before(calledMemberAccessExpr.dot, tokens: [.break(.same, size: 0)])
34863486
} else {
34873487
before(calledMemberAccessExpr.dot, tokens: [.break(.contextual, size: 0)])
@@ -3510,18 +3510,40 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
35103510
}
35113511
}
35123512

3513-
private func isNestedInPostfixIfConfig(node: Syntax) -> Bool {
3514-
var this: Syntax? = node
3513+
private func isNestedInPostfixIfConfig(node: MemberAccessExprSyntax) -> Bool {
3514+
func containsDescendent(ancestor: Syntax, node: MemberAccessExprSyntax) -> Bool {
3515+
if ancestor.children(viewMode: .sourceAccurate).contains(Syntax(node)) {
3516+
return true
3517+
}
35153518

3516-
while this?.parent != nil {
3517-
if this?.parent?.is(PostfixIfConfigExprSyntax.self) == true {
3519+
for child in ancestor.children(viewMode: .sourceAccurate) {
3520+
if containsDescendent(ancestor: child, node: node) {
35183521
return true
35193522
}
3520-
3521-
this = this?.parent
35223523
}
35233524

35243525
return false
3526+
}
3527+
3528+
var this: Syntax? = Syntax(node)
3529+
3530+
while this?.parent != nil {
3531+
if this?.is(TupleExprElementSyntax.self) == true {
3532+
return false
3533+
}
3534+
3535+
if let postfixIfConfig = this?.as(PostfixIfConfigExprSyntax.self) {
3536+
if let ifConfigListSyntax = postfixIfConfig.config.children(viewMode: .sourceAccurate).first?.as(IfConfigClauseListSyntax.self) {
3537+
if containsDescendent(ancestor: Syntax(ifConfigListSyntax), node: node) {
3538+
return true
3539+
}
3540+
}
3541+
}
3542+
3543+
this = this?.parent
3544+
}
3545+
3546+
return false
35253547
}
35263548

35273549
extension Syntax {

Tests/SwiftFormatPrettyPrintTests/IfConfigTests.swift

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,4 +390,68 @@ final class IfConfigTests: PrettyPrintTestCase {
390390

391391
assertPrettyPrintEqual(input: input, expected: expected, linelength: 45)
392392
}
393+
394+
func testPostfixPoundIfBetweenOtherModifiers() {
395+
let input =
396+
"""
397+
EmptyView()
398+
.padding([.vertical])
399+
#if os(iOS)
400+
.iOSSpecificModifier()
401+
#endif
402+
.commonModifier()
403+
"""
404+
405+
let expected =
406+
"""
407+
EmptyView()
408+
.padding([.vertical])
409+
#if os(iOS)
410+
.iOSSpecificModifier()
411+
#endif
412+
.commonModifier()
413+
414+
"""
415+
416+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 45)
417+
}
418+
419+
func testPostfixPoundIfWithTypeInModifier() {
420+
let input =
421+
"""
422+
EmptyView()
423+
.padding([.vertical])
424+
#if os(iOS)
425+
.iOSSpecificModifier(
426+
SpecificType()
427+
.onChanged { _ in
428+
// do things
429+
}
430+
.onEnded { _ in
431+
// do things
432+
}
433+
)
434+
#endif
435+
"""
436+
437+
let expected =
438+
"""
439+
EmptyView()
440+
.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
452+
453+
"""
454+
455+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 45)
456+
}
393457
}

0 commit comments

Comments
 (0)