From c05038072a4e07fc08b989cff38867ca16c460f7 Mon Sep 17 00:00:00 2001 From: John Sundell Date: Sun, 31 Mar 2019 22:40:37 +0200 Subject: [PATCH] Correctly highlight key paths passed as arguments (#64) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This makes Splash correctly highlight key paths that are passed as arguments to a function, especially when there’s no external parameter label for that argument. --- Sources/Splash/Grammar/SwiftGrammar.swift | 4 ++-- Tests/SplashTests/Tests/LiteralTests.swift | 21 ++++++++++++++++++++- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/Sources/Splash/Grammar/SwiftGrammar.swift b/Sources/Splash/Grammar/SwiftGrammar.swift index b07776a..e88177e 100644 --- a/Sources/Splash/Grammar/SwiftGrammar.swift +++ b/Sources/Splash/Grammar/SwiftGrammar.swift @@ -231,7 +231,7 @@ private extension SwiftGrammar { } } - return segment.tokens.next.isAny(of: "(", "()", "())", "(.", "({", "().") + return segment.tokens.next.isAny(of: "(", "()", "())", "(.", "({", "().", #"(\."#) } } @@ -377,7 +377,7 @@ private extension SwiftGrammar { var tokenType: TokenType { return .property } func matches(_ segment: Segment) -> Bool { - return segment.tokens.previous == "\\." + return segment.tokens.previous.isAny(of: #"\."#, #"(\."#) } } diff --git a/Tests/SplashTests/Tests/LiteralTests.swift b/Tests/SplashTests/Tests/LiteralTests.swift index e5fb333..54afca4 100644 --- a/Tests/SplashTests/Tests/LiteralTests.swift +++ b/Tests/SplashTests/Tests/LiteralTests.swift @@ -223,6 +223,24 @@ final class LiteralTests: SyntaxHighlighterTestCase { ]) } + func testKeyPathLiteralsAsArguments() { + let components = highlighter.highlight(#"user.bind(\.name, to: \.text)"#) + + XCTAssertEqual(components, [ + .plainText("user."), + .token("bind", .call), + .plainText(#"(\."#), + .token("name", .property), + .plainText(","), + .whitespace(" "), + .plainText("to:"), + .whitespace(" "), + .plainText(#"\."#), + .token("text", .property), + .plainText(")") + ]) + } + func testAllTestsRunOnLinux() { XCTAssertTrue(TestCaseVerifier.verifyLinuxTests((type(of: self)).allTests)) } @@ -242,7 +260,8 @@ extension LiteralTests { ("testMultiLineRawStringLiteral", testMultiLineRawStringLiteral), ("testDoubleLiteral", testDoubleLiteral), ("testIntegerLiteralWithSeparators", testIntegerLiteralWithSeparators), - ("testKeyPathLiteral", testKeyPathLiteral) + ("testKeyPathLiteral", testKeyPathLiteral), + ("testKeyPathLiteralsAsArguments", testKeyPathLiteralsAsArguments) ] } }