From d74074f40976f8a6cb26e6aefec370fb43a47ccc Mon Sep 17 00:00:00 2001 From: John Sundell Date: Sat, 9 Mar 2019 13:02:33 +0100 Subject: [PATCH] Support switch statements with nil pattern matching MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This change adds support for highlighting `nil` when it occurs inside of a `switch` statement’s `case`. --- Sources/Splash/Grammar/SwiftGrammar.swift | 9 +++++ Tests/SplashTests/Tests/StatementTests.swift | 40 ++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/Sources/Splash/Grammar/SwiftGrammar.swift b/Sources/Splash/Grammar/SwiftGrammar.swift index fa05fa1..3fecf9c 100644 --- a/Sources/Splash/Grammar/SwiftGrammar.swift +++ b/Sources/Splash/Grammar/SwiftGrammar.swift @@ -221,6 +221,15 @@ private extension SwiftGrammar { func matches(_ segment: Segment) -> Bool { if segment.tokens.next == ":" { + // Nil pattern matching inside of a switch statement case + if segment.tokens.current == "nil" { + guard let previousToken = segment.tokens.previous else { + return false + } + + return previousToken.isAny(of: "case", ",") + } + guard segment.tokens.current == "default" else { return false } diff --git a/Tests/SplashTests/Tests/StatementTests.swift b/Tests/SplashTests/Tests/StatementTests.swift index 250bfe8..1ab4150 100644 --- a/Tests/SplashTests/Tests/StatementTests.swift +++ b/Tests/SplashTests/Tests/StatementTests.swift @@ -202,6 +202,45 @@ final class StatementTests: SyntaxHighlighterTestCase { ]) } + func testSwitchStatementWithOptional() { + let components = highlighter.highlight(""" + switch anOptional { + case nil: break + case "value"?: break + default: break + } + """) + + XCTAssertEqual(components, [ + .token("switch", .keyword), + .whitespace(" "), + .plainText("anOptional"), + .whitespace(" "), + .plainText("{"), + .whitespace("\n"), + .token("case", .keyword), + .whitespace(" "), + .token("nil", .keyword), + .plainText(":"), + .whitespace(" "), + .token("break", .keyword), + .whitespace("\n"), + .token("case", .keyword), + .whitespace(" "), + .token("\"value\"", .string), + .plainText("?:"), + .whitespace(" "), + .token("break", .keyword), + .whitespace("\n"), + .token("default", .keyword), + .plainText(":"), + .whitespace(" "), + .token("break", .keyword), + .whitespace("\n"), + .plainText("}") + ]) + } + func testForStatementWithStaticProperty() { let components = highlighter.highlight("for value in Enum.allCases { }") @@ -306,6 +345,7 @@ extension StatementTests { ("testSwitchStatementWithAssociatedValues", testSwitchStatementWithAssociatedValues), ("testSwitchStatementWithFallthrough", testSwitchStatementWithFallthrough), ("testSwitchStatementWithTypePatternMatching", testSwitchStatementWithTypePatternMatching), + ("testSwitchStatementWithOptional", testSwitchStatementWithOptional), ("testForStatementWithStaticProperty", testForStatementWithStaticProperty), ("testForStatementWithContinue", testForStatementWithContinue), ("testRepeatWhileStatement", testRepeatWhileStatement)