Skip to content

Commit

Permalink
Correctly highlight generic return types (#69)
Browse files Browse the repository at this point in the history
This change makes Splash correctly highlight generics that are returned
from a function.
  • Loading branch information
JohnSundell authored May 13, 2019
1 parent 4675ffe commit ec13df1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Sources/Splash/Grammar/SwiftGrammar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
28 changes: 28 additions & 0 deletions Tests/SplashTests/Tests/DeclarationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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> {}")

Expand Down Expand Up @@ -882,6 +909,7 @@ extension DeclarationTests {
("testGenericFunctionDeclarationWithSingleConstraint", testGenericFunctionDeclarationWithSingleConstraint),
("testGenericFunctionDeclarationWithMultipleConstraints", testGenericFunctionDeclarationWithMultipleConstraints),
("testGenericFunctionDeclarationWithGenericParameter", testGenericFunctionDeclarationWithGenericParameter),
("testFunctionDeclarationWithGenericReturnType", testFunctionDeclarationWithGenericReturnType),
("testGenericStructDeclaration", testGenericStructDeclaration),
("testClassDeclaration", testClassDeclaration),
("testCompactClassDeclarationWithInitializer", testCompactClassDeclarationWithInitializer),
Expand Down

0 comments on commit ec13df1

Please sign in to comment.