Don’t highlight keywords used as parameters on a new line (#94)

This patch fixes a bug that would cause Splash to incorrectly highlight
parameter labels defined on a new line when those would match a keyword.
This commit is contained in:
John Sundell 2020-01-29 00:37:08 +01:00 committed by GitHub
parent 4da9bbd0bc
commit 97541c8835
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 8 deletions

View File

@ -78,6 +78,12 @@ private extension SwiftGrammar {
"public", "internal", "fileprivate", "private"
]
static let declarationKeywords: Set<String> = [
"class", "struct", "enum", "func",
"protocol", "typealias", "import",
"associatedtype", "subscript"
]
struct PreprocessingRule: SyntaxRule {
var tokenType: TokenType { return .preprocessing }
private let controlFlowTokens: Set<String> = ["#if", "#endif", "#elseif", "#else"]
@ -306,8 +312,8 @@ private extension SwiftGrammar {
}
}
if !segment.tokens.onSameLine.isEmpty,
let previousToken = segment.tokens.previous {
if let previousToken = segment.tokens.previous,
!declarationKeywords.contains(segment.tokens.current) {
// Highlight the '(set)' part of setter access modifiers
switch segment.tokens.current {
case "(":
@ -341,12 +347,6 @@ private extension SwiftGrammar {
struct TypeRule: SyntaxRule {
var tokenType: TokenType { return .type }
private let declarationKeywords: Set<String> = [
"class", "struct", "enum", "func",
"protocol", "typealias", "import",
"associatedtype", "subscript"
]
func matches(_ segment: Segment) -> Bool {
// Types should not be highlighted when declared
if let previousToken = segment.tokens.previous {

View File

@ -88,6 +88,28 @@ final class DeclarationTests: SyntaxHighlighterTestCase {
])
}
func testFunctionDeclarationWithKeywordArgumentLabelOnNewLine() {
let components = highlighter.highlight("""
func a(
for b: B
)
""")
XCTAssertEqual(components, [
.token("func", .keyword),
.whitespace(" "),
.plainText("a("),
.whitespace("\n "),
.plainText("for"),
.whitespace(" "),
.plainText("b:"),
.whitespace(" "),
.token("B", .type),
.whitespace("\n"),
.plainText(")")
])
}
func testGenericFunctionDeclarationWithKeywordArgumentLabel() {
let components = highlighter.highlight("func perform<O: AnyObject>(for object: O) {}")
@ -1056,6 +1078,7 @@ extension DeclarationTests {
("testPublicFunctionDeclarationWithDocumentationEndingWithDot", testPublicFunctionDeclarationWithDocumentationEndingWithDot),
("testFunctionDeclarationWithEmptyExternalLabel", testFunctionDeclarationWithEmptyExternalLabel),
("testFunctionDeclarationWithKeywordArgumentLabel", testFunctionDeclarationWithKeywordArgumentLabel),
("testFunctionDeclarationWithKeywordArgumentLabelOnNewLine", testFunctionDeclarationWithKeywordArgumentLabelOnNewLine),
("testGenericFunctionDeclarationWithKeywordArgumentLabel", testGenericFunctionDeclarationWithKeywordArgumentLabel),
("testGenericFunctionDeclarationWithoutConstraints", testGenericFunctionDeclarationWithoutConstraints),
("testGenericFunctionDeclarationWithSingleConstraint", testGenericFunctionDeclarationWithSingleConstraint),