From ddd2bf90201784f194e597302642042392193946 Mon Sep 17 00:00:00 2001 From: John Sundell Date: Fri, 8 Mar 2019 17:55:44 +0100 Subject: [PATCH] =?UTF-8?q?Don=E2=80=99t=20treat=20keywords=20used=20as=20?= =?UTF-8?q?function=20names=20as=20keywords?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Swift enables the use of keywords as function names, and we want those to be treated as plain text (like other function names) rather than as keywords. Some keywords require escaping using back-ticks, and this patch accounts for both of those variations. --- Sources/Splash/Grammar/SwiftGrammar.swift | 4 ++ .../SplashTests/Tests/DeclarationTests.swift | 46 +++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/Sources/Splash/Grammar/SwiftGrammar.swift b/Sources/Splash/Grammar/SwiftGrammar.swift index 1a9a628..803c436 100644 --- a/Sources/Splash/Grammar/SwiftGrammar.swift +++ b/Sources/Splash/Grammar/SwiftGrammar.swift @@ -221,6 +221,10 @@ private extension SwiftGrammar { return false } } + + guard !segment.tokens.previous.isAny(of: "func", "`") else { + return false + } } return keywords.contains(segment.tokens.current) diff --git a/Tests/SplashTests/Tests/DeclarationTests.swift b/Tests/SplashTests/Tests/DeclarationTests.swift index c87da9b..bce347b 100644 --- a/Tests/SplashTests/Tests/DeclarationTests.swift +++ b/Tests/SplashTests/Tests/DeclarationTests.swift @@ -520,6 +520,50 @@ final class DeclarationTests: SyntaxHighlighterTestCase { ]) } + func testFunctionDeclarationWithNonEscapedKeywordAsName() { + let components = highlighter.highlight("func get() -> Int { return 7 }") + + XCTAssertEqual(components, [ + .token("func", .keyword), + .whitespace(" "), + .plainText("get()"), + .whitespace(" "), + .plainText("->"), + .whitespace(" "), + .token("Int", .type), + .whitespace(" "), + .plainText("{"), + .whitespace(" "), + .token("return", .keyword), + .whitespace(" "), + .token("7", .number), + .whitespace(" "), + .plainText("}") + ]) + } + + func testFunctionDeclarationWithEscapedKeywordAsName() { + let components = highlighter.highlight("func `public`() -> Int { return 7 }") + + XCTAssertEqual(components, [ + .token("func", .keyword), + .whitespace(" "), + .plainText("`public`()"), + .whitespace(" "), + .plainText("->"), + .whitespace(" "), + .token("Int", .type), + .whitespace(" "), + .plainText("{"), + .whitespace(" "), + .token("return", .keyword), + .whitespace(" "), + .token("7", .number), + .whitespace(" "), + .plainText("}") + ]) + } + func testIndirectEnumDeclaration() { let components = highlighter.highlight(""" indirect enum Content { @@ -583,6 +627,8 @@ extension DeclarationTests { ("testSubscriptDeclaration", testSubscriptDeclaration), ("testDeferDeclaration", testDeferDeclaration), ("testFunctionDeclarationWithInOutParameter", testFunctionDeclarationWithInOutParameter), + ("testFunctionDeclarationWithNonEscapedKeywordAsName", testFunctionDeclarationWithNonEscapedKeywordAsName), + ("testFunctionDeclarationWithEscapedKeywordAsName", testFunctionDeclarationWithEscapedKeywordAsName), ("testIndirectEnumDeclaration", testIndirectEnumDeclaration) ] }