Skip to content

Commit 1559117

Browse files
authored
Merge pull request #13 from JohnSundell/keyword-parameter-labels
Don’t highlight keywords when used as a parameter label
2 parents b8584cd + ac9b512 commit 1559117

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

Sources/Splash/Grammar/SwiftGrammar.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,15 @@ private extension SwiftGrammar {
213213
}
214214
}
215215

216+
if let previousToken = segment.tokens.previous {
217+
// Don't highlight most keywords when used as a parameter label
218+
if !segment.tokens.current.isAny(of: "_", "self", "let", "var") {
219+
guard !previousToken.isAny(of: "(", ",") else {
220+
return false
221+
}
222+
}
223+
}
224+
216225
return keywords.contains(segment.tokens.current)
217226
}
218227
}

Tests/SplashTests/Tests/DeclarationTests.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,21 @@ final class DeclarationTests: SyntaxHighlighterTestCase {
7373
])
7474
}
7575

76+
func testFunctionDeclarationWithKeywordArgumentLabel() {
77+
let components = highlighter.highlight("func a(for b: B)")
78+
79+
XCTAssertEqual(components, [
80+
.token("func", .keyword),
81+
.whitespace(" "),
82+
.plainText("a(for"),
83+
.whitespace(" "),
84+
.plainText("b:"),
85+
.whitespace(" "),
86+
.token("B", .type),
87+
.plainText(")")
88+
])
89+
}
90+
7691
func testGenericFunctionDeclarationWithoutConstraints() {
7792
let components = highlighter.highlight("func hello<A, B>(a: A, b: B)")
7893

@@ -473,6 +488,7 @@ extension DeclarationTests {
473488
("testRequiredFunctionDeclaration", testRequiredFunctionDeclaration),
474489
("testPublicFunctionDeclarationWithDocumentationEndingWithDot", testPublicFunctionDeclarationWithDocumentationEndingWithDot),
475490
("testFunctionDeclarationWithEmptyExternalLabel", testFunctionDeclarationWithEmptyExternalLabel),
491+
("testFunctionDeclarationWithKeywordArgumentLabel", testFunctionDeclarationWithKeywordArgumentLabel),
476492
("testGenericFunctionDeclarationWithoutConstraints", testGenericFunctionDeclarationWithoutConstraints),
477493
("testGenericFunctionDeclarationWithSingleConstraint", testGenericFunctionDeclarationWithSingleConstraint),
478494
("testGenericFunctionDeclarationWithMultipleConstraints", testGenericFunctionDeclarationWithMultipleConstraints),

0 commit comments

Comments
 (0)