From b9177c4104692ff933ffcdcb47c09d432024f5a3 Mon Sep 17 00:00:00 2001 From: John Sundell Date: Sun, 24 Mar 2019 11:35:33 +0100 Subject: [PATCH] Fix highlighting for nested escaping closures (#62) This patch makes Splash correctly highlight nested closures marked with the `@escaping` attribute. The fix is to start treating `@` like a proper token, rather than as a delimiter. --- Sources/Splash/Grammar/SwiftGrammar.swift | 3 ++- Tests/SplashTests/Tests/ClosureTests.swift | 30 +++++++++++++++++++++- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/Sources/Splash/Grammar/SwiftGrammar.swift b/Sources/Splash/Grammar/SwiftGrammar.swift index 74e40f0..b07776a 100644 --- a/Sources/Splash/Grammar/SwiftGrammar.swift +++ b/Sources/Splash/Grammar/SwiftGrammar.swift @@ -17,6 +17,7 @@ public struct SwiftGrammar: Grammar { delimiters.remove("_") delimiters.remove("\"") delimiters.remove("#") + delimiters.remove("@") self.delimiters = delimiters syntaxRules = [ @@ -96,7 +97,7 @@ private extension SwiftGrammar { var tokenType: TokenType { return .keyword } func matches(_ segment: Segment) -> Bool { - return segment.tokens.current == "@" || segment.tokens.previous == "@" + return segment.tokens.current.hasPrefix("@") } } diff --git a/Tests/SplashTests/Tests/ClosureTests.swift b/Tests/SplashTests/Tests/ClosureTests.swift index 637eb07..4aceb2f 100644 --- a/Tests/SplashTests/Tests/ClosureTests.swift +++ b/Tests/SplashTests/Tests/ClosureTests.swift @@ -124,6 +124,33 @@ final class ClosureTests: SyntaxHighlighterTestCase { ]) } + func testNestedEscapingClosure() { + let components = highlighter.highlight("let closures = [(@escaping () -> Void) -> Void]()") + + XCTAssertEqual(components, [ + .token("let", .keyword), + .whitespace(" "), + .plainText("closures"), + .whitespace(" "), + .plainText("="), + .whitespace(" "), + .plainText("[("), + .token("@escaping", .keyword), + .whitespace(" "), + .plainText("()"), + .whitespace(" "), + .plainText("->"), + .whitespace(" "), + .token("Void", .type), + .plainText(")"), + .whitespace(" "), + .plainText("->"), + .whitespace(" "), + .token("Void", .type), + .plainText("]()") + ]) + } + func testAllTestsRunOnLinux() { XCTAssertTrue(TestCaseVerifier.verifyLinuxTests((type(of: self)).allTests)) } @@ -138,7 +165,8 @@ extension ClosureTests { ("testClosureArgumentWithSingleArgument", testClosureArgumentWithSingleArgument), ("testClosureArgumentWithMultipleArguments", testClosureArgumentWithMultipleArguments), ("testEscapingClosureArgument", testEscapingClosureArgument), - ("testPassingClosureAsArgument", testPassingClosureAsArgument) + ("testPassingClosureAsArgument", testPassingClosureAsArgument), + ("testNestedEscapingClosure", testNestedEscapingClosure) ] } }