Don’t highlight keywords when used as a parameter label

Most keywords can be used as parameter labels, such as “for” or “in”, but
they shouldn’t be highlighted in that context.
This commit is contained in:
John Sundell 2018-08-27 19:01:57 +02:00
parent dc83f92228
commit ac9b512013
2 changed files with 25 additions and 0 deletions

View File

@ -213,6 +213,15 @@ private extension SwiftGrammar {
}
}
if let previousToken = segment.tokens.previous {
// Don't highlight most keywords when used as a parameter label
if !segment.tokens.current.isAny(of: "_", "self", "let", "var") {
guard !previousToken.isAny(of: "(", ",") else {
return false
}
}
}
return keywords.contains(segment.tokens.current)
}
}

View File

@ -73,6 +73,21 @@ final class DeclarationTests: SyntaxHighlighterTestCase {
])
}
func testFunctionDeclarationWithKeywordArgumentLabel() {
let components = highlighter.highlight("func a(for b: B)")
XCTAssertEqual(components, [
.token("func", .keyword),
.whitespace(" "),
.plainText("a(for"),
.whitespace(" "),
.plainText("b:"),
.whitespace(" "),
.token("B", .type),
.plainText(")")
])
}
func testGenericFunctionDeclarationWithoutConstraints() {
let components = highlighter.highlight("func hello<A, B>(a: A, b: B)")
@ -473,6 +488,7 @@ extension DeclarationTests {
("testRequiredFunctionDeclaration", testRequiredFunctionDeclaration),
("testPublicFunctionDeclarationWithDocumentationEndingWithDot", testPublicFunctionDeclarationWithDocumentationEndingWithDot),
("testFunctionDeclarationWithEmptyExternalLabel", testFunctionDeclarationWithEmptyExternalLabel),
("testFunctionDeclarationWithKeywordArgumentLabel", testFunctionDeclarationWithKeywordArgumentLabel),
("testGenericFunctionDeclarationWithoutConstraints", testGenericFunctionDeclarationWithoutConstraints),
("testGenericFunctionDeclarationWithSingleConstraint", testGenericFunctionDeclarationWithSingleConstraint),
("testGenericFunctionDeclarationWithMultipleConstraints", testGenericFunctionDeclarationWithMultipleConstraints),