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),