Correctly highlight associated types

This patch makes Splash correctly highlight associated types within
protocol declarations. Like other declarations, typed declared using
the `associatedtype` keyword should not be highlighted.
This commit is contained in:
John Sundell 2019-03-08 18:43:12 +01:00
parent 45740696d0
commit f9ad32a0a6
2 changed files with 34 additions and 1 deletions

View File

@ -236,7 +236,8 @@ private extension SwiftGrammar {
private let declarationKeywords: Set<String> = [ private let declarationKeywords: Set<String> = [
"class", "struct", "enum", "func", "class", "struct", "enum", "func",
"protocol", "typealias", "import" "protocol", "typealias", "import",
"associatedtype"
] ]
func matches(_ segment: Segment) -> Bool { func matches(_ segment: Segment) -> Bool {

View File

@ -291,6 +291,37 @@ final class DeclarationTests: SyntaxHighlighterTestCase {
]) ])
} }
func testProtocolDeclarationWithAssociatedTypes() {
let components = highlighter.highlight("""
protocol Task {
associatedtype Input
associatedtype Error: Swift.Error
}
""")
XCTAssertEqual(components, [
.token("protocol", .keyword),
.whitespace(" "),
.plainText("Task"),
.whitespace(" "),
.plainText("{"),
.whitespace("\n "),
.token("associatedtype", .keyword),
.whitespace(" "),
.plainText("Input"),
.whitespace("\n "),
.token("associatedtype", .keyword),
.whitespace(" "),
.plainText("Error:"),
.whitespace(" "),
.token("Swift", .type),
.plainText("."),
.token("Error", .type),
.whitespace("\n"),
.plainText("}")
])
}
func testExtensionDeclaration() { func testExtensionDeclaration() {
let components = highlighter.highlight("extension UIViewController { }") let components = highlighter.highlight("extension UIViewController { }")
@ -636,6 +667,7 @@ extension DeclarationTests {
("testClassDeclarationWithMultipleProtocolConformances", testClassDeclarationWithMultipleProtocolConformances), ("testClassDeclarationWithMultipleProtocolConformances", testClassDeclarationWithMultipleProtocolConformances),
("testSubclassDeclaration", testSubclassDeclaration), ("testSubclassDeclaration", testSubclassDeclaration),
("testProtocolDeclaration", testProtocolDeclaration), ("testProtocolDeclaration", testProtocolDeclaration),
("testProtocolDeclarationWithAssociatedTypes", testProtocolDeclarationWithAssociatedTypes),
("testExtensionDeclaration", testExtensionDeclaration), ("testExtensionDeclaration", testExtensionDeclaration),
("testExtensionDeclarationWithConstraint", testExtensionDeclarationWithConstraint), ("testExtensionDeclarationWithConstraint", testExtensionDeclarationWithConstraint),
("testLazyPropertyDeclaration", testLazyPropertyDeclaration), ("testLazyPropertyDeclaration", testLazyPropertyDeclaration),