Correctly highlight generic return types (#69)

This change makes Splash correctly highlight generics that are returned
from a function.
This commit is contained in:
John Sundell 2019-05-13 11:25:16 +02:00 committed by GitHub
parent 4675ffe963
commit ec13df124a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 0 deletions

View File

@ -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
}

View File

@ -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),