Merge pull request #13 from JohnSundell/keyword-parameter-labels

Don’t highlight keywords when used as a parameter label
This commit is contained in:
John Sundell 2018-08-27 19:15:22 +02:00 committed by GitHub
commit 1559117b5a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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) 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() { func testGenericFunctionDeclarationWithoutConstraints() {
let components = highlighter.highlight("func hello<A, B>(a: A, b: B)") let components = highlighter.highlight("func hello<A, B>(a: A, b: B)")
@ -473,6 +488,7 @@ extension DeclarationTests {
("testRequiredFunctionDeclaration", testRequiredFunctionDeclaration), ("testRequiredFunctionDeclaration", testRequiredFunctionDeclaration),
("testPublicFunctionDeclarationWithDocumentationEndingWithDot", testPublicFunctionDeclarationWithDocumentationEndingWithDot), ("testPublicFunctionDeclarationWithDocumentationEndingWithDot", testPublicFunctionDeclarationWithDocumentationEndingWithDot),
("testFunctionDeclarationWithEmptyExternalLabel", testFunctionDeclarationWithEmptyExternalLabel), ("testFunctionDeclarationWithEmptyExternalLabel", testFunctionDeclarationWithEmptyExternalLabel),
("testFunctionDeclarationWithKeywordArgumentLabel", testFunctionDeclarationWithKeywordArgumentLabel),
("testGenericFunctionDeclarationWithoutConstraints", testGenericFunctionDeclarationWithoutConstraints), ("testGenericFunctionDeclarationWithoutConstraints", testGenericFunctionDeclarationWithoutConstraints),
("testGenericFunctionDeclarationWithSingleConstraint", testGenericFunctionDeclarationWithSingleConstraint), ("testGenericFunctionDeclarationWithSingleConstraint", testGenericFunctionDeclarationWithSingleConstraint),
("testGenericFunctionDeclarationWithMultipleConstraints", testGenericFunctionDeclarationWithMultipleConstraints), ("testGenericFunctionDeclarationWithMultipleConstraints", testGenericFunctionDeclarationWithMultipleConstraints),