Skip to content

Commit

Permalink
Support highlighting custom compiler directives (#88)
Browse files Browse the repository at this point in the history
Add support for highlighting the `#warning` and `#error` directives.
  • Loading branch information
JohnSundell authored Nov 4, 2019
1 parent 66714d6 commit bc20ac9
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Sources/Splash/Extensions/Sequence/Sequence+AnyOf.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ internal extension Sequence where Element: Equatable {
return contains(anyOf: candidates)
}

func contains(anyOf candidates: [Element]) -> Bool {
func contains<S: Sequence>(anyOf candidates: S) -> Bool where S.Element == Element {
for candidate in candidates {
if contains(candidate) {
return true
Expand Down
11 changes: 8 additions & 3 deletions Sources/Splash/Grammar/SwiftGrammar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,19 @@ private extension SwiftGrammar {

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

func matches(_ segment: Segment) -> Bool {
if segment.tokens.current.isAny(of: tokens) {
if segment.tokens.current.isAny(of: controlFlowTokens) {
return true
}

return segment.tokens.onSameLine.contains(anyOf: tokens)
if segment.tokens.current.isAny(of: directiveTokens) {
return true
}

return segment.tokens.onSameLine.contains(anyOf: controlFlowTokens)
}
}

Expand Down
26 changes: 25 additions & 1 deletion Tests/SplashTests/Tests/PreprocessorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,28 @@ final class PreprocessorTests: SyntaxHighlighterTestCase {
])
}

func testWarningDirective() {
let components = highlighter.highlight(#"#warning("Hey!")"#)

XCTAssertEqual(components, [
.token("#warning", .preprocessing),
.plainText("("),
.token(#""Hey!""#, .string),
.plainText(")")
])
}

func testErrorDirective() {
let components = highlighter.highlight(#"#error("No!")"#)

XCTAssertEqual(components, [
.token("#error", .preprocessing),
.plainText("("),
.token(#""No!""#, .string),
.plainText(")")
])
}

func testAllTestsRunOnLinux() {
XCTAssertTrue(TestCaseVerifier.verifyLinuxTests((type(of: self)).allTests))
}
Expand All @@ -91,7 +113,9 @@ extension PreprocessorTests {
("testPreprocessing", testPreprocessing),
("testSelector", testSelector),
("testFunctionAttribute", testFunctionAttribute),
("testAvailabilityCheck", testAvailabilityCheck)
("testAvailabilityCheck", testAvailabilityCheck),
("testWarningDirective", testWarningDirective),
("testErrorDirective", testErrorDirective)
]
}
}

0 comments on commit bc20ac9

Please sign in to comment.