Correctly highlight subscript declarations

This patch fixes syntax highlighting when declaring
a subscript:

```
struct MyStruct {
	subscript(key: Key) -> Value? {
		…
	}
}
```
This commit is contained in:
John Sundell 2018-08-27 18:32:22 +02:00
parent a86694802e
commit 1faa928214
3 changed files with 51 additions and 3 deletions

View File

@ -47,7 +47,7 @@ private extension SwiftGrammar {
"super", "self", "set", "true", "false", "nil", "super", "self", "set", "true", "false", "nil",
"override", "where", "_", "default", "break", "override", "where", "_", "default", "break",
"#selector", "required", "willSet", "didSet", "#selector", "required", "willSet", "didSet",
"lazy" "lazy", "subscript"
] ]
struct PreprocessingRule: SyntaxRule { struct PreprocessingRule: SyntaxRule {
@ -162,6 +162,12 @@ private extension SwiftGrammar {
return false 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 { if let previousToken = segment.tokens.previous {
guard !keywordsToAvoid.contains(previousToken) else { guard !keywordsToAvoid.contains(previousToken) else {
return false return false

View File

@ -22,7 +22,11 @@ struct TestCaseVerifier<Case: XCTestCase> {
} }
guard testNames.contains(name) else { 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 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() { func testAllTestsRunOnLinux() {
XCTAssertTrue(TestCaseVerifier.verifyLinuxTests((type(of: self)).allTests)) XCTAssertTrue(TestCaseVerifier.verifyLinuxTests((type(of: self)).allTests))
} }
@ -449,7 +486,8 @@ extension DeclarationTests {
("testLazyPropertyDeclaration", testLazyPropertyDeclaration), ("testLazyPropertyDeclaration", testLazyPropertyDeclaration),
("testGenericPropertyDeclaration", testGenericPropertyDeclaration), ("testGenericPropertyDeclaration", testGenericPropertyDeclaration),
("testPropertyDeclarationWithWillSet", testPropertyDeclarationWithWillSet), ("testPropertyDeclarationWithWillSet", testPropertyDeclarationWithWillSet),
("testPropertyDeclarationWithDidSet", testPropertyDeclarationWithDidSet) ("testPropertyDeclarationWithDidSet", testPropertyDeclarationWithDidSet),
("testSubscriptDeclaration", testSubscriptDeclaration)
] ]
} }
} }