Skip to content

Commit 4da9bbd

Browse files
authored
Add support for the “prefix” keyword (#91)
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).
1 parent 3de0275 commit 4da9bbd

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

Sources/Splash/Grammar/SwiftGrammar.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,10 @@ private extension SwiftGrammar {
287287
var tokenType: TokenType { return .keyword }
288288

289289
func matches(_ segment: Segment) -> Bool {
290+
if segment.tokens.current == "prefix" && segment.tokens.next == "func" {
291+
return true
292+
}
293+
290294
if segment.tokens.next == ":" {
291295
// Nil pattern matching inside of a switch statement case
292296
if segment.tokens.current == "nil" {

Tests/SplashTests/Tests/DeclarationTests.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -955,6 +955,31 @@ final class DeclarationTests: SyntaxHighlighterTestCase {
955955
])
956956
}
957957

958+
func testPrefixFunctionDeclaration() {
959+
let components = highlighter.highlight("prefix func !(rhs: Bool) -> Bool { !rhs }")
960+
961+
XCTAssertEqual(components, [
962+
.token("prefix", .keyword),
963+
.whitespace(" "),
964+
.token("func", .keyword),
965+
.whitespace(" "),
966+
.plainText("!(rhs:"),
967+
.whitespace(" "),
968+
.token("Bool", .type),
969+
.plainText(")"),
970+
.whitespace(" "),
971+
.plainText("->"),
972+
.whitespace(" "),
973+
.token("Bool", .type),
974+
.whitespace(" "),
975+
.plainText("{"),
976+
.whitespace(" "),
977+
.plainText("!rhs"),
978+
.whitespace(" "),
979+
.plainText("}")
980+
])
981+
}
982+
958983
func testIndirectEnumDeclaration() {
959984
let components = highlighter.highlight("""
960985
indirect enum Content {
@@ -1064,6 +1089,7 @@ extension DeclarationTests {
10641089
("testNonMutatingFunction", testNonMutatingFunction),
10651090
("testRethrowingFunctionDeclaration", testRethrowingFunctionDeclaration),
10661091
("testFunctionDeclarationWithOpaqueReturnType", testFunctionDeclarationWithOpaqueReturnType),
1092+
("testPrefixFunctionDeclaration", testPrefixFunctionDeclaration),
10671093
("testIndirectEnumDeclaration", testIndirectEnumDeclaration),
10681094
("testWrappedPropertyDeclarations", testWrappedPropertyDeclarations)
10691095
]

0 commit comments

Comments
 (0)