Skip to content

Move open group token past ifstmt's if token to avoid extra newlines. #191

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 16, 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
2 changes: 1 addition & 1 deletion Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1310,7 +1310,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
// This group applies to a top-level if-stmt so that all of the bodies will have the same
// breaking behavior.
if let ifStmt = node.item.as(IfStmtSyntax.self) {
before(ifStmt.firstToken, tokens: .open(.consistent))
before(ifStmt.conditions.firstToken, tokens: .open(.consistent))
after(ifStmt.lastToken, tokens: .close)
}
return .visitChildren
Expand Down
72 changes: 38 additions & 34 deletions Tests/SwiftFormatPrettyPrintTests/IfStmtTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -521,47 +521,51 @@ final class IfStmtTests: PrettyPrintTestCase {
func testMultipleIfStmts() {
let input =
"""
if foo && bar { baz() } else if bar { baz() } else if foo { baz() } else { blargh() }
if foo && bar && quxxe { baz() } else if bar { baz() } else if foo { baz() } else if quxxe { baz() } else { blargh() }
if let foo = getmyfoo(), let bar = getmybar(), foo.baz && bar.baz { foo() } else { bar() }
if let foo = getmyfoo(), let bar = getmybar(), foo.baz && bar.baz && someOtherCondition { foo() } else { bar() }
if let foo = getmyfoo(), let bar = getmybar(), foo.baz && bar.baz && someOtherCondition { foo() }
func foo() {
if foo && bar { baz() } else if bar { baz() } else if foo { baz() } else { blargh() }
if foo && bar && quxxe { baz() } else if bar { baz() } else if foo { baz() } else if quxxe { baz() } else { blargh() }
if let foo = getmyfoo(), let bar = getmybar(), foo.baz && bar.baz { foo() } else { bar() }
if let foo = getmyfoo(), let bar = getmybar(), foo.baz && bar.baz && someOtherCondition { foo() } else { bar() }
if let foo = getmyfoo(), let bar = getmybar(), foo.baz && bar.baz && someOtherCondition { foo() }
}
"""

let expected =
"""
if foo && bar { baz() } else if bar { baz() } else if foo { baz() } else { blargh() }
if foo && bar && quxxe {
baz()
} else if bar {
baz()
} else if foo {
baz()
} else if quxxe {
baz()
} else {
blargh()
}
if let foo = getmyfoo(), let bar = getmybar(), foo.baz && bar.baz {
foo()
} else {
bar()
}
if let foo = getmyfoo(), let bar = getmybar(),
foo.baz && bar.baz && someOtherCondition
{
foo()
} else {
bar()
}
if let foo = getmyfoo(), let bar = getmybar(),
foo.baz && bar.baz && someOtherCondition
{
foo()
func foo() {
if foo && bar { baz() } else if bar { baz() } else if foo { baz() } else { blargh() }
if foo && bar && quxxe {
baz()
} else if bar {
baz()
} else if foo {
baz()
} else if quxxe {
baz()
} else {
blargh()
}
if let foo = getmyfoo(), let bar = getmybar(), foo.baz && bar.baz {
foo()
} else {
bar()
}
if let foo = getmyfoo(), let bar = getmybar(),
foo.baz && bar.baz && someOtherCondition
{
foo()
} else {
bar()
}
if let foo = getmyfoo(), let bar = getmybar(),
foo.baz && bar.baz && someOtherCondition
{
foo()
}
}

"""

assertPrettyPrintEqual(input: input, expected: expected, linelength: 85)
assertPrettyPrintEqual(input: input, expected: expected, linelength: 87)
}
}