diff --git a/Sources/Splash/Grammar/SwiftGrammar.swift b/Sources/Splash/Grammar/SwiftGrammar.swift index ec3949f..d45c28c 100644 --- a/Sources/Splash/Grammar/SwiftGrammar.swift +++ b/Sources/Splash/Grammar/SwiftGrammar.swift @@ -325,8 +325,10 @@ private extension SwiftGrammar { if let previousToken = segment.tokens.previous { // Don't highlight variables with the same name as a keyword // when used in optional binding, such as if let, guard let: - guard !previousToken.isAny(of: "let", "var") else { - return false + if !segment.tokens.onSameLine.isEmpty, segment.tokens.current != "self" { + guard !previousToken.isAny(of: "let", "var") else { + return false + } } if !declarationKeywords.contains(segment.tokens.current) { diff --git a/Tests/SplashTests/Tests/DeclarationTests.swift b/Tests/SplashTests/Tests/DeclarationTests.swift index a13cbb2..d905bab 100644 --- a/Tests/SplashTests/Tests/DeclarationTests.swift +++ b/Tests/SplashTests/Tests/DeclarationTests.swift @@ -691,6 +691,27 @@ final class DeclarationTests: SyntaxHighlighterTestCase { ]) } + func testPropertyDeclarationAfterCommentEndingWithVarKeyword() { + let components = highlighter.highlight(""" + // var + var number = 7 + """) + + XCTAssertEqual(components, [ + .token("//", .comment), + .whitespace(" "), + .token("var", .comment), + .whitespace("\n"), + .token("var", .keyword), + .whitespace(" "), + .plainText("number"), + .whitespace(" "), + .plainText("="), + .whitespace(" "), + .token("7", .number) + ]) + } + func testSubscriptDeclaration() { let components = highlighter.highlight(""" extension Collection { @@ -1102,6 +1123,7 @@ extension DeclarationTests { ("testPropertyDeclarationWithWillSet", testPropertyDeclarationWithWillSet), ("testPropertyDeclarationWithDidSet", testPropertyDeclarationWithDidSet), ("testPropertyWithSetterAccessLevel", testPropertyWithSetterAccessLevel), + ("testPropertyDeclarationAfterCommentEndingWithVarKeyword", testPropertyDeclarationAfterCommentEndingWithVarKeyword), ("testSubscriptDeclaration", testSubscriptDeclaration), ("testGenericSubscriptDeclaration", testGenericSubscriptDeclaration), ("testDeferDeclaration", testDeferDeclaration), diff --git a/Tests/SplashTests/Tests/StatementTests.swift b/Tests/SplashTests/Tests/StatementTests.swift index c4e7368..a43fb63 100644 --- a/Tests/SplashTests/Tests/StatementTests.swift +++ b/Tests/SplashTests/Tests/StatementTests.swift @@ -80,6 +80,26 @@ final class StatementTests: SyntaxHighlighterTestCase { ]) } + func testGuardStatementUnwrappingWeakSelf() { + let components = highlighter.highlight("guard let self = self else {}") + + XCTAssertEqual(components, [ + .token("guard", .keyword), + .whitespace(" "), + .token("let", .keyword), + .whitespace(" "), + .token("self", .keyword), + .whitespace(" "), + .plainText("="), + .whitespace(" "), + .token("self", .keyword), + .whitespace(" "), + .token("else", .keyword), + .whitespace(" "), + .plainText("{}") + ]) + } + func testSwitchStatement() { let components = highlighter.highlight(""" switch variable { @@ -414,6 +434,7 @@ extension StatementTests { ("testImportStatementWithSubmodule", testImportStatementWithSubmodule), ("testChainedIfElseStatements", testChainedIfElseStatements), ("testIfLetStatementWithKeywordSymbolName", testIfLetStatementWithKeywordSymbolName), + ("testGuardStatementUnwrappingWeakSelf", testGuardStatementUnwrappingWeakSelf), ("testSwitchStatement", testSwitchStatement), ("testSwitchStatementWithSingleAssociatedValue", testSwitchStatementWithSingleAssociatedValue), ("testSwitchStatementWithMultipleAssociatedValues", testSwitchStatementWithMultipleAssociatedValues),