Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix formatting issue with IfConfigDecl/ImportDecl interaction. #860

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions Sources/SwiftFormat/PrettyPrint/TokenStreamCreator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1492,7 +1492,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {

override func visit(_ node: IfConfigDeclSyntax) -> SyntaxVisitorContinueKind {
// there has to be a break after an #endif
after(node.poundEndif, tokens: .break(.same, size: 0))
after(node.poundEndif, tokens: .break(.same, size: 0, newlines: .soft))
return .visitChildren
}

Expand Down Expand Up @@ -1951,17 +1951,28 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
}

override func visit(_ node: ImportDeclSyntax) -> SyntaxVisitorContinueKind {
// Import declarations should never be wrapped.
before(
node.firstToken(viewMode: .sourceAccurate),
tokens: .printerControl(kind: .disableBreaking(allowDiscretionary: false))
)
// Import declarations ignore wrapping when it is safe to do so (no #if constructs)
let isSafeToIgnoreBreaking = !node.attributes.contains(where: { element in
return element.is(IfConfigDeclSyntax.self)
})

if isSafeToIgnoreBreaking {
before(
node.firstToken(viewMode: .sourceAccurate),
tokens: .printerControl(kind: .disableBreaking(allowDiscretionary: false))
)
}

arrangeAttributeList(node.attributes)
after(node.importKeyword, tokens: .space)
after(node.importKindSpecifier, tokens: .space)

after(node.lastToken(viewMode: .sourceAccurate), tokens: .printerControl(kind: .enableBreaking))
if isSafeToIgnoreBreaking {
after(
node.lastToken(viewMode: .sourceAccurate),
tokens: .printerControl(kind: .enableBreaking)
)
}
return .visitChildren
}

Expand Down
32 changes: 26 additions & 6 deletions Tests/SwiftFormatTests/PrettyPrint/IfConfigTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -383,10 +383,10 @@ final class IfConfigTests: PrettyPrintTestCase {
.toggleStyle(
SwitchToggleStyle(tint: Color.blue))
#endif
.accessibilityValue(
binding.wrappedValue == true
? "On" : "Off"
)
.accessibilityValue(
binding.wrappedValue == true
? "On" : "Off"
)
}

"""
Expand Down Expand Up @@ -482,7 +482,7 @@ final class IfConfigTests: PrettyPrintTestCase {
#if os(iOS)
.iOSSpecificModifier()
#endif
.commonModifier()
.commonModifier()

"""

Expand Down Expand Up @@ -510,7 +510,7 @@ final class IfConfigTests: PrettyPrintTestCase {
#if os(iOS)
.iOSSpecificModifier()
#endif
.commonModifier()
.commonModifier()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can these changes be avoided? The idea is that the indentation of these would be the same if there weren't an intervening #if-config; i.e., we wouldn't have this:

SomeFunction(
  foo,
  bar
)
  .commonModifier()


"""

Expand Down Expand Up @@ -557,4 +557,24 @@ final class IfConfigTests: PrettyPrintTestCase {
configuration.indentConditionalCompilationBlocks = false
assertPrettyPrintEqual(input: input, expected: input, linelength: 45, configuration: configuration)
}

func testIfConfigDeclPartOfImport() {
let input =
"""
#if os(Foo)
@_spiOnly
#elseif os(Bar)
@_spiOnly
#else
@_spiOnly
#endif
@_spi(Foo) import Foundation

"""
var configuration = Configuration.forTesting
configuration.respectsExistingLineBreaks = false
configuration.indentConditionalCompilationBlocks = false
assertPrettyPrintEqual(input: input, expected: input, linelength: 80, configuration: configuration)

}
}