From ac9b51201353c601a671efa479a39846ff4a1008 Mon Sep 17 00:00:00 2001 From: John Sundell Date: Mon, 27 Aug 2018 19:01:57 +0200 Subject: [PATCH] =?UTF-8?q?Don=E2=80=99t=20highlight=20keywords=20when=20u?= =?UTF-8?q?sed=20as=20a=20parameter=20label?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Most keywords can be used as parameter labels, such as “for” or “in”, but they shouldn’t be highlighted in that context. --- Sources/Splash/Grammar/SwiftGrammar.swift | 9 +++++++++ Tests/SplashTests/Tests/DeclarationTests.swift | 16 ++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/Sources/Splash/Grammar/SwiftGrammar.swift b/Sources/Splash/Grammar/SwiftGrammar.swift index b034d91..ff6152f 100644 --- a/Sources/Splash/Grammar/SwiftGrammar.swift +++ b/Sources/Splash/Grammar/SwiftGrammar.swift @@ -213,6 +213,15 @@ private extension SwiftGrammar { } } + if let previousToken = segment.tokens.previous { + // Don't highlight most keywords when used as a parameter label + if !segment.tokens.current.isAny(of: "_", "self", "let", "var") { + guard !previousToken.isAny(of: "(", ",") else { + return false + } + } + } + return keywords.contains(segment.tokens.current) } } diff --git a/Tests/SplashTests/Tests/DeclarationTests.swift b/Tests/SplashTests/Tests/DeclarationTests.swift index 27fe459..1480ee4 100644 --- a/Tests/SplashTests/Tests/DeclarationTests.swift +++ b/Tests/SplashTests/Tests/DeclarationTests.swift @@ -73,6 +73,21 @@ final class DeclarationTests: SyntaxHighlighterTestCase { ]) } + func testFunctionDeclarationWithKeywordArgumentLabel() { + let components = highlighter.highlight("func a(for b: B)") + + XCTAssertEqual(components, [ + .token("func", .keyword), + .whitespace(" "), + .plainText("a(for"), + .whitespace(" "), + .plainText("b:"), + .whitespace(" "), + .token("B", .type), + .plainText(")") + ]) + } + func testGenericFunctionDeclarationWithoutConstraints() { let components = highlighter.highlight("func hello(a: A, b: B)") @@ -473,6 +488,7 @@ extension DeclarationTests { ("testRequiredFunctionDeclaration", testRequiredFunctionDeclaration), ("testPublicFunctionDeclarationWithDocumentationEndingWithDot", testPublicFunctionDeclarationWithDocumentationEndingWithDot), ("testFunctionDeclarationWithEmptyExternalLabel", testFunctionDeclarationWithEmptyExternalLabel), + ("testFunctionDeclarationWithKeywordArgumentLabel", testFunctionDeclarationWithKeywordArgumentLabel), ("testGenericFunctionDeclarationWithoutConstraints", testGenericFunctionDeclarationWithoutConstraints), ("testGenericFunctionDeclarationWithSingleConstraint", testGenericFunctionDeclarationWithSingleConstraint), ("testGenericFunctionDeclarationWithMultipleConstraints", testGenericFunctionDeclarationWithMultipleConstraints),