Skip to content

Commit 35fdb4f

Browse files
authored
Merge pull request #216 from dylansturg/nondiscretionary_try_breaks
Use non-discretionary newlines in the break after `try` keyword.
2 parents ed45527 + cc6f926 commit 35fdb4f

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift

+3-1
Original file line numberDiff line numberDiff line change
@@ -1420,7 +1420,9 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
14201420
}
14211421

14221422
override func visit(_ node: TryExprSyntax) -> SyntaxVisitorContinueKind {
1423-
before(node.expression.firstToken, tokens: .break)
1423+
before(
1424+
node.expression.firstToken,
1425+
tokens: .break(.continue, newlines: .elective(ignoresDiscretionary: true)))
14241426

14251427
// Check for an anchor token inside of the expression to group with the try keyword.
14261428
if let anchorToken = findTryExprConnectingToken(inExpr: node.expression) {

Tests/SwiftFormatPrettyPrintTests/TryCatchTests.swift

+10
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,8 @@ final class TryCatchTests: PrettyPrintTestCase {
236236
let input =
237237
"""
238238
let aVeryLongArgumentName = try foo.bar()
239+
let aVeryLongArgumentName = try
240+
foo.bar()
239241
let aVeryLongArgumentName = try? foo.bar()
240242
let abc = try foo.baz().quxxe(a, b, c).bar()
241243
let abc = try foo
@@ -246,10 +248,15 @@ final class TryCatchTests: PrettyPrintTestCase {
246248
let abc = try foo.baz().quxxe(a, b, c).bar[0]
247249
let abc = try foo
248250
.baz().quxxe(a, b, c).bar[0]
251+
let abc = try
252+
foo
253+
.baz().quxxe(a, b, c).bar[0]
249254
"""
250255

251256
let expected =
252257
"""
258+
let aVeryLongArgumentName =
259+
try foo.bar()
253260
let aVeryLongArgumentName =
254261
try foo.bar()
255262
let aVeryLongArgumentName =
@@ -268,6 +275,9 @@ final class TryCatchTests: PrettyPrintTestCase {
268275
let abc =
269276
try foo
270277
.baz().quxxe(a, b, c).bar[0]
278+
let abc =
279+
try foo
280+
.baz().quxxe(a, b, c).bar[0]
271281
272282
"""
273283

0 commit comments

Comments
 (0)