diff --git a/Sources/Splash/Grammar/SwiftGrammar.swift b/Sources/Splash/Grammar/SwiftGrammar.swift index 1441837..3217478 100644 --- a/Sources/Splash/Grammar/SwiftGrammar.swift +++ b/Sources/Splash/Grammar/SwiftGrammar.swift @@ -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 } @@ -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 } @@ -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 } diff --git a/Tests/SplashTests/Tests/StatementTests.swift b/Tests/SplashTests/Tests/StatementTests.swift index c06ddc1..70af5bd 100644 --- a/Tests/SplashTests/Tests/StatementTests.swift +++ b/Tests/SplashTests/Tests/StatementTests.swift @@ -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)) } @@ -348,7 +366,9 @@ extension StatementTests { ("testSwitchStatementWithOptional", testSwitchStatementWithOptional), ("testForStatementWithStaticProperty", testForStatementWithStaticProperty), ("testForStatementWithContinue", testForStatementWithContinue), - ("testRepeatWhileStatement", testRepeatWhileStatement) + ("testRepeatWhileStatement", testRepeatWhileStatement), + ("testInitializingTypeWithLeadingUnderscore", testInitializingTypeWithLeadingUnderscore), + ("testCallingFunctionWithLeadingUnderscore", testCallingFunctionWithLeadingUnderscore) ] } }