Skip to content

Commit bc20ac9

Browse files
authored
Support highlighting custom compiler directives (#88)
Add support for highlighting the `#warning` and `#error` directives.
1 parent 66714d6 commit bc20ac9

File tree

3 files changed

+34
-5
lines changed

3 files changed

+34
-5
lines changed

Sources/Splash/Extensions/Sequence/Sequence+AnyOf.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ internal extension Sequence where Element: Equatable {
1111
return contains(anyOf: candidates)
1212
}
1313

14-
func contains(anyOf candidates: [Element]) -> Bool {
14+
func contains<S: Sequence>(anyOf candidates: S) -> Bool where S.Element == Element {
1515
for candidate in candidates {
1616
if contains(candidate) {
1717
return true

Sources/Splash/Grammar/SwiftGrammar.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,19 @@ private extension SwiftGrammar {
8080

8181
struct PreprocessingRule: SyntaxRule {
8282
var tokenType: TokenType { return .preprocessing }
83-
private let tokens = ["#if", "#endif", "#elseif", "#else"]
83+
private let controlFlowTokens: Set<String> = ["#if", "#endif", "#elseif", "#else"]
84+
private let directiveTokens: Set<String> = ["#warning", "#error"]
8485

8586
func matches(_ segment: Segment) -> Bool {
86-
if segment.tokens.current.isAny(of: tokens) {
87+
if segment.tokens.current.isAny(of: controlFlowTokens) {
8788
return true
8889
}
8990

90-
return segment.tokens.onSameLine.contains(anyOf: tokens)
91+
if segment.tokens.current.isAny(of: directiveTokens) {
92+
return true
93+
}
94+
95+
return segment.tokens.onSameLine.contains(anyOf: controlFlowTokens)
9196
}
9297
}
9398

Tests/SplashTests/Tests/PreprocessorTests.swift

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,28 @@ final class PreprocessorTests: SyntaxHighlighterTestCase {
8080
])
8181
}
8282

83+
func testWarningDirective() {
84+
let components = highlighter.highlight(#"#warning("Hey!")"#)
85+
86+
XCTAssertEqual(components, [
87+
.token("#warning", .preprocessing),
88+
.plainText("("),
89+
.token(#""Hey!""#, .string),
90+
.plainText(")")
91+
])
92+
}
93+
94+
func testErrorDirective() {
95+
let components = highlighter.highlight(#"#error("No!")"#)
96+
97+
XCTAssertEqual(components, [
98+
.token("#error", .preprocessing),
99+
.plainText("("),
100+
.token(#""No!""#, .string),
101+
.plainText(")")
102+
])
103+
}
104+
83105
func testAllTestsRunOnLinux() {
84106
XCTAssertTrue(TestCaseVerifier.verifyLinuxTests((type(of: self)).allTests))
85107
}
@@ -91,7 +113,9 @@ extension PreprocessorTests {
91113
("testPreprocessing", testPreprocessing),
92114
("testSelector", testSelector),
93115
("testFunctionAttribute", testFunctionAttribute),
94-
("testAvailabilityCheck", testAvailabilityCheck)
116+
("testAvailabilityCheck", testAvailabilityCheck),
117+
("testWarningDirective", testWarningDirective),
118+
("testErrorDirective", testErrorDirective)
95119
]
96120
}
97121
}

0 commit comments

Comments
 (0)