Skip to content

Add spaces after labels for overlooked stmt types. #175

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 2, 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
4 changes: 4 additions & 0 deletions Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
// MARK: - Control flow statement nodes

override func visit(_ node: IfStmtSyntax) -> SyntaxVisitorContinueKind {
after(node.labelColon, tokens: .space)
after(node.ifKeyword, tokens: .space)

// Add break groups, using open continuation breaks, around any conditions after the first so
Expand Down Expand Up @@ -531,6 +532,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
}

override func visit(_ node: RepeatWhileStmtSyntax) -> SyntaxVisitorContinueKind {
after(node.labelColon, tokens: .space)
arrangeBracesAndContents(of: node.body, contentsKeyPath: \.statements)

if config.lineBreakBeforeControlFlowKeywords {
Expand All @@ -550,6 +552,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
}

override func visit(_ node: DoStmtSyntax) -> SyntaxVisitorContinueKind {
after(node.labelColon, tokens: .space)
arrangeBracesAndContents(of: node.body, contentsKeyPath: \.statements)
return .visitChildren
}
Expand Down Expand Up @@ -591,6 +594,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
}

override func visit(_ node: SwitchStmtSyntax) -> SyntaxVisitorContinueKind {
after(node.labelColon, tokens: .space)
before(node.switchKeyword, tokens: .open)
after(node.switchKeyword, tokens: .space)
before(node.leftBrace, tokens: .break(.reset))
Expand Down
61 changes: 61 additions & 0 deletions Tests/SwiftFormatPrettyPrintTests/DoStmtTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import SwiftFormatConfiguration

final class DoStmtTests: PrettyPrintTestCase {
func testBasicDoStmt() {
let input =
"""
do {}
do { f() }
do { foo() }
do { let a = 123
var b = "abc"
}
do { veryLongFunctionCallThatShouldBeBrokenOntoANewLine() }
"""

let expected =
"""
do {}
do { f() }
do { foo() }
do {
let a = 123
var b = "abc"
}
do {
veryLongFunctionCallThatShouldBeBrokenOntoANewLine()
}

"""

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

func testLabeledDoStmt() {
let input = """
someLabel:do {
bar()
baz()
}
somePrettyLongLabelThatTakesUpManyColumns: do {
bar()
baz()
}
"""

let expected = """
someLabel: do {
bar()
baz()
}
somePrettyLongLabelThatTakesUpManyColumns: do
{
bar()
baz()
}

"""
assertPrettyPrintEqual(input: input, expected: expected, linelength: 45)
}
}

27 changes: 27 additions & 0 deletions Tests/SwiftFormatPrettyPrintTests/IfStmtTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -488,4 +488,31 @@ final class IfStmtTests: PrettyPrintTestCase {

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

func testLabeledIfStmt() {
let input =
"""
someLabel:if foo && bar {
// do something
}
anotherVeryLongLabelThatTakesUpTooManyCharacters: if foo && bar {
// do something else
}
"""

let expected =
"""
someLabel: if foo && bar {
// do something
}
anotherVeryLongLabelThatTakesUpTooManyCharacters: if foo
&& bar
{
// do something else
}

"""

assertPrettyPrintEqual(input: input, expected: expected, linelength: 50)
}
}
27 changes: 27 additions & 0 deletions Tests/SwiftFormatPrettyPrintTests/RepeatStmtTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,31 @@ final class RepeatStmtTests: PrettyPrintTestCase {
"""
assertPrettyPrintEqual(input: input, expected: input + "\n", linelength: 45)
}

func testLabeledRepeat() {
let input = """
someLabel:repeat {
bar()
baz()
} while condition
somePrettyLongLabelThatTakesUpManyColumns: repeat {
bar()
baz()
} while condition
"""

let expected = """
someLabel: repeat {
bar()
baz()
} while condition
somePrettyLongLabelThatTakesUpManyColumns: repeat
{
bar()
baz()
} while condition

"""
assertPrettyPrintEqual(input: input, expected: expected, linelength: 45)
}
}
38 changes: 38 additions & 0 deletions Tests/SwiftFormatPrettyPrintTests/SwitchStmtTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -288,4 +288,42 @@ final class SwitchStmtTests: PrettyPrintTestCase {

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

func testLabeledSwitchStmt() {
let input =
"""
label:switch foo {
case bar:
callForBar()
case baz:
callForBaz()
}
someVeryExtremelyLongLabel: switch foo {
case bar:
callForBar()
case baz:
callForBaz()
}
"""

let expected =
"""
label: switch foo {
case bar:
callForBar()
case baz:
callForBaz()
}
someVeryExtremelyLongLabel: switch foo
{
case bar:
callForBar()
case baz:
callForBaz()
}

"""

assertPrettyPrintEqual(input: input, expected: expected, linelength: 20)
}
}
14 changes: 14 additions & 0 deletions Tests/SwiftFormatPrettyPrintTests/XCTestManifests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,16 @@ extension DifferentiationAttributeTests {
]
}

extension DoStmtTests {
// DO NOT MODIFY: This is autogenerated, use:
// `swift test --generate-linuxmain`
// to regenerate.
static let __allTests__DoStmtTests = [
("testBasicDoStmt", testBasicDoStmt),
("testLabeledDoStmt", testLabeledDoStmt),
]
}

extension EnumDeclTests {
// DO NOT MODIFY: This is autogenerated, use:
// `swift test --generate-linuxmain`
Expand Down Expand Up @@ -394,6 +404,7 @@ extension IfStmtTests {
("testIfElseStatement_noBreakBeforeElse", testIfElseStatement_noBreakBeforeElse),
("testIfLetStatements", testIfLetStatements),
("testIfStatement", testIfStatement),
("testLabeledIfStmt", testLabeledIfStmt),
("testMatchingPatternConditions", testMatchingPatternConditions),
("testOptionalBindingConditions", testOptionalBindingConditions),
("testParenthesizedClauses", testParenthesizedClauses),
Expand Down Expand Up @@ -555,6 +566,7 @@ extension RepeatStmtTests {
static let __allTests__RepeatStmtTests = [
("testBasicRepeatTests_breakBeforeWhile", testBasicRepeatTests_breakBeforeWhile),
("testBasicRepeatTests_noBreakBeforeWhile", testBasicRepeatTests_noBreakBeforeWhile),
("testLabeledRepeat", testLabeledRepeat),
("testNestedRepeat", testNestedRepeat),
]
}
Expand Down Expand Up @@ -691,6 +703,7 @@ extension SwitchStmtTests {
// to regenerate.
static let __allTests__SwitchStmtTests = [
("testBasicSwitch", testBasicSwitch),
("testLabeledSwitchStmt", testLabeledSwitchStmt),
("testNestedSwitch", testNestedSwitch),
("testNewlinesDisambiguatingWhereClauses", testNewlinesDisambiguatingWhereClauses),
("testSwitchCases", testSwitchCases),
Expand Down Expand Up @@ -814,6 +827,7 @@ public func __allTests() -> [XCTestCaseEntry] {
testCase(DeinitializerDeclTests.__allTests__DeinitializerDeclTests),
testCase(DictionaryDeclTests.__allTests__DictionaryDeclTests),
testCase(DifferentiationAttributeTests.__allTests__DifferentiationAttributeTests),
testCase(DoStmtTests.__allTests__DoStmtTests),
testCase(EnumDeclTests.__allTests__EnumDeclTests),
testCase(ExtensionDeclTests.__allTests__ExtensionDeclTests),
testCase(ForInStmtTests.__allTests__ForInStmtTests),
Expand Down