From 72837ce7f0dd9ebd018dace6f91eead1d5c8340e Mon Sep 17 00:00:00 2001 From: John Sundell Date: Sat, 16 Mar 2019 12:44:02 +0100 Subject: [PATCH] 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. --- Sources/Splash/Grammar/SwiftGrammar.swift | 9 +++++++++ Tests/SplashTests/Tests/LiteralTests.swift | 21 ++++++++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/Sources/Splash/Grammar/SwiftGrammar.swift b/Sources/Splash/Grammar/SwiftGrammar.swift index a73939c..74e40f0 100644 --- a/Sources/Splash/Grammar/SwiftGrammar.swift +++ b/Sources/Splash/Grammar/SwiftGrammar.swift @@ -29,6 +29,7 @@ public struct SwiftGrammar: Grammar { NumberRule(), TypeRule(), CallRule(), + KeyPathRule(), PropertyRule(), DotAccessRule(), 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 { var tokenType: TokenType { return .property } diff --git a/Tests/SplashTests/Tests/LiteralTests.swift b/Tests/SplashTests/Tests/LiteralTests.swift index 052f117..e5fb333 100644 --- a/Tests/SplashTests/Tests/LiteralTests.swift +++ b/Tests/SplashTests/Tests/LiteralTests.swift @@ -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() { XCTAssertTrue(TestCaseVerifier.verifyLinuxTests((type(of: self)).allTests)) } @@ -223,7 +241,8 @@ extension LiteralTests { ("testSingleLineRawStringLiteral", testSingleLineRawStringLiteral), ("testMultiLineRawStringLiteral", testMultiLineRawStringLiteral), ("testDoubleLiteral", testDoubleLiteral), - ("testIntegerLiteralWithSeparators", testIntegerLiteralWithSeparators) + ("testIntegerLiteralWithSeparators", testIntegerLiteralWithSeparators), + ("testKeyPathLiteral", testKeyPathLiteral) ] } }