Skip to content

Add breaks around conditions of while-stmt. #184

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 8, 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
13 changes: 13 additions & 0 deletions Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,19 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
after(node.labelColon, tokens: .space)
after(node.whileKeyword, tokens: .space)

// Add break groups, using open continuation breaks, around any conditions after the first so
// that continuations inside of the conditions can stack in addition to continuations between
// the conditions. There are no breaks around the first condition because there was historically
// not break after the while token and adding such a break would cause excessive changes to
// previously formatted code.
// This has the side effect that the label + `while` + tokens up to the first break in the first
// condition could be longer than the column limit since there are no breaks between the label
// or while token.
for condition in node.conditions.dropFirst() {
before(condition.firstToken, tokens: .break(.open(kind: .continuation), size: 0))
after(condition.lastToken, tokens: .break(.close(mustBreak: false), size: 0))
}

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

return .visitChildren
Expand Down
54 changes: 54 additions & 0 deletions Tests/SwiftFormatPrettyPrintTests/WhileStmtTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ final class WhileStmtTests: PrettyPrintTestCase {
let a = 123
var b = "abc"
}
myLabel: while myVeryLongFirstCondition && condition2 || condition3
{
let a = 123
var b = "abc"
}
"""

let expected =
Expand All @@ -83,6 +88,55 @@ final class WhileStmtTests: PrettyPrintTestCase {
let a = 123
var b = "abc"
}
myLabel: while myVeryLongFirstCondition
&& condition2 || condition3
{
let a = 123
var b = "abc"
}

"""

assertPrettyPrintEqual(input: input, expected: expected, linelength: 29)
}

func testWhileLoopMultipleConditionElements() {
let input =
"""
while x >= 0 && y >= 0 && x < foo && y < bar, let object = foo.value(at: y), let otherObject = foo.value(at: x), isEqual(a, b) {
foo()
}
while x >= 0 && y >= 0
&& x < foo && y < bar,
let object =
foo.value(at: y),
let otherObject = foo.value(at: x), isEqual(a, b) {
foo()
}
"""

let expected =
"""
while x >= 0 && y >= 0
&& x < foo && y < bar,
let object = foo.value(
at: y),
let otherObject =
foo.value(at: x),
isEqual(a, b)
{
foo()
}
while x >= 0 && y >= 0
&& x < foo && y < bar,
let object =
foo.value(at: y),
let otherObject =
foo.value(at: x),
isEqual(a, b)
{
foo()
}

"""

Expand Down
2 changes: 2 additions & 0 deletions Tests/SwiftFormatPrettyPrintTests/XCTestManifests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,7 @@ extension NewlineTests {
static let __allTests__NewlineTests = [
("testLeadingNewlines", testLeadingNewlines),
("testLeadingNewlinesWithComments", testLeadingNewlinesWithComments),
("testNewlinesBetweenMembers", testNewlinesBetweenMembers),
("testTrailingNewlines", testTrailingNewlines),
("testTrailingNewlinesWithComments", testTrailingNewlinesWithComments),
]
Expand Down Expand Up @@ -802,6 +803,7 @@ extension WhileStmtTests {
static let __allTests__WhileStmtTests = [
("testBasicWhileLoops", testBasicWhileLoops),
("testLabeledWhileLoops", testLabeledWhileLoops),
("testWhileLoopMultipleConditionElements", testWhileLoopMultipleConditionElements),
]
}

Expand Down