Correctly highlight closure argument shorthands (#66)

This patch makes Splash correctly highlight closure argument
shorthands (`$0`, `$1`, etc.), while also making the logic for
detecting a function call a bit simpler.
This commit is contained in:
John Sundell 2019-04-14 22:17:47 +02:00 committed by GitHub
parent 2b9e65e0d8
commit d01f7a6b2f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 4 deletions

View File

@ -144,9 +144,11 @@ private extension SwiftGrammar {
func matches(_ segment: Segment) -> Bool {
// Don't match against index-based closure arguments
guard segment.tokens.previous != "$" else {
if let previous = segment.tokens.previous {
guard !previous.hasSuffix("$") else {
return false
}
}
// Integers can be separated using "_", so handle that
if segment.tokens.current.removing("_").isNumber {
@ -231,7 +233,7 @@ private extension SwiftGrammar {
}
}
return segment.tokens.next.isAny(of: "(", "()", "())", "(.", "({", "().", #"(\."#)
return segment.tokens.next?.starts(with: "(") ?? false
}
}

View File

@ -151,6 +151,35 @@ final class ClosureTests: SyntaxHighlighterTestCase {
])
}
func testClosureArgumentShorthands() {
let components = highlighter.highlight("""
call {
print($0)
_ = $1
$2()
}
""")
XCTAssertEqual(components, [
.token("call", .call),
.whitespace(" "),
.plainText("{"),
.whitespace("\n "),
.token("print", .call),
.plainText("($0)"),
.whitespace("\n "),
.token("_", .keyword),
.whitespace(" "),
.plainText("="),
.whitespace(" "),
.plainText("$1"),
.whitespace("\n "),
.plainText("$2()"),
.whitespace("\n"),
.plainText("}")
])
}
func testAllTestsRunOnLinux() {
XCTAssertTrue(TestCaseVerifier.verifyLinuxTests((type(of: self)).allTests))
}
@ -166,7 +195,8 @@ extension ClosureTests {
("testClosureArgumentWithMultipleArguments", testClosureArgumentWithMultipleArguments),
("testEscapingClosureArgument", testEscapingClosureArgument),
("testPassingClosureAsArgument", testPassingClosureAsArgument),
("testNestedEscapingClosure", testNestedEscapingClosure)
("testNestedEscapingClosure", testNestedEscapingClosure),
("testClosureArgumentShorthands", testClosureArgumentShorthands)
]
}
}