From dc83f92228e1848e58d6692d8dc8464f539e1ed9 Mon Sep 17 00:00:00 2001 From: John Sundell Date: Mon, 27 Aug 2018 18:44:07 +0200 Subject: [PATCH] =?UTF-8?q?Highlight=20=E2=80=98self=E2=80=99=20as=20keywo?= =?UTF-8?q?rd=20when=20used=20to=20refer=20to=20a=20type?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This patch fixes syntax highlighting when using `.self` to refer to a type. --- Sources/Splash/Grammar/SwiftGrammar.swift | 12 +++++++----- Tests/SplashTests/Tests/FunctionCallTests.swift | 16 +++++++++++++++- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/Sources/Splash/Grammar/SwiftGrammar.swift b/Sources/Splash/Grammar/SwiftGrammar.swift index 72b84f9..b034d91 100644 --- a/Sources/Splash/Grammar/SwiftGrammar.swift +++ b/Sources/Splash/Grammar/SwiftGrammar.swift @@ -276,6 +276,10 @@ private extension SwiftGrammar { return false } + guard segment.tokens.current != "self" else { + return false + } + return segment.tokens.onSameLine.first != "import" } } @@ -292,14 +296,12 @@ private extension SwiftGrammar { return false } - guard !segment.prefixedByDotAccess else { + guard segment.tokens.current != "self" else { return false } - if let previousToken = segment.tokens.previous { - guard !keywords.contains(previousToken) else { - return false - } + guard !segment.prefixedByDotAccess else { + return false } return segment.tokens.onSameLine.first != "import" diff --git a/Tests/SplashTests/Tests/FunctionCallTests.swift b/Tests/SplashTests/Tests/FunctionCallTests.swift index 2060c74..13b52e4 100644 --- a/Tests/SplashTests/Tests/FunctionCallTests.swift +++ b/Tests/SplashTests/Tests/FunctionCallTests.swift @@ -88,6 +88,19 @@ final class FunctionCallTests: SyntaxHighlighterTestCase { ]) } + func testPassingTypeToFunction() { + let components = highlighter.highlight("call(String.self)") + + XCTAssertEqual(components, [ + .token("call", .call), + .plainText("("), + .token("String", .type), + .plainText("."), + .token("self", .keyword), + .plainText(")") + ]) + } + func testAllTestsRunOnLinux() { XCTAssertTrue(TestCaseVerifier.verifyLinuxTests((type(of: self)).allTests)) } @@ -101,7 +114,8 @@ extension FunctionCallTests { ("testExplicitInitializerCall", testExplicitInitializerCall), ("testAccessingPropertyAfterFunctionCallWithoutArguments", testAccessingPropertyAfterFunctionCallWithoutArguments), ("testAccessingPropertyAfterFunctionCallWithArguments", testAccessingPropertyAfterFunctionCallWithArguments), - ("testCallingStaticMethodOnGenericType", testCallingStaticMethodOnGenericType) + ("testCallingStaticMethodOnGenericType", testCallingStaticMethodOnGenericType), + ("testPassingTypeToFunction", testPassingTypeToFunction) ] } }