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:
parent
2b9e65e0d8
commit
d01f7a6b2f
@ -144,8 +144,10 @@ private extension SwiftGrammar {
|
|||||||
|
|
||||||
func matches(_ segment: Segment) -> Bool {
|
func matches(_ segment: Segment) -> Bool {
|
||||||
// Don't match against index-based closure arguments
|
// Don't match against index-based closure arguments
|
||||||
guard segment.tokens.previous != "$" else {
|
if let previous = segment.tokens.previous {
|
||||||
return false
|
guard !previous.hasSuffix("$") else {
|
||||||
|
return false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Integers can be separated using "_", so handle that
|
// Integers can be separated using "_", so handle that
|
||||||
@ -231,7 +233,7 @@ private extension SwiftGrammar {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return segment.tokens.next.isAny(of: "(", "()", "())", "(.", "({", "().", #"(\."#)
|
return segment.tokens.next?.starts(with: "(") ?? false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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() {
|
func testAllTestsRunOnLinux() {
|
||||||
XCTAssertTrue(TestCaseVerifier.verifyLinuxTests((type(of: self)).allTests))
|
XCTAssertTrue(TestCaseVerifier.verifyLinuxTests((type(of: self)).allTests))
|
||||||
}
|
}
|
||||||
@ -166,7 +195,8 @@ extension ClosureTests {
|
|||||||
("testClosureArgumentWithMultipleArguments", testClosureArgumentWithMultipleArguments),
|
("testClosureArgumentWithMultipleArguments", testClosureArgumentWithMultipleArguments),
|
||||||
("testEscapingClosureArgument", testEscapingClosureArgument),
|
("testEscapingClosureArgument", testEscapingClosureArgument),
|
||||||
("testPassingClosureAsArgument", testPassingClosureAsArgument),
|
("testPassingClosureAsArgument", testPassingClosureAsArgument),
|
||||||
("testNestedEscapingClosure", testNestedEscapingClosure)
|
("testNestedEscapingClosure", testNestedEscapingClosure),
|
||||||
|
("testClosureArgumentShorthands", testClosureArgumentShorthands)
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user