From b1f77c9ff163ee82177edafb3a21209edd123912 Mon Sep 17 00:00:00 2001 From: John Sundell Date: Wed, 17 Jul 2019 10:57:30 +0200 Subject: [PATCH] Fix highlighting for interpolated closure shorthand arguments (#75) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This patch fixes syntax highlighting for when shorthand closure arguments ($0, $1…) are interpolated into a string literal. The fix is to no longer treat “$” as a delimiter, which should help solve similar issues that might be encountered in the future as well. --- Sources/Splash/Grammar/SwiftGrammar.swift | 1 + Tests/SplashTests/Tests/LiteralTests.swift | 11 +++++++++++ 2 files changed, 12 insertions(+) diff --git a/Sources/Splash/Grammar/SwiftGrammar.swift b/Sources/Splash/Grammar/SwiftGrammar.swift index fd1b23b..1441837 100644 --- a/Sources/Splash/Grammar/SwiftGrammar.swift +++ b/Sources/Splash/Grammar/SwiftGrammar.swift @@ -18,6 +18,7 @@ public struct SwiftGrammar: Grammar { delimiters.remove("\"") delimiters.remove("#") delimiters.remove("@") + delimiters.remove("$") self.delimiters = delimiters syntaxRules = [ diff --git a/Tests/SplashTests/Tests/LiteralTests.swift b/Tests/SplashTests/Tests/LiteralTests.swift index 54afca4..609ad87 100644 --- a/Tests/SplashTests/Tests/LiteralTests.swift +++ b/Tests/SplashTests/Tests/LiteralTests.swift @@ -76,6 +76,16 @@ final class LiteralTests: SyntaxHighlighterTestCase { ]) } + func testStringLiteralWithInterpolatedClosureArgumentShorthand() { + let components = highlighter.highlight(#""\($0)""#) + + XCTAssertEqual(components, [ + .token("\"", .string), + .plainText(#"\($0)"#), + .token("\"", .string) + ]) + } + func testStringLiteralWithCustomIterpolation() { let components = highlighter.highlight(""" "Hello \\(label: a, b) world \\(label: call())" @@ -254,6 +264,7 @@ extension LiteralTests { ("testStringLiteralWithEscapedQuote", testStringLiteralWithEscapedQuote), ("testStringLiteralWithAttribute", testStringLiteralWithAttribute), ("testStringLiteralInterpolation", testStringLiteralInterpolation), + ("testStringLiteralWithInterpolatedClosureArgumentShorthand", testStringLiteralWithInterpolatedClosureArgumentShorthand), ("testStringLiteralWithCustomIterpolation", testStringLiteralWithCustomIterpolation), ("testMultiLineStringLiteral", testMultiLineStringLiteral), ("testSingleLineRawStringLiteral", testSingleLineRawStringLiteral),