Skip to content

Commit 72837ce

Browse files
authored
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.
1 parent 2cdc99c commit 72837ce

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

Sources/Splash/Grammar/SwiftGrammar.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public struct SwiftGrammar: Grammar {
2929
NumberRule(),
3030
TypeRule(),
3131
CallRule(),
32+
KeyPathRule(),
3233
PropertyRule(),
3334
DotAccessRule(),
3435
KeywordRule()
@@ -371,6 +372,14 @@ private extension SwiftGrammar {
371372
}
372373
}
373374

375+
struct KeyPathRule: SyntaxRule {
376+
var tokenType: TokenType { return .property }
377+
378+
func matches(_ segment: Segment) -> Bool {
379+
return segment.tokens.previous == "\\."
380+
}
381+
}
382+
374383
struct PropertyRule: SyntaxRule {
375384
var tokenType: TokenType { return .property }
376385

Tests/SplashTests/Tests/LiteralTests.swift

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,24 @@ final class LiteralTests: SyntaxHighlighterTestCase {
205205
])
206206
}
207207

208+
func testKeyPathLiteral() {
209+
let components = highlighter.highlight("let value = object[keyPath: \\.property]")
210+
211+
XCTAssertEqual(components, [
212+
.token("let", .keyword),
213+
.whitespace(" "),
214+
.plainText("value"),
215+
.whitespace(" "),
216+
.plainText("="),
217+
.whitespace(" "),
218+
.plainText("object[keyPath:"),
219+
.whitespace(" "),
220+
.plainText("\\."),
221+
.token("property", .property),
222+
.plainText("]")
223+
])
224+
}
225+
208226
func testAllTestsRunOnLinux() {
209227
XCTAssertTrue(TestCaseVerifier.verifyLinuxTests((type(of: self)).allTests))
210228
}
@@ -223,7 +241,8 @@ extension LiteralTests {
223241
("testSingleLineRawStringLiteral", testSingleLineRawStringLiteral),
224242
("testMultiLineRawStringLiteral", testMultiLineRawStringLiteral),
225243
("testDoubleLiteral", testDoubleLiteral),
226-
("testIntegerLiteralWithSeparators", testIntegerLiteralWithSeparators)
244+
("testIntegerLiteralWithSeparators", testIntegerLiteralWithSeparators),
245+
("testKeyPathLiteral", testKeyPathLiteral)
227246
]
228247
}
229248
}

0 commit comments

Comments
 (0)