Skip to content

Commit 8d83deb

Browse files
authored
Never highlight keywords like “init” and “didSet” as function calls (#119)
1 parent ee516eb commit 8d83deb

3 files changed

Lines changed: 68 additions & 2 deletions

File tree

Sources/Splash/Grammar/SwiftGrammar.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,11 @@ private extension SwiftGrammar {
265265
return false
266266
}
267267

268+
// Never highlight initializers as regular function calls
269+
if token == "init" {
270+
return false
271+
}
272+
268273
// There's a few keywords that might look like function calls
269274
if callLikeKeywords.contains(segment.tokens.current) {
270275
if let nextToken = segment.tokens.next {
@@ -305,7 +310,7 @@ private extension SwiftGrammar {
305310
return false
306311
}
307312

308-
if segment.tokens.previous != "." {
313+
if segment.tokens.previous != "." || segment.tokens.onSameLine.isEmpty {
309314
guard !keywords.contains(segment.tokens.current) else {
310315
return false
311316
}
@@ -509,7 +514,7 @@ private extension SwiftGrammar {
509514
return false
510515
}
511516

512-
guard segment.tokens.current != "self" else {
517+
guard !segment.tokens.current.isAny(of: "self", "init") else {
513518
return false
514519
}
515520

Tests/SplashTests/Tests/DeclarationTests.swift

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,47 @@ final class DeclarationTests: SyntaxHighlighterTestCase {
682682
])
683683
}
684684

685+
func testPropertyWithCommentedDidSet() {
686+
let components = highlighter.highlight("""
687+
struct Hello {
688+
var property: Int {
689+
// Comment.
690+
didSet { }
691+
}
692+
}
693+
""")
694+
695+
XCTAssertEqual(components, [
696+
.token("struct", .keyword),
697+
.whitespace(" "),
698+
.plainText("Hello"),
699+
.whitespace(" "),
700+
.plainText("{"),
701+
.whitespace("\n "),
702+
.token("var", .keyword),
703+
.whitespace(" "),
704+
.plainText("property:"),
705+
.whitespace(" "),
706+
.token("Int", .type),
707+
.whitespace(" "),
708+
.plainText("{"),
709+
.whitespace("\n "),
710+
.token("//", .comment),
711+
.whitespace(" "),
712+
.token("Comment.", .comment),
713+
.whitespace("\n "),
714+
.token("didSet", .keyword),
715+
.whitespace(" "),
716+
.plainText("{"),
717+
.whitespace(" "),
718+
.plainText("}"),
719+
.whitespace("\n "),
720+
.plainText("}"),
721+
.whitespace("\n"),
722+
.plainText("}")
723+
])
724+
}
725+
685726
func testPropertyWithSetterAccessLevel() {
686727
let components = highlighter.highlight("""
687728
struct Hello {
@@ -1305,6 +1346,7 @@ extension DeclarationTests {
13051346
("testGenericPropertyDeclaration", testGenericPropertyDeclaration),
13061347
("testPropertyDeclarationWithWillSet", testPropertyDeclarationWithWillSet),
13071348
("testPropertyDeclarationWithDidSet", testPropertyDeclarationWithDidSet),
1349+
("testPropertyWithCommentedDidSet", testPropertyWithCommentedDidSet),
13081350
("testPropertyWithSetterAccessLevel", testPropertyWithSetterAccessLevel),
13091351
("testPropertyDeclarationAfterCommentEndingWithVarKeyword", testPropertyDeclarationAfterCommentEndingWithVarKeyword),
13101352
("testPropertyDeclarationWithStaticPropertyDefaultValue", testPropertyDeclarationWithStaticPropertyDefaultValue),

Tests/SplashTests/Tests/FunctionCallTests.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,24 @@ final class FunctionCallTests: SyntaxHighlighterTestCase {
6666
])
6767
}
6868

69+
func testExplicitInitializerCallUsingTrailingClosureSyntax() {
70+
let components = highlighter.highlight("let task = Task.init {}")
71+
72+
XCTAssertEqual(components, [
73+
.token("let", .keyword),
74+
.whitespace(" "),
75+
.plainText("task"),
76+
.whitespace(" "),
77+
.plainText("="),
78+
.whitespace(" "),
79+
.token("Task", .type),
80+
.plainText("."),
81+
.token("init", .keyword),
82+
.whitespace(" "),
83+
.plainText("{}")
84+
])
85+
}
86+
6987
func testDotSyntaxInitializerCall() {
7088
let components = highlighter.highlight("let string: String = .init()")
7189

@@ -225,6 +243,7 @@ extension FunctionCallTests {
225243
("testFunctionCallWithNil", testFunctionCallWithNil),
226244
("testImplicitInitializerCall", testImplicitInitializerCall),
227245
("testExplicitInitializerCall", testExplicitInitializerCall),
246+
("testExplicitInitializerCallUsingTrailingClosureSyntax", testExplicitInitializerCallUsingTrailingClosureSyntax),
228247
("testDotSyntaxInitializerCall", testDotSyntaxInitializerCall),
229248
("testAccessingPropertyAfterFunctionCallWithoutArguments", testAccessingPropertyAfterFunctionCallWithoutArguments),
230249
("testAccessingPropertyAfterFunctionCallWithArguments", testAccessingPropertyAfterFunctionCallWithArguments),

0 commit comments

Comments
 (0)