Skip to content

Commit 370f685

Browse files
authored
Support types and functions named with a leading underscore (#74)
This patch fixes syntax highlighting for types and functions which names begin with an underscore. Previously these entities would be treated as plain text, but now they’re highlighted correctly as either types or function calls.
1 parent b1f77c9 commit 370f685

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

Sources/Splash/Grammar/SwiftGrammar.swift

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,11 @@ private extension SwiftGrammar {
190190
}
191191

192192
func matches(_ segment: Segment) -> Bool {
193-
guard segment.tokens.current.startsWithLetter else {
193+
let token = segment.tokens.current.trimmingCharacters(
194+
in: CharacterSet(charactersIn: "_")
195+
)
196+
197+
guard token.startsWithLetter else {
194198
return false
195199
}
196200

@@ -305,7 +309,11 @@ private extension SwiftGrammar {
305309
}
306310
}
307311

308-
guard segment.tokens.current.isCapitalized else {
312+
let token = segment.tokens.current.trimmingCharacters(
313+
in: CharacterSet(charactersIn: "_")
314+
)
315+
316+
guard token.isCapitalized else {
309317
return false
310318
}
311319

@@ -316,7 +324,7 @@ private extension SwiftGrammar {
316324
// The XCTAssert family of functions is a bit of an edge case,
317325
// since they start with capital letters. Since they are so
318326
// commonly used, we'll add a special case for them here:
319-
guard !segment.tokens.current.starts(with: "XCTAssert") else {
327+
guard !token.starts(with: "XCTAssert") else {
320328
return false
321329
}
322330

Tests/SplashTests/Tests/StatementTests.swift

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,24 @@ final class StatementTests: SyntaxHighlighterTestCase {
330330
])
331331
}
332332

333+
func testInitializingTypeWithLeadingUnderscore() {
334+
let components = highlighter.highlight("_MyType()")
335+
336+
XCTAssertEqual(components, [
337+
.token("_MyType", .type),
338+
.plainText("()")
339+
])
340+
}
341+
342+
func testCallingFunctionWithLeadingUnderscore() {
343+
let components = highlighter.highlight("_myFunction()")
344+
345+
XCTAssertEqual(components, [
346+
.token("_myFunction", .call),
347+
.plainText("()")
348+
])
349+
}
350+
333351
func testAllTestsRunOnLinux() {
334352
XCTAssertTrue(TestCaseVerifier.verifyLinuxTests((type(of: self)).allTests))
335353
}
@@ -348,7 +366,9 @@ extension StatementTests {
348366
("testSwitchStatementWithOptional", testSwitchStatementWithOptional),
349367
("testForStatementWithStaticProperty", testForStatementWithStaticProperty),
350368
("testForStatementWithContinue", testForStatementWithContinue),
351-
("testRepeatWhileStatement", testRepeatWhileStatement)
369+
("testRepeatWhileStatement", testRepeatWhileStatement),
370+
("testInitializingTypeWithLeadingUnderscore", testInitializingTypeWithLeadingUnderscore),
371+
("testCallingFunctionWithLeadingUnderscore", testCallingFunctionWithLeadingUnderscore)
352372
]
353373
}
354374
}

0 commit comments

Comments
 (0)