Skip to content

Updates for recent syntax/toolchain changes (2020-04-19). #193

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

Merged
merged 1 commit into from
Apr 21, 2020
Merged
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
6 changes: 3 additions & 3 deletions Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,14 @@ let package = Package(
.library(name: "SwiftFormatConfiguration", targets: ["SwiftFormatConfiguration"]),
],
dependencies: [
.package(url: "https://github.com/apple/swift-syntax", from: "0.50200.0"),
.package(url: "https://github.com/apple/swift-argument-parser.git", .upToNextMinor(from: "0.0.4")),
.package(
url: "https://github.com/apple/swift-syntax",
.revision("swift-DEVELOPMENT-SNAPSHOT-2020-04-19-a")
),
.package(
url: "https://github.com/apple/swift-argument-parser.git",
.upToNextMinor(from: "0.0.4")
),
],
targets: [
.target(
Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ is also expressed in the `SwiftSyntax` dependency in

| Xcode Release | Swift Version | `swift-format` Branch |
|:-------------:|:---------------------------------------:|:----------------------|
| – | swift-5.2-RELEASE | `master` |
| – | swift-DEVELOPMENT-SNAPSHOT-2020-04-19-a | `master` |
| Xcode 11.4 | Swift 5.2 | `swift-5.2-branch` |
| Xcode 11.0 | Swift 5.1 | `swift-5.1-branch` |

Expand All @@ -46,12 +46,14 @@ version of Swift or on a developer snapshot. Changes committed to `master`
that are compatible with the latest release branch will be cherry-picked into
that branch.

To test that the formatter was built succesfully and is compatible with your swift toolchain, you can run the following command:
To test that the formatter was built succesfully and is compatible with your
Swift toolchain, you can run the following command:

```
swift test --parallel
```
We recommend using the `--parallel` flag to speed up the test run since there are a large number of tests.
We recommend using the `--parallel` flag to speed up the test run since there
are a large number of tests.

## Command Line Usage

Expand Down
17 changes: 0 additions & 17 deletions Sources/SwiftFormatConfiguration/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -228,23 +228,6 @@ public struct Configuration: Codable, Equatable {
}
}

/// Configuration for the NoPlaygroundLiterals rule.
public struct NoPlaygroundLiteralsConfiguration: Codable, Equatable {
public enum ResolveBehavior: String, Codable {
/// If not sure, use `UIColor` to replace `#colorLiteral`.
case useUIColor

/// If not sure, use `NSColor` to replace `#colorLiteral`.
case useNSColor

/// If not sure, raise an error.
case error
}

/// Resolution behavior to use when encountering an ambiguous `#colorLiteral`.
public let resolveAmbiguousColor: ResolveBehavior = .useUIColor
}

/// Configuration for the `FileScopedDeclarationPrivacy` rule.
public struct FileScopedDeclarationPrivacyConfiguration: Codable, Equatable {
public enum AccessLevel: String, Codable {
Expand Down
51 changes: 20 additions & 31 deletions Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,20 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
let catchPrecedingBreak = config.lineBreakBeforeControlFlowKeywords
? Token.break(.same, newlines: .soft) : Token.space
before(node.catchKeyword, tokens: catchPrecedingBreak)
before(node.pattern?.firstToken, tokens: .space)

if let catchItems = node.catchItems {
// If there are multiple items in the `catch` clause, wrap each in open/close breaks so that
// their internal breaks stack correctly. Otherwise, if there is only a single clause, use the
// old (pre-SE-0276) behavior (a fixed space after the `catch` keyword).
if catchItems.count > 1 {
for catchItem in catchItems {
before(catchItem.firstToken, tokens: .break(.open(kind: .continuation)))
after(catchItem.lastToken, tokens: .break(.close(mustBreak: false), size: 0))
}
} else {
before(node.catchItems?.firstToken, tokens: .space)
}
}

arrangeBracesAndContents(of: node.body, contentsKeyPath: \.statements)

Expand Down Expand Up @@ -1499,7 +1512,9 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
// }
//
let wherePrecedingBreak: Token
if !config.lineBreakBeforeControlFlowKeywords, let parent = node.parent, parent.is(CatchClauseSyntax.self) {
if !config.lineBreakBeforeControlFlowKeywords,
let parent = node.parent, parent.is(CatchItemSyntax.self)
{
wherePrecedingBreak = .break(.continue)
} else {
wherePrecedingBreak = .break(.same)
Expand Down Expand Up @@ -2100,21 +2115,6 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
needsBreakBeforeWhereClause = true
}

// TODO: These properties will likely go away in a future version since the parser no longer
// reads the `vjp:` and `jvp:` arguments to `@differentiable`.
if let vjp = node.maybeVJP {
before(vjp.firstToken, tokens: .open)
after(vjp.lastToken, tokens: .close)
after(vjp.trailingComma, tokens: .break(.same))
needsBreakBeforeWhereClause = true
}
if let jvp = node.maybeJVP {
before(jvp.firstToken, tokens: .open)
after(jvp.lastToken, tokens: .close)
after(jvp.trailingComma, tokens: .break(.same))
needsBreakBeforeWhereClause = true
}

if let whereClause = node.whereClause {
if needsBreakBeforeWhereClause {
before(whereClause.firstToken, tokens: .break(.same))
Expand All @@ -2125,24 +2125,13 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
return .visitChildren
}

override func visit(_ node: DifferentiableAttributeFuncSpecifierSyntax)
-> SyntaxVisitorContinueKind
{
// This node encapsulates the `vjp:` or `jvp:` label and decl name in a `@differentiable`
// attribute.
// TODO: This node will likely go away in a future version since the parser no longer reads the
// `vjp:` and `jvp:` arguments to `@differentiable`.
after(node.colon, tokens: .break(.continue, newlines: .elective(ignoresDiscretionary: true)))
return .visitChildren
}

override func visit(_ node: DifferentiationParamsSyntax) -> SyntaxVisitorContinueKind {
override func visit(_ node: DifferentiabilityParamsSyntax) -> SyntaxVisitorContinueKind {
after(node.leftParen, tokens: .break(.open, size: 0), .open)
before(node.rightParen, tokens: .break(.close, size: 0), .close)
return .visitChildren
}

override func visit(_ node: DifferentiationParamSyntax) -> SyntaxVisitorContinueKind {
override func visit(_ node: DifferentiabilityParamSyntax) -> SyntaxVisitorContinueKind {
after(node.trailingComma, tokens: .break(.same))
return .visitChildren
}
Expand All @@ -2168,7 +2157,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
}
#endif

override func visit(_ node: DifferentiationParamsClauseSyntax) -> SyntaxVisitorContinueKind {
override func visit(_ node: DifferentiabilityParamsClauseSyntax) -> SyntaxVisitorContinueKind {
// This node encapsulates the `wrt:` label and value/variable in a `@differentiable`,
// `@derivative`, or `@transpose` attribute.
after(node.colon, tokens: .break(.continue, newlines: .elective(ignoresDiscretionary: true)))
Expand Down
58 changes: 58 additions & 0 deletions Tests/SwiftFormatPrettyPrintTests/TryCatchTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -173,4 +173,62 @@ final class TryCatchTests: PrettyPrintTestCase {
"""
assertPrettyPrintEqual(input: input, expected: input + "\n", linelength: 45)
}

func testMultipleCatchItems() {
let input =
"""
do { try thisMightFail() } catch error1, error2 { print("Nope") }
do { try thisMightFail() } catch longErrorType, error2 { print("Nope") }
do { try thisMightFail() } catch longErrorTypeName, longErrorType2(let someLongVariable) { print("Nope") }
do { try thisMightFail() } catch longErrorTypeName, longErrorType2 as SomeLongErrorType { print("Nope") }
do { try thisMightFail() } catch longErrorName where someCondition, longErrorType2 { print("Nope") }
do { try thisMightFail() } catch longErrorTypeName, longErrorType2 as SomeLongErrorType where someCondition, longErrorType3 { print("Nope") }
"""

let expected =
"""
do {
try thisMightFail()
} catch error1, error2 {
print("Nope")
}
do {
try thisMightFail()
} catch longErrorType,
error2
{ print("Nope") }
do {
try thisMightFail()
} catch
longErrorTypeName,
longErrorType2(
let someLongVariable)
{ print("Nope") }
do {
try thisMightFail()
} catch
longErrorTypeName,
longErrorType2
as SomeLongErrorType
{ print("Nope") }
do {
try thisMightFail()
} catch longErrorName
where someCondition,
longErrorType2
{ print("Nope") }
do {
try thisMightFail()
} catch
longErrorTypeName,
longErrorType2
as SomeLongErrorType
where someCondition,
longErrorType3
{ print("Nope") }

"""

assertPrettyPrintEqual(input: input, expected: expected, linelength: 25)
}
}
1 change: 1 addition & 0 deletions Tests/SwiftFormatPrettyPrintTests/XCTestManifests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,7 @@ extension TryCatchTests {
("testCatchWhere_noBreakBeforeCatch", testCatchWhere_noBreakBeforeCatch),
("testDoTryCatch_breakBeforeCatch", testDoTryCatch_breakBeforeCatch),
("testDoTryCatch_noBreakBeforeCatch", testDoTryCatch_noBreakBeforeCatch),
("testMultipleCatchItems", testMultipleCatchItems),
("testNestedDo", testNestedDo),
]
}
Expand Down