Skip to content

Commit

Permalink
Merge pull request #216 from dylansturg/nondiscretionary_try_breaks
Browse files Browse the repository at this point in the history
Use non-discretionary newlines in the break after `try` keyword.
  • Loading branch information
allevato committed Aug 5, 2020
2 parents ed45527 + cc6f926 commit 35fdb4f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
4 changes: 3 additions & 1 deletion Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1420,7 +1420,9 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
}

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

// Check for an anchor token inside of the expression to group with the try keyword.
if let anchorToken = findTryExprConnectingToken(inExpr: node.expression) {
Expand Down
10 changes: 10 additions & 0 deletions Tests/SwiftFormatPrettyPrintTests/TryCatchTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,8 @@ final class TryCatchTests: PrettyPrintTestCase {
let input =
"""
let aVeryLongArgumentName = try foo.bar()
let aVeryLongArgumentName = try
foo.bar()
let aVeryLongArgumentName = try? foo.bar()
let abc = try foo.baz().quxxe(a, b, c).bar()
let abc = try foo
Expand All @@ -246,10 +248,15 @@ final class TryCatchTests: PrettyPrintTestCase {
let abc = try foo.baz().quxxe(a, b, c).bar[0]
let abc = try foo
.baz().quxxe(a, b, c).bar[0]
let abc = try
foo
.baz().quxxe(a, b, c).bar[0]
"""

let expected =
"""
let aVeryLongArgumentName =
try foo.bar()
let aVeryLongArgumentName =
try foo.bar()
let aVeryLongArgumentName =
Expand All @@ -268,6 +275,9 @@ final class TryCatchTests: PrettyPrintTestCase {
let abc =
try foo
.baz().quxxe(a, b, c).bar[0]
let abc =
try foo
.baz().quxxe(a, b, c).bar[0]
"""

Expand Down

0 comments on commit 35fdb4f

Please sign in to comment.