Merge pull request #11 from JohnSundell/subscript-declarations

Correctly highlight subscript declarations
This commit is contained in:
John Sundell 2018-08-27 18:44:28 +02:00 committed by GitHub
commit 0e90f5d86c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 51 additions and 3 deletions

View File

@ -47,7 +47,7 @@ private extension SwiftGrammar {
"super", "self", "set", "true", "false", "nil",
"override", "where", "_", "default", "break",
"#selector", "required", "willSet", "didSet",
"lazy"
"lazy", "subscript"
]
struct PreprocessingRule: SyntaxRule {
@ -162,6 +162,12 @@ private extension SwiftGrammar {
return false
}
// Subscripting is a bit of an edge case, since it's the only keyword
// that looks like a function call, so we need to handle it explicitly
guard segment.tokens.current != "subscript" else {
return false
}
if let previousToken = segment.tokens.previous {
guard !keywordsToAvoid.contains(previousToken) else {
return false

View File

@ -22,7 +22,11 @@ struct TestCaseVerifier<Case: XCTestCase> {
}
guard testNames.contains(name) else {
XCTFail("Test case \(Case.self) does not include test \(name) on Linux")
XCTFail("""
Test case \(Case.self) does not include test \(name) on Linux.
Please add it to the test case's 'allTests' array.
""")
return false
}
}

View File

@ -424,6 +424,43 @@ final class DeclarationTests: SyntaxHighlighterTestCase {
])
}
func testSubscriptDeclaration() {
let components = highlighter.highlight("""
extension Collection {
subscript(key: Key) -> Value? { return nil }
}
""")
XCTAssertEqual(components, [
.token("extension", .keyword),
.whitespace(" "),
.token("Collection", .type),
.whitespace(" "),
.plainText("{"),
.whitespace("\n "),
.token("subscript", .keyword),
.plainText("(key:"),
.whitespace(" "),
.token("Key", .type),
.plainText(")"),
.whitespace(" "),
.plainText("->"),
.whitespace(" "),
.token("Value", .type),
.plainText("?"),
.whitespace(" "),
.plainText("{"),
.whitespace(" "),
.token("return", .keyword),
.whitespace(" "),
.token("nil", .keyword),
.whitespace(" "),
.plainText("}"),
.whitespace("\n"),
.plainText("}")
])
}
func testAllTestsRunOnLinux() {
XCTAssertTrue(TestCaseVerifier.verifyLinuxTests((type(of: self)).allTests))
}
@ -449,7 +486,8 @@ extension DeclarationTests {
("testLazyPropertyDeclaration", testLazyPropertyDeclaration),
("testGenericPropertyDeclaration", testGenericPropertyDeclaration),
("testPropertyDeclarationWithWillSet", testPropertyDeclarationWithWillSet),
("testPropertyDeclarationWithDidSet", testPropertyDeclarationWithDidSet)
("testPropertyDeclarationWithDidSet", testPropertyDeclarationWithDidSet),
("testSubscriptDeclaration", testSubscriptDeclaration)
]
}
}