Skip to content

Commit

Permalink
Add support for the “prefix” keyword (#91)
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
JohnSundell authored Dec 30, 2019
1 parent 3de0275 commit 4da9bbd
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Sources/Splash/Grammar/SwiftGrammar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down
26 changes: 26 additions & 0 deletions Tests/SplashTests/Tests/DeclarationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -1064,6 +1089,7 @@ extension DeclarationTests {
("testNonMutatingFunction", testNonMutatingFunction),
("testRethrowingFunctionDeclaration", testRethrowingFunctionDeclaration),
("testFunctionDeclarationWithOpaqueReturnType", testFunctionDeclarationWithOpaqueReturnType),
("testPrefixFunctionDeclaration", testPrefixFunctionDeclaration),
("testIndirectEnumDeclaration", testIndirectEnumDeclaration),
("testWrappedPropertyDeclarations", testWrappedPropertyDeclarations)
]
Expand Down

0 comments on commit 4da9bbd

Please sign in to comment.