Correctly highlight generic return types (#69)
This change makes Splash correctly highlight generics that are returned from a function.
This commit is contained in:
parent
4675ffe963
commit
ec13df124a
|
@ -326,6 +326,11 @@ private extension SwiftGrammar {
|
|||
// Since the declaration might be on another line, we have to walk
|
||||
// backwards through all tokens until we've found enough information.
|
||||
for token in segment.tokens.all.reversed() {
|
||||
// Highlight return type generics as normal
|
||||
if token == "->" {
|
||||
return true
|
||||
}
|
||||
|
||||
if !foundOpeningBracket && token == "<" {
|
||||
foundOpeningBracket = true
|
||||
}
|
||||
|
|
|
@ -197,6 +197,33 @@ final class DeclarationTests: SyntaxHighlighterTestCase {
|
|||
])
|
||||
}
|
||||
|
||||
func testFunctionDeclarationWithGenericReturnType() {
|
||||
let components = highlighter.highlight("""
|
||||
func array() -> Array<Element> { return [] }
|
||||
""")
|
||||
|
||||
XCTAssertEqual(components, [
|
||||
.token("func", .keyword),
|
||||
.whitespace(" "),
|
||||
.plainText("array()"),
|
||||
.whitespace(" "),
|
||||
.plainText("->"),
|
||||
.whitespace(" "),
|
||||
.token("Array", .type),
|
||||
.plainText("<"),
|
||||
.token("Element", .type),
|
||||
.plainText(">"),
|
||||
.whitespace(" "),
|
||||
.plainText("{"),
|
||||
.whitespace(" "),
|
||||
.token("return", .keyword),
|
||||
.whitespace(" "),
|
||||
.plainText("[]"),
|
||||
.whitespace(" "),
|
||||
.plainText("}")
|
||||
])
|
||||
}
|
||||
|
||||
func testGenericStructDeclaration() {
|
||||
let components = highlighter.highlight("struct MyStruct<A: Hello, B> {}")
|
||||
|
||||
|
@ -882,6 +909,7 @@ extension DeclarationTests {
|
|||
("testGenericFunctionDeclarationWithSingleConstraint", testGenericFunctionDeclarationWithSingleConstraint),
|
||||
("testGenericFunctionDeclarationWithMultipleConstraints", testGenericFunctionDeclarationWithMultipleConstraints),
|
||||
("testGenericFunctionDeclarationWithGenericParameter", testGenericFunctionDeclarationWithGenericParameter),
|
||||
("testFunctionDeclarationWithGenericReturnType", testFunctionDeclarationWithGenericReturnType),
|
||||
("testGenericStructDeclaration", testGenericStructDeclaration),
|
||||
("testClassDeclaration", testClassDeclaration),
|
||||
("testCompactClassDeclarationWithInitializer", testCompactClassDeclarationWithInitializer),
|
||||
|
|
Loading…
Reference in New Issue