Skip to content

Commit

Permalink
Support types and functions named with a leading underscore (#74)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
JohnSundell authored Jul 17, 2019
1 parent b1f77c9 commit 370f685
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
14 changes: 11 additions & 3 deletions Sources/Splash/Grammar/SwiftGrammar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,11 @@ private extension SwiftGrammar {
}

func matches(_ segment: Segment) -> Bool {
guard segment.tokens.current.startsWithLetter else {
let token = segment.tokens.current.trimmingCharacters(
in: CharacterSet(charactersIn: "_")
)

guard token.startsWithLetter else {
return false
}

Expand Down Expand Up @@ -305,7 +309,11 @@ private extension SwiftGrammar {
}
}

guard segment.tokens.current.isCapitalized else {
let token = segment.tokens.current.trimmingCharacters(
in: CharacterSet(charactersIn: "_")
)

guard token.isCapitalized else {
return false
}

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

Expand Down
22 changes: 21 additions & 1 deletion Tests/SplashTests/Tests/StatementTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,24 @@ final class StatementTests: SyntaxHighlighterTestCase {
])
}

func testInitializingTypeWithLeadingUnderscore() {
let components = highlighter.highlight("_MyType()")

XCTAssertEqual(components, [
.token("_MyType", .type),
.plainText("()")
])
}

func testCallingFunctionWithLeadingUnderscore() {
let components = highlighter.highlight("_myFunction()")

XCTAssertEqual(components, [
.token("_myFunction", .call),
.plainText("()")
])
}

func testAllTestsRunOnLinux() {
XCTAssertTrue(TestCaseVerifier.verifyLinuxTests((type(of: self)).allTests))
}
Expand All @@ -348,7 +366,9 @@ extension StatementTests {
("testSwitchStatementWithOptional", testSwitchStatementWithOptional),
("testForStatementWithStaticProperty", testForStatementWithStaticProperty),
("testForStatementWithContinue", testForStatementWithContinue),
("testRepeatWhileStatement", testRepeatWhileStatement)
("testRepeatWhileStatement", testRepeatWhileStatement),
("testInitializingTypeWithLeadingUnderscore", testInitializingTypeWithLeadingUnderscore),
("testCallingFunctionWithLeadingUnderscore", testCallingFunctionWithLeadingUnderscore)
]
}
}

0 comments on commit 370f685

Please sign in to comment.