Skip to content

Commit aa8f3f4

Browse files
committed
Updates for recent syntax/toolchain changes.
- Remove differentiation nodes that no longer exist. - Add support for multiple `catch` clauses (SE-0276). - Remove a never-used configuration helper type that incorrectly used a `let`-with-initial-value, which recent toolchain snapshots now issue a warning for in `Decodable` types. This PR moves the `master` branch up to require the `swift-DEVELOPMENT-SNAPSHOT-2020-04-19-a` development snapshot toolchain.
1 parent 80ce1ed commit aa8f3f4

File tree

7 files changed

+95
-56
lines changed

7 files changed

+95
-56
lines changed

Package.resolved

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,14 @@ let package = Package(
2121
.library(name: "SwiftFormatConfiguration", targets: ["SwiftFormatConfiguration"]),
2222
],
2323
dependencies: [
24-
.package(url: "https://github.com/apple/swift-syntax", from: "0.50200.0"),
25-
.package(url: "https://github.com/apple/swift-argument-parser.git", .upToNextMinor(from: "0.0.4")),
24+
.package(
25+
url: "https://github.com/apple/swift-syntax",
26+
.revision("swift-DEVELOPMENT-SNAPSHOT-2020-04-19-a")
27+
),
28+
.package(
29+
url: "https://github.com/apple/swift-argument-parser.git",
30+
.upToNextMinor(from: "0.0.4")
31+
),
2632
],
2733
targets: [
2834
.target(

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ is also expressed in the `SwiftSyntax` dependency in
2525

2626
| Xcode Release | Swift Version | `swift-format` Branch |
2727
|:-------------:|:---------------------------------------:|:----------------------|
28-
|| swift-5.2-RELEASE | `master` |
28+
|| swift-DEVELOPMENT-SNAPSHOT-2020-04-19-a | `master` |
2929
| Xcode 11.4 | Swift 5.2 | `swift-5.2-branch` |
3030
| Xcode 11.0 | Swift 5.1 | `swift-5.1-branch` |
3131

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

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

5152
```
5253
swift test --parallel
5354
```
54-
We recommend using the `--parallel` flag to speed up the test run since there are a large number of tests.
55+
We recommend using the `--parallel` flag to speed up the test run since there
56+
are a large number of tests.
5557

5658
## Command Line Usage
5759

Sources/SwiftFormatConfiguration/Configuration.swift

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -228,23 +228,6 @@ public struct Configuration: Codable, Equatable {
228228
}
229229
}
230230

231-
/// Configuration for the NoPlaygroundLiterals rule.
232-
public struct NoPlaygroundLiteralsConfiguration: Codable, Equatable {
233-
public enum ResolveBehavior: String, Codable {
234-
/// If not sure, use `UIColor` to replace `#colorLiteral`.
235-
case useUIColor
236-
237-
/// If not sure, use `NSColor` to replace `#colorLiteral`.
238-
case useNSColor
239-
240-
/// If not sure, raise an error.
241-
case error
242-
}
243-
244-
/// Resolution behavior to use when encountering an ambiguous `#colorLiteral`.
245-
public let resolveAmbiguousColor: ResolveBehavior = .useUIColor
246-
}
247-
248231
/// Configuration for the `FileScopedDeclarationPrivacy` rule.
249232
public struct FileScopedDeclarationPrivacyConfiguration: Codable, Equatable {
250233
public enum AccessLevel: String, Codable {

Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,20 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
580580
let catchPrecedingBreak = config.lineBreakBeforeControlFlowKeywords
581581
? Token.break(.same, newlines: .soft) : Token.space
582582
before(node.catchKeyword, tokens: catchPrecedingBreak)
583-
before(node.pattern?.firstToken, tokens: .space)
583+
584+
if let catchItems = node.catchItems {
585+
// If there are multiple items in the `catch` clause, wrap each in open/close breaks so that
586+
// their internal breaks stack correctly. Otherwise, if there is only a single clause, use the
587+
// old (pre-SE-0276) behavior (a fixed space after the `catch` keyword).
588+
if catchItems.count > 1 {
589+
for catchItem in catchItems {
590+
before(catchItem.firstToken, tokens: .break(.open(kind: .continuation)))
591+
after(catchItem.lastToken, tokens: .break(.close(mustBreak: false), size: 0))
592+
}
593+
} else {
594+
before(node.catchItems?.firstToken, tokens: .space)
595+
}
596+
}
584597

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

@@ -1499,7 +1512,9 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
14991512
// }
15001513
//
15011514
let wherePrecedingBreak: Token
1502-
if !config.lineBreakBeforeControlFlowKeywords, let parent = node.parent, parent.is(CatchClauseSyntax.self) {
1515+
if !config.lineBreakBeforeControlFlowKeywords,
1516+
let parent = node.parent, parent.is(CatchItemSyntax.self)
1517+
{
15031518
wherePrecedingBreak = .break(.continue)
15041519
} else {
15051520
wherePrecedingBreak = .break(.same)
@@ -2100,21 +2115,6 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
21002115
needsBreakBeforeWhereClause = true
21012116
}
21022117

2103-
// TODO: These properties will likely go away in a future version since the parser no longer
2104-
// reads the `vjp:` and `jvp:` arguments to `@differentiable`.
2105-
if let vjp = node.maybeVJP {
2106-
before(vjp.firstToken, tokens: .open)
2107-
after(vjp.lastToken, tokens: .close)
2108-
after(vjp.trailingComma, tokens: .break(.same))
2109-
needsBreakBeforeWhereClause = true
2110-
}
2111-
if let jvp = node.maybeJVP {
2112-
before(jvp.firstToken, tokens: .open)
2113-
after(jvp.lastToken, tokens: .close)
2114-
after(jvp.trailingComma, tokens: .break(.same))
2115-
needsBreakBeforeWhereClause = true
2116-
}
2117-
21182118
if let whereClause = node.whereClause {
21192119
if needsBreakBeforeWhereClause {
21202120
before(whereClause.firstToken, tokens: .break(.same))
@@ -2125,24 +2125,13 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
21252125
return .visitChildren
21262126
}
21272127

2128-
override func visit(_ node: DifferentiableAttributeFuncSpecifierSyntax)
2129-
-> SyntaxVisitorContinueKind
2130-
{
2131-
// This node encapsulates the `vjp:` or `jvp:` label and decl name in a `@differentiable`
2132-
// attribute.
2133-
// TODO: This node will likely go away in a future version since the parser no longer reads the
2134-
// `vjp:` and `jvp:` arguments to `@differentiable`.
2135-
after(node.colon, tokens: .break(.continue, newlines: .elective(ignoresDiscretionary: true)))
2136-
return .visitChildren
2137-
}
2138-
2139-
override func visit(_ node: DifferentiationParamsSyntax) -> SyntaxVisitorContinueKind {
2128+
override func visit(_ node: DifferentiabilityParamsSyntax) -> SyntaxVisitorContinueKind {
21402129
after(node.leftParen, tokens: .break(.open, size: 0), .open)
21412130
before(node.rightParen, tokens: .break(.close, size: 0), .close)
21422131
return .visitChildren
21432132
}
21442133

2145-
override func visit(_ node: DifferentiationParamSyntax) -> SyntaxVisitorContinueKind {
2134+
override func visit(_ node: DifferentiabilityParamSyntax) -> SyntaxVisitorContinueKind {
21462135
after(node.trailingComma, tokens: .break(.same))
21472136
return .visitChildren
21482137
}
@@ -2168,7 +2157,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
21682157
}
21692158
#endif
21702159

2171-
override func visit(_ node: DifferentiationParamsClauseSyntax) -> SyntaxVisitorContinueKind {
2160+
override func visit(_ node: DifferentiabilityParamsClauseSyntax) -> SyntaxVisitorContinueKind {
21722161
// This node encapsulates the `wrt:` label and value/variable in a `@differentiable`,
21732162
// `@derivative`, or `@transpose` attribute.
21742163
after(node.colon, tokens: .break(.continue, newlines: .elective(ignoresDiscretionary: true)))

Tests/SwiftFormatPrettyPrintTests/TryCatchTests.swift

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,4 +173,62 @@ final class TryCatchTests: PrettyPrintTestCase {
173173
"""
174174
assertPrettyPrintEqual(input: input, expected: input + "\n", linelength: 45)
175175
}
176+
177+
func testMultipleCatchItems() {
178+
let input =
179+
"""
180+
do { try thisMightFail() } catch error1, error2 { print("Nope") }
181+
do { try thisMightFail() } catch longErrorType, error2 { print("Nope") }
182+
do { try thisMightFail() } catch longErrorTypeName, longErrorType2(let someLongVariable) { print("Nope") }
183+
do { try thisMightFail() } catch longErrorTypeName, longErrorType2 as SomeLongErrorType { print("Nope") }
184+
do { try thisMightFail() } catch longErrorName where someCondition, longErrorType2 { print("Nope") }
185+
do { try thisMightFail() } catch longErrorTypeName, longErrorType2 as SomeLongErrorType where someCondition, longErrorType3 { print("Nope") }
186+
"""
187+
188+
let expected =
189+
"""
190+
do {
191+
try thisMightFail()
192+
} catch error1, error2 {
193+
print("Nope")
194+
}
195+
do {
196+
try thisMightFail()
197+
} catch longErrorType,
198+
error2
199+
{ print("Nope") }
200+
do {
201+
try thisMightFail()
202+
} catch
203+
longErrorTypeName,
204+
longErrorType2(
205+
let someLongVariable)
206+
{ print("Nope") }
207+
do {
208+
try thisMightFail()
209+
} catch
210+
longErrorTypeName,
211+
longErrorType2
212+
as SomeLongErrorType
213+
{ print("Nope") }
214+
do {
215+
try thisMightFail()
216+
} catch longErrorName
217+
where someCondition,
218+
longErrorType2
219+
{ print("Nope") }
220+
do {
221+
try thisMightFail()
222+
} catch
223+
longErrorTypeName,
224+
longErrorType2
225+
as SomeLongErrorType
226+
where someCondition,
227+
longErrorType3
228+
{ print("Nope") }
229+
230+
"""
231+
232+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 25)
233+
}
176234
}

Tests/SwiftFormatPrettyPrintTests/XCTestManifests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,7 @@ extension TryCatchTests {
747747
("testCatchWhere_noBreakBeforeCatch", testCatchWhere_noBreakBeforeCatch),
748748
("testDoTryCatch_breakBeforeCatch", testDoTryCatch_breakBeforeCatch),
749749
("testDoTryCatch_noBreakBeforeCatch", testDoTryCatch_noBreakBeforeCatch),
750+
("testMultipleCatchItems", testMultipleCatchItems),
750751
("testNestedDo", testNestedDo),
751752
]
752753
}

0 commit comments

Comments
 (0)