Support highlighting custom compiler directives (#88)

Add support for highlighting the `#warning` and `#error` directives.
This commit is contained in:
John Sundell 2019-11-04 11:31:08 +01:00 committed by GitHub
parent 66714d6867
commit bc20ac969c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 5 deletions

View File

@ -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

View File

@ -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)
}
}

View File

@ -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))
}
@ -91,7 +113,9 @@ extension PreprocessorTests {
("testPreprocessing", testPreprocessing),
("testSelector", testSelector),
("testFunctionAttribute", testFunctionAttribute),
("testAvailabilityCheck", testAvailabilityCheck)
("testAvailabilityCheck", testAvailabilityCheck),
("testWarningDirective", testWarningDirective),
("testErrorDirective", testErrorDirective)
]
}
}