From d72e816cd81a53fa1d4d7afef9a464b432a7cdb0 Mon Sep 17 00:00:00 2001 From: John Sundell Date: Fri, 8 Mar 2019 22:04:10 +0100 Subject: [PATCH] =?UTF-8?q?Support=20=E2=80=98is=E2=80=99=20keyword?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This change makes Splash support the `is` keyword, for example in type pattern matching in `switch` statements. --- Sources/Splash/Grammar/SwiftGrammar.swift | 2 +- Tests/SplashTests/Tests/StatementTests.swift | 34 ++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/Sources/Splash/Grammar/SwiftGrammar.swift b/Sources/Splash/Grammar/SwiftGrammar.swift index 7ea72bd..fa05fa1 100644 --- a/Sources/Splash/Grammar/SwiftGrammar.swift +++ b/Sources/Splash/Grammar/SwiftGrammar.swift @@ -48,7 +48,7 @@ private extension SwiftGrammar { "#selector", "required", "willSet", "didSet", "lazy", "subscript", "defer", "inout", "while", "continue", "fallthrough", "repeat", "indirect", - "deinit" + "deinit", "is" ] as Set).union(accessControlKeywords) static let accessControlKeywords: Set = [ diff --git a/Tests/SplashTests/Tests/StatementTests.swift b/Tests/SplashTests/Tests/StatementTests.swift index 0e64a92..250bfe8 100644 --- a/Tests/SplashTests/Tests/StatementTests.swift +++ b/Tests/SplashTests/Tests/StatementTests.swift @@ -169,6 +169,39 @@ final class StatementTests: SyntaxHighlighterTestCase { ]) } + func testSwitchStatementWithTypePatternMatching() { + let components = highlighter.highlight(""" + switch variable { + case is MyType: break + default: break + } + """) + + XCTAssertEqual(components, [ + .token("switch", .keyword), + .whitespace(" "), + .plainText("variable"), + .whitespace(" "), + .plainText("{"), + .whitespace("\n"), + .token("case", .keyword), + .whitespace(" "), + .token("is", .keyword), + .whitespace(" "), + .token("MyType", .type), + .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 { }") @@ -272,6 +305,7 @@ extension StatementTests { ("testSwitchStatement", testSwitchStatement), ("testSwitchStatementWithAssociatedValues", testSwitchStatementWithAssociatedValues), ("testSwitchStatementWithFallthrough", testSwitchStatementWithFallthrough), + ("testSwitchStatementWithTypePatternMatching", testSwitchStatementWithTypePatternMatching), ("testForStatementWithStaticProperty", testForStatementWithStaticProperty), ("testForStatementWithContinue", testForStatementWithContinue), ("testRepeatWhileStatement", testRepeatWhileStatement)