Skip to content

Commit 8df2cb0

Browse files
authored
Merge pull request #238 from dylansturg/pound_if_cases
Handle #if clauses inside of switch statements.
2 parents 9ecc5e2 + 191094c commit 8df2cb0

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed

Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,17 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
640640
before(node.leftBrace, tokens: .break(.reset))
641641
after(node.leftBrace, tokens: .close)
642642

643+
// An if-configuration clause around a switch-case encloses the case's node, so an
644+
// if-configuration clause requires a break here in order to be allowed on a new line.
645+
for ifConfigDecl in node.cases.filter({ $0.is(IfConfigDeclSyntax.self) }) {
646+
if config.indentSwitchCaseLabels {
647+
before(ifConfigDecl.firstToken, tokens: .break(.open))
648+
after(ifConfigDecl.lastToken, tokens: .break(.close, size: 0))
649+
} else {
650+
before(ifConfigDecl.firstToken, tokens: .break(.same))
651+
}
652+
}
653+
643654
let newlines: NewlineBehavior =
644655
areBracesCompletelyEmpty(node, contentsKeyPath: \.cases) ? .elective : .soft
645656
before(node.rightBrace, tokens: .break(.same, size: 0, newlines: newlines))

Tests/SwiftFormatPrettyPrintTests/SwitchStmtTests.swift

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import SwiftFormatConfiguration
2+
13
final class SwitchStmtTests: PrettyPrintTestCase {
24
func testBasicSwitch() {
35
let input =
@@ -332,4 +334,97 @@ final class SwitchStmtTests: PrettyPrintTestCase {
332334

333335
assertPrettyPrintEqual(input: input, expected: expected, linelength: 20)
334336
}
337+
338+
func testConditionalCases() {
339+
let input =
340+
"""
341+
switch foo {
342+
#if CONDITION_A
343+
case bar:
344+
callForBar()
345+
#endif
346+
case baz:
347+
callForBaz()
348+
}
349+
switch foo2 {
350+
case bar2:
351+
callForBar()
352+
#if CONDITION_B
353+
case baz2:
354+
callForBaz()
355+
#endif
356+
}
357+
"""
358+
359+
let expected =
360+
"""
361+
switch foo {
362+
#if CONDITION_A
363+
case bar:
364+
callForBar()
365+
#endif
366+
case baz:
367+
callForBaz()
368+
}
369+
switch foo2 {
370+
case bar2:
371+
callForBar()
372+
#if CONDITION_B
373+
case baz2:
374+
callForBaz()
375+
#endif
376+
}
377+
378+
"""
379+
380+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 40)
381+
}
382+
383+
func testConditionalCasesIndenting() {
384+
let input =
385+
"""
386+
switch foo {
387+
#if CONDITION_A
388+
case bar:
389+
callForBar()
390+
#endif
391+
case baz:
392+
callForBaz()
393+
}
394+
switch foo2 {
395+
case bar2:
396+
callForBar()
397+
#if CONDITION_B
398+
case baz2:
399+
callForBaz()
400+
#endif
401+
}
402+
"""
403+
404+
let expected =
405+
"""
406+
switch foo {
407+
#if CONDITION_A
408+
case bar:
409+
callForBar()
410+
#endif
411+
case baz:
412+
callForBaz()
413+
}
414+
switch foo2 {
415+
case bar2:
416+
callForBar()
417+
#if CONDITION_B
418+
case baz2:
419+
callForBaz()
420+
#endif
421+
}
422+
423+
"""
424+
425+
var configuration = Configuration()
426+
configuration.indentSwitchCaseLabels = true
427+
assertPrettyPrintEqual(
428+
input: input, expected: expected, linelength: 40, configuration: configuration)
429+
}
335430
}

0 commit comments

Comments
 (0)