From a86694802e1ede4da69853da231dcc11f6d8149b Mon Sep 17 00:00:00 2001 From: John Sundell Date: Mon, 27 Aug 2018 18:14:17 +0200 Subject: [PATCH] Highlight enums used as subscripting keys This patch fixes highlighting when an enum is used as a dictionary key, like this: ``` let value = dictionary[.key] ``` --- Sources/Splash/Grammar/SwiftGrammar.swift | 2 +- Tests/SplashTests/Tests/EnumTests.swift | 13 ++++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/Sources/Splash/Grammar/SwiftGrammar.swift b/Sources/Splash/Grammar/SwiftGrammar.swift index e4266f3..891084a 100644 --- a/Sources/Splash/Grammar/SwiftGrammar.swift +++ b/Sources/Splash/Grammar/SwiftGrammar.swift @@ -262,7 +262,7 @@ private extension SwiftGrammar { var tokenType: TokenType { return .dotAccess } func matches(_ segment: Segment) -> Bool { - guard segment.tokens.previous.isAny(of: ".", "(.") else { + guard segment.tokens.previous.isAny(of: ".", "(.", "[.") else { return false } diff --git a/Tests/SplashTests/Tests/EnumTests.swift b/Tests/SplashTests/Tests/EnumTests.swift index be44b62..5b8f403 100644 --- a/Tests/SplashTests/Tests/EnumTests.swift +++ b/Tests/SplashTests/Tests/EnumTests.swift @@ -48,6 +48,16 @@ final class EnumTests: SyntaxHighlighterTestCase { ]) } + func testUsingEnumInSubscript() { + let components = highlighter.highlight("dictionary[.key]") + + XCTAssertEqual(components, [ + .plainText("dictionary[."), + .token("key", .dotAccess), + .plainText("]") + ]) + } + func testAllTestsRunOnLinux() { XCTAssertTrue(TestCaseVerifier.verifyLinuxTests((type(of: self)).allTests)) } @@ -58,7 +68,8 @@ extension EnumTests { return [ ("testEnumDotSyntaxInAssignment", testEnumDotSyntaxInAssignment), ("testEnumDotSyntaxAsArgument", testEnumDotSyntaxAsArgument), - ("testEnumDotSyntaxWithAssociatedValue", testEnumDotSyntaxWithAssociatedValue) + ("testEnumDotSyntaxWithAssociatedValue", testEnumDotSyntaxWithAssociatedValue), + ("testUsingEnumInSubscript", testUsingEnumInSubscript) ] } }