From f9ad32a0a6f9924b6be76b528b5e8c0d29f57a12 Mon Sep 17 00:00:00 2001 From: John Sundell Date: Fri, 8 Mar 2019 18:43:12 +0100 Subject: [PATCH] Correctly highlight associated types This patch makes Splash correctly highlight associated types within protocol declarations. Like other declarations, typed declared using the `associatedtype` keyword should not be highlighted. --- Sources/Splash/Grammar/SwiftGrammar.swift | 3 +- .../SplashTests/Tests/DeclarationTests.swift | 32 +++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/Sources/Splash/Grammar/SwiftGrammar.swift b/Sources/Splash/Grammar/SwiftGrammar.swift index 8e0570e..314ada7 100644 --- a/Sources/Splash/Grammar/SwiftGrammar.swift +++ b/Sources/Splash/Grammar/SwiftGrammar.swift @@ -236,7 +236,8 @@ private extension SwiftGrammar { private let declarationKeywords: Set = [ "class", "struct", "enum", "func", - "protocol", "typealias", "import" + "protocol", "typealias", "import", + "associatedtype" ] func matches(_ segment: Segment) -> Bool { diff --git a/Tests/SplashTests/Tests/DeclarationTests.swift b/Tests/SplashTests/Tests/DeclarationTests.swift index feb961f..12dda13 100644 --- a/Tests/SplashTests/Tests/DeclarationTests.swift +++ b/Tests/SplashTests/Tests/DeclarationTests.swift @@ -291,6 +291,37 @@ final class DeclarationTests: SyntaxHighlighterTestCase { ]) } + func testProtocolDeclarationWithAssociatedTypes() { + let components = highlighter.highlight(""" + protocol Task { + associatedtype Input + associatedtype Error: Swift.Error + } + """) + + XCTAssertEqual(components, [ + .token("protocol", .keyword), + .whitespace(" "), + .plainText("Task"), + .whitespace(" "), + .plainText("{"), + .whitespace("\n "), + .token("associatedtype", .keyword), + .whitespace(" "), + .plainText("Input"), + .whitespace("\n "), + .token("associatedtype", .keyword), + .whitespace(" "), + .plainText("Error:"), + .whitespace(" "), + .token("Swift", .type), + .plainText("."), + .token("Error", .type), + .whitespace("\n"), + .plainText("}") + ]) + } + func testExtensionDeclaration() { let components = highlighter.highlight("extension UIViewController { }") @@ -636,6 +667,7 @@ extension DeclarationTests { ("testClassDeclarationWithMultipleProtocolConformances", testClassDeclarationWithMultipleProtocolConformances), ("testSubclassDeclaration", testSubclassDeclaration), ("testProtocolDeclaration", testProtocolDeclaration), + ("testProtocolDeclarationWithAssociatedTypes", testProtocolDeclarationWithAssociatedTypes), ("testExtensionDeclaration", testExtensionDeclaration), ("testExtensionDeclarationWithConstraint", testExtensionDeclarationWithConstraint), ("testLazyPropertyDeclaration", testLazyPropertyDeclaration),