Correctly highlight the root level of a key path (#60)

When key path literals are used, Splash will now correctly highlight the
root level of that key path as a property, rather than as plain text.
This commit is contained in:
John Sundell 2019-03-16 12:44:02 +01:00 committed by GitHub
parent 2cdc99ce60
commit 72837ce7f0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 1 deletions

View File

@ -29,6 +29,7 @@ public struct SwiftGrammar: Grammar {
NumberRule(), NumberRule(),
TypeRule(), TypeRule(),
CallRule(), CallRule(),
KeyPathRule(),
PropertyRule(), PropertyRule(),
DotAccessRule(), DotAccessRule(),
KeywordRule() KeywordRule()
@ -371,6 +372,14 @@ private extension SwiftGrammar {
} }
} }
struct KeyPathRule: SyntaxRule {
var tokenType: TokenType { return .property }
func matches(_ segment: Segment) -> Bool {
return segment.tokens.previous == "\\."
}
}
struct PropertyRule: SyntaxRule { struct PropertyRule: SyntaxRule {
var tokenType: TokenType { return .property } var tokenType: TokenType { return .property }

View File

@ -205,6 +205,24 @@ final class LiteralTests: SyntaxHighlighterTestCase {
]) ])
} }
func testKeyPathLiteral() {
let components = highlighter.highlight("let value = object[keyPath: \\.property]")
XCTAssertEqual(components, [
.token("let", .keyword),
.whitespace(" "),
.plainText("value"),
.whitespace(" "),
.plainText("="),
.whitespace(" "),
.plainText("object[keyPath:"),
.whitespace(" "),
.plainText("\\."),
.token("property", .property),
.plainText("]")
])
}
func testAllTestsRunOnLinux() { func testAllTestsRunOnLinux() {
XCTAssertTrue(TestCaseVerifier.verifyLinuxTests((type(of: self)).allTests)) XCTAssertTrue(TestCaseVerifier.verifyLinuxTests((type(of: self)).allTests))
} }
@ -223,7 +241,8 @@ extension LiteralTests {
("testSingleLineRawStringLiteral", testSingleLineRawStringLiteral), ("testSingleLineRawStringLiteral", testSingleLineRawStringLiteral),
("testMultiLineRawStringLiteral", testMultiLineRawStringLiteral), ("testMultiLineRawStringLiteral", testMultiLineRawStringLiteral),
("testDoubleLiteral", testDoubleLiteral), ("testDoubleLiteral", testDoubleLiteral),
("testIntegerLiteralWithSeparators", testIntegerLiteralWithSeparators) ("testIntegerLiteralWithSeparators", testIntegerLiteralWithSeparators),
("testKeyPathLiteral", testKeyPathLiteral)
] ]
} }
} }