From 2cdc99ce6017c184b406dc224eaf0bc0b0cbe7a5 Mon Sep 17 00:00:00 2001 From: John Sundell Date: Sat, 16 Mar 2019 12:41:53 +0100 Subject: [PATCH] Correctly highlight generic parameters (#59) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This patch makes Splash highlight generics that appear as part of a generic function’s parameter list in a correct way. --- Sources/Splash/Grammar/SwiftGrammar.swift | 5 ++++ .../SplashTests/Tests/DeclarationTests.swift | 28 +++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/Sources/Splash/Grammar/SwiftGrammar.swift b/Sources/Splash/Grammar/SwiftGrammar.swift index df6225c..a73939c 100644 --- a/Sources/Splash/Grammar/SwiftGrammar.swift +++ b/Sources/Splash/Grammar/SwiftGrammar.swift @@ -326,6 +326,11 @@ private extension SwiftGrammar { foundOpeningBracket = true } + // Handling generic lists for parameters, rather than declarations + if foundOpeningBracket && token == ":" { + return true + } + guard !declarationKeywords.contains(token) else { // If it turns out that we weren't in fact inside of a generic // declaration, (lacking "<"), then highlight the type as normal. diff --git a/Tests/SplashTests/Tests/DeclarationTests.swift b/Tests/SplashTests/Tests/DeclarationTests.swift index 664d274..00f309a 100644 --- a/Tests/SplashTests/Tests/DeclarationTests.swift +++ b/Tests/SplashTests/Tests/DeclarationTests.swift @@ -150,6 +150,33 @@ final class DeclarationTests: SyntaxHighlighterTestCase { ]) } + func testGenericFunctionDeclarationWithGenericParameter() { + let components = highlighter.highlight("func value(at keyPath: KeyPath) -> T? {}") + + XCTAssertEqual(components, [ + .token("func", .keyword), + .whitespace(" "), + .plainText("value(at"), + .whitespace(" "), + .plainText("keyPath:"), + .whitespace(" "), + .token("KeyPath", .type), + .plainText("<"), + .token("Element", .type), + .plainText(","), + .whitespace(" "), + .token("T", .type), + .plainText(">)"), + .whitespace(" "), + .plainText("->"), + .whitespace(" "), + .token("T", .type), + .plainText("?"), + .whitespace(" "), + .plainText("{}") + ]) + } + func testGenericStructDeclaration() { let components = highlighter.highlight("struct MyStruct {}") @@ -833,6 +860,7 @@ extension DeclarationTests { ("testGenericFunctionDeclarationWithoutConstraints", testGenericFunctionDeclarationWithoutConstraints), ("testGenericFunctionDeclarationWithSingleConstraint", testGenericFunctionDeclarationWithSingleConstraint), ("testGenericFunctionDeclarationWithMultipleConstraints", testGenericFunctionDeclarationWithMultipleConstraints), + ("testGenericFunctionDeclarationWithGenericParameter", testGenericFunctionDeclarationWithGenericParameter), ("testGenericStructDeclaration", testGenericStructDeclaration), ("testClassDeclaration", testClassDeclaration), ("testCompactClassDeclarationWithInitializer", testCompactClassDeclarationWithInitializer),