Never highlight keywords like “init” and “didSet” as function calls (#119)

This commit is contained in:
John Sundell 2020-10-12 23:44:13 +02:00 committed by GitHub
parent ee516ebf19
commit 8d83deb708
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 68 additions and 2 deletions

View File

@ -265,6 +265,11 @@ private extension SwiftGrammar {
return false
}
// Never highlight initializers as regular function calls
if token == "init" {
return false
}
// There's a few keywords that might look like function calls
if callLikeKeywords.contains(segment.tokens.current) {
if let nextToken = segment.tokens.next {
@ -305,7 +310,7 @@ private extension SwiftGrammar {
return false
}
if segment.tokens.previous != "." {
if segment.tokens.previous != "." || segment.tokens.onSameLine.isEmpty {
guard !keywords.contains(segment.tokens.current) else {
return false
}
@ -509,7 +514,7 @@ private extension SwiftGrammar {
return false
}
guard segment.tokens.current != "self" else {
guard !segment.tokens.current.isAny(of: "self", "init") else {
return false
}

View File

@ -682,6 +682,47 @@ final class DeclarationTests: SyntaxHighlighterTestCase {
])
}
func testPropertyWithCommentedDidSet() {
let components = highlighter.highlight("""
struct Hello {
var property: Int {
// Comment.
didSet { }
}
}
""")
XCTAssertEqual(components, [
.token("struct", .keyword),
.whitespace(" "),
.plainText("Hello"),
.whitespace(" "),
.plainText("{"),
.whitespace("\n "),
.token("var", .keyword),
.whitespace(" "),
.plainText("property:"),
.whitespace(" "),
.token("Int", .type),
.whitespace(" "),
.plainText("{"),
.whitespace("\n "),
.token("//", .comment),
.whitespace(" "),
.token("Comment.", .comment),
.whitespace("\n "),
.token("didSet", .keyword),
.whitespace(" "),
.plainText("{"),
.whitespace(" "),
.plainText("}"),
.whitespace("\n "),
.plainText("}"),
.whitespace("\n"),
.plainText("}")
])
}
func testPropertyWithSetterAccessLevel() {
let components = highlighter.highlight("""
struct Hello {
@ -1305,6 +1346,7 @@ extension DeclarationTests {
("testGenericPropertyDeclaration", testGenericPropertyDeclaration),
("testPropertyDeclarationWithWillSet", testPropertyDeclarationWithWillSet),
("testPropertyDeclarationWithDidSet", testPropertyDeclarationWithDidSet),
("testPropertyWithCommentedDidSet", testPropertyWithCommentedDidSet),
("testPropertyWithSetterAccessLevel", testPropertyWithSetterAccessLevel),
("testPropertyDeclarationAfterCommentEndingWithVarKeyword", testPropertyDeclarationAfterCommentEndingWithVarKeyword),
("testPropertyDeclarationWithStaticPropertyDefaultValue", testPropertyDeclarationWithStaticPropertyDefaultValue),

View File

@ -66,6 +66,24 @@ final class FunctionCallTests: SyntaxHighlighterTestCase {
])
}
func testExplicitInitializerCallUsingTrailingClosureSyntax() {
let components = highlighter.highlight("let task = Task.init {}")
XCTAssertEqual(components, [
.token("let", .keyword),
.whitespace(" "),
.plainText("task"),
.whitespace(" "),
.plainText("="),
.whitespace(" "),
.token("Task", .type),
.plainText("."),
.token("init", .keyword),
.whitespace(" "),
.plainText("{}")
])
}
func testDotSyntaxInitializerCall() {
let components = highlighter.highlight("let string: String = .init()")
@ -225,6 +243,7 @@ extension FunctionCallTests {
("testFunctionCallWithNil", testFunctionCallWithNil),
("testImplicitInitializerCall", testImplicitInitializerCall),
("testExplicitInitializerCall", testExplicitInitializerCall),
("testExplicitInitializerCallUsingTrailingClosureSyntax", testExplicitInitializerCallUsingTrailingClosureSyntax),
("testDotSyntaxInitializerCall", testDotSyntaxInitializerCall),
("testAccessingPropertyAfterFunctionCallWithoutArguments", testAccessingPropertyAfterFunctionCallWithoutArguments),
("testAccessingPropertyAfterFunctionCallWithArguments", testAccessingPropertyAfterFunctionCallWithArguments),