From ca9a1b7bff35381fe5ae9fe2ec9333cc5f2a586c Mon Sep 17 00:00:00 2001 From: John Sundell Date: Mon, 25 May 2020 01:18:22 +0200 Subject: [PATCH] Correctly highlight subclasses inheriting from a generic superclass (#108) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This change fixes syntax highlighting in situations when a subclass is inheriting from a superclass that’s generic. --- Sources/Splash/Grammar/SwiftGrammar.swift | 2 +- Tests/SplashTests/Tests/DeclarationTests.swift | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/Sources/Splash/Grammar/SwiftGrammar.swift b/Sources/Splash/Grammar/SwiftGrammar.swift index 8d94078..5ad2afa 100644 --- a/Sources/Splash/Grammar/SwiftGrammar.swift +++ b/Sources/Splash/Grammar/SwiftGrammar.swift @@ -419,7 +419,7 @@ private extension SwiftGrammar { } // Handling generic lists for parameters, rather than declarations - if foundOpeningBracket && token == ":" { + if foundOpeningBracket && token.isAny(of: ":", ">:") { return true } diff --git a/Tests/SplashTests/Tests/DeclarationTests.swift b/Tests/SplashTests/Tests/DeclarationTests.swift index d44c05f..d6b1fef 100644 --- a/Tests/SplashTests/Tests/DeclarationTests.swift +++ b/Tests/SplashTests/Tests/DeclarationTests.swift @@ -368,6 +368,23 @@ final class DeclarationTests: SyntaxHighlighterTestCase { ]) } + func testGenericSubclassDeclaration() { + let components = highlighter.highlight("class Promise: Future {}") + + XCTAssertEqual(components, [ + .token("class", .keyword), + .whitespace(" "), + .plainText("Promise:"), + .whitespace(" "), + .token("Future", .type), + .plainText("<"), + .token("Value", .type), + .plainText(">"), + .whitespace(" "), + .plainText("{}") + ]) + } + func testProtocolDeclaration() { let components = highlighter.highlight(""" protocol Hello { @@ -1161,6 +1178,7 @@ extension DeclarationTests { ("testClassDeclarationWithDeinit", testClassDeclarationWithDeinit), ("testClassDeclarationWithMultipleProtocolConformances", testClassDeclarationWithMultipleProtocolConformances), ("testSubclassDeclaration", testSubclassDeclaration), + ("testGenericSubclassDeclaration", testGenericSubclassDeclaration), ("testProtocolDeclaration", testProtocolDeclaration), ("testProtocolDeclarationWithAssociatedTypes", testProtocolDeclarationWithAssociatedTypes), ("testExtensionDeclaration", testExtensionDeclaration),