From 2e3f17c2d09689c8bf175c4a84ff7f2ad3353301 Mon Sep 17 00:00:00 2001 From: John Sundell Date: Wed, 8 Jun 2022 22:13:57 +0200 Subject: [PATCH] Correctly highlight the 'any' keyword when used within other type references (#130) Treat it the same way as the `some` keyword. --- Sources/Splash/Grammar/SwiftGrammar.swift | 2 +- .../SplashTests/Tests/DeclarationTests.swift | 38 +++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/Sources/Splash/Grammar/SwiftGrammar.swift b/Sources/Splash/Grammar/SwiftGrammar.swift index fcf1944..79a98d5 100644 --- a/Sources/Splash/Grammar/SwiftGrammar.swift +++ b/Sources/Splash/Grammar/SwiftGrammar.swift @@ -342,7 +342,7 @@ private extension SwiftGrammar { return true } - if segment.tokens.current == "some" { + if segment.tokens.current.isAny(of: "some", "any") { guard segment.tokens.previous != "case" else { return false } diff --git a/Tests/SplashTests/Tests/DeclarationTests.swift b/Tests/SplashTests/Tests/DeclarationTests.swift index 8f1e402..806b513 100644 --- a/Tests/SplashTests/Tests/DeclarationTests.swift +++ b/Tests/SplashTests/Tests/DeclarationTests.swift @@ -1119,6 +1119,44 @@ final class DeclarationTests: SyntaxHighlighterTestCase { ]) } + func testFunctionDeclarationWithGenericAnyParameter() { + let components = highlighter.highlight("func process(collection: any Collection)") + + XCTAssertEqual(components, [ + .token("func", .keyword), + .whitespace(" "), + .plainText("process(collection:"), + .whitespace(" "), + .token("any", .keyword), + .whitespace(" "), + .token("Collection", .type), + .plainText("<"), + .token("any", .keyword), + .whitespace(" "), + .token("Value", .type), + .plainText(">)") + ]) + } + + func testFunctionDeclarationWithAnyDictionary() { + let components = highlighter.highlight("func process(dictionary: [String: any Value])") + + XCTAssertEqual(components, [ + .token("func", .keyword), + .whitespace(" "), + .plainText("process(dictionary:"), + .whitespace(" "), + .plainText("["), + .token("String", .type), + .plainText(":"), + .whitespace(" "), + .token("any", .keyword), + .whitespace(" "), + .token("Value", .type), + .plainText("])") + ]) + } + func testPrefixFunctionDeclaration() { let components = highlighter.highlight("prefix func !(rhs: Bool) -> Bool { !rhs }")