From 2b9e65e0d86b90bb5c4e9d8f3fcca059b0ae5cf7 Mon Sep 17 00:00:00 2001 From: John Sundell Date: Sun, 31 Mar 2019 22:52:09 +0200 Subject: [PATCH] Fix highlighting for keywords used as generic function parameter labels (#65) This patch fixes highlighting for parameter labels that also match a keyword, when used within a generic function declaration. --- Sources/Splash/Grammar/SwiftGrammar.swift | 2 +- .../SplashTests/Tests/DeclarationTests.swift | 21 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/Sources/Splash/Grammar/SwiftGrammar.swift b/Sources/Splash/Grammar/SwiftGrammar.swift index e88177e..16b2690 100644 --- a/Sources/Splash/Grammar/SwiftGrammar.swift +++ b/Sources/Splash/Grammar/SwiftGrammar.swift @@ -271,7 +271,7 @@ private extension SwiftGrammar { // Don't highlight most keywords when used as a parameter label if !segment.tokens.current.isAny(of: "_", "self", "let", "var", "true", "false") { - guard !previousToken.isAny(of: "(", ",") else { + guard !previousToken.isAny(of: "(", ",", ">(") else { return false } } diff --git a/Tests/SplashTests/Tests/DeclarationTests.swift b/Tests/SplashTests/Tests/DeclarationTests.swift index 00f309a..601b584 100644 --- a/Tests/SplashTests/Tests/DeclarationTests.swift +++ b/Tests/SplashTests/Tests/DeclarationTests.swift @@ -88,6 +88,26 @@ final class DeclarationTests: SyntaxHighlighterTestCase { ]) } + func testGenericFunctionDeclarationWithKeywordArgumentLabel() { + let components = highlighter.highlight("func perform(for object: O) {}") + + XCTAssertEqual(components, [ + .token("func", .keyword), + .whitespace(" "), + .plainText("perform(for"), + .whitespace(" "), + .plainText("object:"), + .whitespace(" "), + .token("O", .type), + .plainText(")"), + .whitespace(" "), + .plainText("{}") + ]) + } + func testGenericFunctionDeclarationWithoutConstraints() { let components = highlighter.highlight("func hello(a: A, b: B)") @@ -857,6 +877,7 @@ extension DeclarationTests { ("testPublicFunctionDeclarationWithDocumentationEndingWithDot", testPublicFunctionDeclarationWithDocumentationEndingWithDot), ("testFunctionDeclarationWithEmptyExternalLabel", testFunctionDeclarationWithEmptyExternalLabel), ("testFunctionDeclarationWithKeywordArgumentLabel", testFunctionDeclarationWithKeywordArgumentLabel), + ("testGenericFunctionDeclarationWithKeywordArgumentLabel", testGenericFunctionDeclarationWithKeywordArgumentLabel), ("testGenericFunctionDeclarationWithoutConstraints", testGenericFunctionDeclarationWithoutConstraints), ("testGenericFunctionDeclarationWithSingleConstraint", testGenericFunctionDeclarationWithSingleConstraint), ("testGenericFunctionDeclarationWithMultipleConstraints", testGenericFunctionDeclarationWithMultipleConstraints),