Skip to content

Commit 21bfa7d

Browse files
authored
Merge pull request swiftlang#130 from dabelknap/compiler-control
Add support for #if compiler control statements
2 parents f7f9d4e + 6b31bb7 commit 21bfa7d

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

Sources/PrettyPrint/TokenStreamCreator.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,12 @@ private final class TokenStreamCreator: SyntaxVisitor {
268268
super.visit(node)
269269
}
270270

271+
override func visit(_ node: IfConfigClauseSyntax) {
272+
after(node.poundKeyword, tokens: .break)
273+
after(node.condition?.lastToken, tokens: .newline)
274+
super.visit(node)
275+
}
276+
271277
override func visit(_ node: MemberDeclBlockSyntax) {
272278
for i in 0..<(node.members.count - 1) {
273279
after(node.members[i].lastToken, tokens: .newline)
@@ -318,10 +324,6 @@ private final class TokenStreamCreator: SyntaxVisitor {
318324
super.visit(node)
319325
}
320326

321-
override func visit(_ node: IfConfigClauseSyntax) {
322-
super.visit(node)
323-
}
324-
325327
override func visit(_ node: EnumCaseElementSyntax) {
326328
after(node.trailingComma, tokens: .break(offset: 2))
327329
super.visit(node)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
public class CompilerControlTests: PrettyPrintTestCase {
2+
public func testConditionalCompilation() {
3+
let input =
4+
"""
5+
#if swift(>=4.0)
6+
print("Stuff")
7+
#endif
8+
#if swift(>=4.0)
9+
print("Stuff")
10+
#elseif compiler(>=3.0)
11+
print("More Stuff")
12+
print("Another Line")
13+
#endif
14+
"""
15+
16+
let expected =
17+
"""
18+
#if swift(>=4.0)
19+
print("Stuff")
20+
#endif
21+
#if swift(>=4.0)
22+
print("Stuff")
23+
#elseif compiler(>=3.0)
24+
print("More Stuff")
25+
print("Another Line")
26+
#endif
27+
28+
"""
29+
30+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 45)
31+
}
32+
}

0 commit comments

Comments
 (0)