Skip to content

Commit c6c33a9

Browse files
authored
Merge pull request swiftlang#125 from dabelknap/switch-case-where
Support value binding for switch cases
2 parents 913a874 + f0021c7 commit c6c33a9

File tree

2 files changed

+123
-4
lines changed

2 files changed

+123
-4
lines changed

Sources/PrettyPrint/TokenStreamCreator.swift

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,17 @@ private final class TokenStreamCreator: SyntaxVisitor {
368368
super.visit(node)
369369
}
370370

371+
override func visit(_ node: CaseItemSyntax) {
372+
before(node.firstToken, tokens: .open)
373+
before(node.whereClause?.firstToken, tokens: .break)
374+
if let trailingComma = node.trailingComma {
375+
after(trailingComma, tokens: .close, .break)
376+
} else {
377+
after(node.lastToken, tokens: .close)
378+
}
379+
super.visit(node)
380+
}
381+
371382
override func visit(_ node: SwitchDefaultLabelSyntax) {
372383
// Implementation not needed.
373384
super.visit(node)
@@ -442,10 +453,6 @@ private final class TokenStreamCreator: SyntaxVisitor {
442453
super.visit(node)
443454
}
444455

445-
override func visit(_ node: CaseItemSyntax) {
446-
super.visit(node)
447-
}
448-
449456
override func visit(_ node: TypeExprSyntax) {
450457
super.visit(node)
451458
}
@@ -943,6 +950,7 @@ private final class TokenStreamCreator: SyntaxVisitor {
943950
}
944951

945952
override func visit(_ node: ValueBindingPatternSyntax) {
953+
after(node.letOrVarKeyword, tokens: .break)
946954
super.visit(node)
947955
}
948956

Tests/PrettyPrinterTests/SwitchStmtTests.swift

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,40 @@ public class SwitchStmtTests: PrettyPrintTestCase {
7575
assertPrettyPrintEqual(input: input, expected: expected, linelength: 35)
7676
}
7777

78+
public func testSwitchCompoundCases() {
79+
let input =
80+
"""
81+
switch someChar {
82+
case "a":
83+
print("a")
84+
case "b", "c":
85+
print("bc")
86+
case "d", "e", "f", "g", "h":
87+
print("defgh")
88+
default:
89+
print("default")
90+
}
91+
"""
92+
93+
let expected =
94+
"""
95+
switch someChar {
96+
case "a":
97+
print("a")
98+
case "b", "c":
99+
print("bc")
100+
case "d", "e", "f",
101+
"g", "h":
102+
print("defgh")
103+
default:
104+
print("default")
105+
}
106+
107+
"""
108+
109+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 20)
110+
}
111+
78112
public func testNestedSwitch() {
79113
let input =
80114
"""
@@ -109,4 +143,81 @@ public class SwitchStmtTests: PrettyPrintTestCase {
109143

110144
assertPrettyPrintEqual(input: input, expected: expected, linelength: 35)
111145
}
146+
147+
public func testSwitchValueBinding() {
148+
let input =
149+
"""
150+
switch someValue {
151+
case let thisval:
152+
let c = 123
153+
var d = 456 + thisval
154+
}
155+
switch somePoint {
156+
case (let x, 0):
157+
print(x)
158+
case (0, let y):
159+
print(y)
160+
case let (x, y):
161+
print(x + y)
162+
}
163+
switch anotherPoint {
164+
case (let distance, 0), (0, let distance):
165+
print(distance)
166+
case (let distance, 0), (0, let distance), (let distance, 10):
167+
print(distance)
168+
default:
169+
print("A message")
170+
}
171+
switch pointy {
172+
case let (x, y) where x == y:
173+
print("Equal")
174+
case let (x, y) where x == -y:
175+
print("Opposite sign")
176+
case let (reallyLongName, anotherLongName) where reallyLongName == -anotherLongName:
177+
print("Opposite sign")
178+
case let (x, y):
179+
print("Arbitrary value")
180+
}
181+
"""
182+
183+
let expected =
184+
"""
185+
switch someValue {
186+
case let thisval:
187+
let c = 123
188+
var d = 456 + thisval
189+
}
190+
switch somePoint {
191+
case (let x, 0):
192+
print(x)
193+
case (0, let y):
194+
print(y)
195+
case let (x, y):
196+
print(x + y)
197+
}
198+
switch anotherPoint {
199+
case (let distance, 0), (0, let distance):
200+
print(distance)
201+
case (let distance, 0), (0, let distance),
202+
(let distance, 10):
203+
print(distance)
204+
default:
205+
print("A message")
206+
}
207+
switch pointy {
208+
case let (x, y) where x == y:
209+
print("Equal")
210+
case let (x, y) where x == -y:
211+
print("Opposite sign")
212+
case let (reallyLongName, anotherLongName)
213+
where reallyLongName == -anotherLongName:
214+
print("Opposite sign")
215+
case let (x, y):
216+
print("Arbitrary value")
217+
}
218+
219+
"""
220+
221+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 45)
222+
}
112223
}

0 commit comments

Comments
 (0)