Don’t treat keywords used as function names as keywords

Swift enables the use of keywords as function names, and we want those
to be treated as plain text (like other function names) rather than as
keywords. Some keywords require escaping using back-ticks, and this
patch accounts for both of those variations.
This commit is contained in:
John Sundell 2019-03-08 17:55:44 +01:00
parent 87437d7425
commit ddd2bf9020
2 changed files with 50 additions and 0 deletions

View File

@ -221,6 +221,10 @@ private extension SwiftGrammar {
return false return false
} }
} }
guard !segment.tokens.previous.isAny(of: "func", "`") else {
return false
}
} }
return keywords.contains(segment.tokens.current) return keywords.contains(segment.tokens.current)

View File

@ -520,6 +520,50 @@ final class DeclarationTests: SyntaxHighlighterTestCase {
]) ])
} }
func testFunctionDeclarationWithNonEscapedKeywordAsName() {
let components = highlighter.highlight("func get() -> Int { return 7 }")
XCTAssertEqual(components, [
.token("func", .keyword),
.whitespace(" "),
.plainText("get()"),
.whitespace(" "),
.plainText("->"),
.whitespace(" "),
.token("Int", .type),
.whitespace(" "),
.plainText("{"),
.whitespace(" "),
.token("return", .keyword),
.whitespace(" "),
.token("7", .number),
.whitespace(" "),
.plainText("}")
])
}
func testFunctionDeclarationWithEscapedKeywordAsName() {
let components = highlighter.highlight("func `public`() -> Int { return 7 }")
XCTAssertEqual(components, [
.token("func", .keyword),
.whitespace(" "),
.plainText("`public`()"),
.whitespace(" "),
.plainText("->"),
.whitespace(" "),
.token("Int", .type),
.whitespace(" "),
.plainText("{"),
.whitespace(" "),
.token("return", .keyword),
.whitespace(" "),
.token("7", .number),
.whitespace(" "),
.plainText("}")
])
}
func testIndirectEnumDeclaration() { func testIndirectEnumDeclaration() {
let components = highlighter.highlight(""" let components = highlighter.highlight("""
indirect enum Content { indirect enum Content {
@ -583,6 +627,8 @@ extension DeclarationTests {
("testSubscriptDeclaration", testSubscriptDeclaration), ("testSubscriptDeclaration", testSubscriptDeclaration),
("testDeferDeclaration", testDeferDeclaration), ("testDeferDeclaration", testDeferDeclaration),
("testFunctionDeclarationWithInOutParameter", testFunctionDeclarationWithInOutParameter), ("testFunctionDeclarationWithInOutParameter", testFunctionDeclarationWithInOutParameter),
("testFunctionDeclarationWithNonEscapedKeywordAsName", testFunctionDeclarationWithNonEscapedKeywordAsName),
("testFunctionDeclarationWithEscapedKeywordAsName", testFunctionDeclarationWithEscapedKeywordAsName),
("testIndirectEnumDeclaration", testIndirectEnumDeclaration) ("testIndirectEnumDeclaration", testIndirectEnumDeclaration)
] ]
} }