Skip to content

Commit a32359c

Browse files
authored
Merge pull request swiftlang#175 from dylansturg/labels_need_space
Add spaces after labels for overlooked stmt types.
2 parents 2b00345 + 65d0e08 commit a32359c

File tree

6 files changed

+171
-0
lines changed

6 files changed

+171
-0
lines changed

Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
444444
// MARK: - Control flow statement nodes
445445

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

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

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

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

552554
override func visit(_ node: DoStmtSyntax) -> SyntaxVisitorContinueKind {
555+
after(node.labelColon, tokens: .space)
553556
arrangeBracesAndContents(of: node.body, contentsKeyPath: \.statements)
554557
return .visitChildren
555558
}
@@ -591,6 +594,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
591594
}
592595

593596
override func visit(_ node: SwitchStmtSyntax) -> SyntaxVisitorContinueKind {
597+
after(node.labelColon, tokens: .space)
594598
before(node.switchKeyword, tokens: .open)
595599
after(node.switchKeyword, tokens: .space)
596600
before(node.leftBrace, tokens: .break(.reset))
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import SwiftFormatConfiguration
2+
3+
final class DoStmtTests: PrettyPrintTestCase {
4+
func testBasicDoStmt() {
5+
let input =
6+
"""
7+
do {}
8+
do { f() }
9+
do { foo() }
10+
do { let a = 123
11+
var b = "abc"
12+
}
13+
do { veryLongFunctionCallThatShouldBeBrokenOntoANewLine() }
14+
"""
15+
16+
let expected =
17+
"""
18+
do {}
19+
do { f() }
20+
do { foo() }
21+
do {
22+
let a = 123
23+
var b = "abc"
24+
}
25+
do {
26+
veryLongFunctionCallThatShouldBeBrokenOntoANewLine()
27+
}
28+
29+
"""
30+
31+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 25)
32+
}
33+
34+
func testLabeledDoStmt() {
35+
let input = """
36+
someLabel:do {
37+
bar()
38+
baz()
39+
}
40+
somePrettyLongLabelThatTakesUpManyColumns: do {
41+
bar()
42+
baz()
43+
}
44+
"""
45+
46+
let expected = """
47+
someLabel: do {
48+
bar()
49+
baz()
50+
}
51+
somePrettyLongLabelThatTakesUpManyColumns: do
52+
{
53+
bar()
54+
baz()
55+
}
56+
57+
"""
58+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 45)
59+
}
60+
}
61+

Tests/SwiftFormatPrettyPrintTests/IfStmtTests.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,4 +488,31 @@ final class IfStmtTests: PrettyPrintTestCase {
488488

489489
assertPrettyPrintEqual(input: input, expected: expected, linelength: 50)
490490
}
491+
492+
func testLabeledIfStmt() {
493+
let input =
494+
"""
495+
someLabel:if foo && bar {
496+
// do something
497+
}
498+
anotherVeryLongLabelThatTakesUpTooManyCharacters: if foo && bar {
499+
// do something else
500+
}
501+
"""
502+
503+
let expected =
504+
"""
505+
someLabel: if foo && bar {
506+
// do something
507+
}
508+
anotherVeryLongLabelThatTakesUpTooManyCharacters: if foo
509+
&& bar
510+
{
511+
// do something else
512+
}
513+
514+
"""
515+
516+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 50)
517+
}
491518
}

Tests/SwiftFormatPrettyPrintTests/RepeatStmtTests.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,31 @@ final class RepeatStmtTests: PrettyPrintTestCase {
125125
"""
126126
assertPrettyPrintEqual(input: input, expected: input + "\n", linelength: 45)
127127
}
128+
129+
func testLabeledRepeat() {
130+
let input = """
131+
someLabel:repeat {
132+
bar()
133+
baz()
134+
} while condition
135+
somePrettyLongLabelThatTakesUpManyColumns: repeat {
136+
bar()
137+
baz()
138+
} while condition
139+
"""
140+
141+
let expected = """
142+
someLabel: repeat {
143+
bar()
144+
baz()
145+
} while condition
146+
somePrettyLongLabelThatTakesUpManyColumns: repeat
147+
{
148+
bar()
149+
baz()
150+
} while condition
151+
152+
"""
153+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 45)
154+
}
128155
}

Tests/SwiftFormatPrettyPrintTests/SwitchStmtTests.swift

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,4 +288,42 @@ final class SwitchStmtTests: PrettyPrintTestCase {
288288

289289
assertPrettyPrintEqual(input: input, expected: expected, linelength: 40)
290290
}
291+
292+
func testLabeledSwitchStmt() {
293+
let input =
294+
"""
295+
label:switch foo {
296+
case bar:
297+
callForBar()
298+
case baz:
299+
callForBaz()
300+
}
301+
someVeryExtremelyLongLabel: switch foo {
302+
case bar:
303+
callForBar()
304+
case baz:
305+
callForBaz()
306+
}
307+
"""
308+
309+
let expected =
310+
"""
311+
label: switch foo {
312+
case bar:
313+
callForBar()
314+
case baz:
315+
callForBaz()
316+
}
317+
someVeryExtremelyLongLabel: switch foo
318+
{
319+
case bar:
320+
callForBar()
321+
case baz:
322+
callForBaz()
323+
}
324+
325+
"""
326+
327+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 20)
328+
}
291329
}

Tests/SwiftFormatPrettyPrintTests/XCTestManifests.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,16 @@ extension DifferentiationAttributeTests {
215215
]
216216
}
217217

218+
extension DoStmtTests {
219+
// DO NOT MODIFY: This is autogenerated, use:
220+
// `swift test --generate-linuxmain`
221+
// to regenerate.
222+
static let __allTests__DoStmtTests = [
223+
("testBasicDoStmt", testBasicDoStmt),
224+
("testLabeledDoStmt", testLabeledDoStmt),
225+
]
226+
}
227+
218228
extension EnumDeclTests {
219229
// DO NOT MODIFY: This is autogenerated, use:
220230
// `swift test --generate-linuxmain`
@@ -396,6 +406,7 @@ extension IfStmtTests {
396406
("testIfElseStatement_noBreakBeforeElse", testIfElseStatement_noBreakBeforeElse),
397407
("testIfLetStatements", testIfLetStatements),
398408
("testIfStatement", testIfStatement),
409+
("testLabeledIfStmt", testLabeledIfStmt),
399410
("testMatchingPatternConditions", testMatchingPatternConditions),
400411
("testOptionalBindingConditions", testOptionalBindingConditions),
401412
("testParenthesizedClauses", testParenthesizedClauses),
@@ -557,6 +568,7 @@ extension RepeatStmtTests {
557568
static let __allTests__RepeatStmtTests = [
558569
("testBasicRepeatTests_breakBeforeWhile", testBasicRepeatTests_breakBeforeWhile),
559570
("testBasicRepeatTests_noBreakBeforeWhile", testBasicRepeatTests_noBreakBeforeWhile),
571+
("testLabeledRepeat", testLabeledRepeat),
560572
("testNestedRepeat", testNestedRepeat),
561573
]
562574
}
@@ -693,6 +705,7 @@ extension SwitchStmtTests {
693705
// to regenerate.
694706
static let __allTests__SwitchStmtTests = [
695707
("testBasicSwitch", testBasicSwitch),
708+
("testLabeledSwitchStmt", testLabeledSwitchStmt),
696709
("testNestedSwitch", testNestedSwitch),
697710
("testNewlinesDisambiguatingWhereClauses", testNewlinesDisambiguatingWhereClauses),
698711
("testSwitchCases", testSwitchCases),
@@ -816,6 +829,7 @@ public func __allTests() -> [XCTestCaseEntry] {
816829
testCase(DeinitializerDeclTests.__allTests__DeinitializerDeclTests),
817830
testCase(DictionaryDeclTests.__allTests__DictionaryDeclTests),
818831
testCase(DifferentiationAttributeTests.__allTests__DifferentiationAttributeTests),
832+
testCase(DoStmtTests.__allTests__DoStmtTests),
819833
testCase(EnumDeclTests.__allTests__EnumDeclTests),
820834
testCase(ExtensionDeclTests.__allTests__ExtensionDeclTests),
821835
testCase(ForInStmtTests.__allTests__ForInStmtTests),

0 commit comments

Comments
 (0)