From 4da9bbd0bc14d5774b112fc4bb1859d5a67fb478 Mon Sep 17 00:00:00 2001 From: John Sundell Date: Mon, 30 Dec 2019 23:39:54 +0000 Subject: [PATCH] =?UTF-8?q?Add=20support=20for=20the=20=E2=80=9Cprefix?= =?UTF-8?q?=E2=80=9D=20keyword=20(#91)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This patch makes Splash correctly highlight the `prefix` keyword, which can only appear before `func`, so a special case was added for it (for now). --- Sources/Splash/Grammar/SwiftGrammar.swift | 4 +++ .../SplashTests/Tests/DeclarationTests.swift | 26 +++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/Sources/Splash/Grammar/SwiftGrammar.swift b/Sources/Splash/Grammar/SwiftGrammar.swift index 1dbf34d..05bd9a4 100644 --- a/Sources/Splash/Grammar/SwiftGrammar.swift +++ b/Sources/Splash/Grammar/SwiftGrammar.swift @@ -287,6 +287,10 @@ private extension SwiftGrammar { var tokenType: TokenType { return .keyword } func matches(_ segment: Segment) -> Bool { + if segment.tokens.current == "prefix" && segment.tokens.next == "func" { + return true + } + if segment.tokens.next == ":" { // Nil pattern matching inside of a switch statement case if segment.tokens.current == "nil" { diff --git a/Tests/SplashTests/Tests/DeclarationTests.swift b/Tests/SplashTests/Tests/DeclarationTests.swift index 8256747..58453c3 100644 --- a/Tests/SplashTests/Tests/DeclarationTests.swift +++ b/Tests/SplashTests/Tests/DeclarationTests.swift @@ -955,6 +955,31 @@ final class DeclarationTests: SyntaxHighlighterTestCase { ]) } + func testPrefixFunctionDeclaration() { + let components = highlighter.highlight("prefix func !(rhs: Bool) -> Bool { !rhs }") + + XCTAssertEqual(components, [ + .token("prefix", .keyword), + .whitespace(" "), + .token("func", .keyword), + .whitespace(" "), + .plainText("!(rhs:"), + .whitespace(" "), + .token("Bool", .type), + .plainText(")"), + .whitespace(" "), + .plainText("->"), + .whitespace(" "), + .token("Bool", .type), + .whitespace(" "), + .plainText("{"), + .whitespace(" "), + .plainText("!rhs"), + .whitespace(" "), + .plainText("}") + ]) + } + func testIndirectEnumDeclaration() { let components = highlighter.highlight(""" indirect enum Content { @@ -1064,6 +1089,7 @@ extension DeclarationTests { ("testNonMutatingFunction", testNonMutatingFunction), ("testRethrowingFunctionDeclaration", testRethrowingFunctionDeclaration), ("testFunctionDeclarationWithOpaqueReturnType", testFunctionDeclarationWithOpaqueReturnType), + ("testPrefixFunctionDeclaration", testPrefixFunctionDeclaration), ("testIndirectEnumDeclaration", testIndirectEnumDeclaration), ("testWrappedPropertyDeclarations", testWrappedPropertyDeclarations) ]