Merge pull request #37 from JohnSundell/multiple-protocols

Correctly highlight types conforming to multiple protocols
This commit is contained in:
John Sundell 2019-03-08 18:24:41 +01:00 committed by GitHub
commit 48449ae5b1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 1 deletions

View File

@ -257,11 +257,19 @@ private extension SwiftGrammar {
// In a generic declaration, only highlight constraints // In a generic declaration, only highlight constraints
if segment.tokens.previous.isAny(of: "<", ",") { if segment.tokens.previous.isAny(of: "<", ",") {
var foundOpeningBracket = false
// Since the declaration might be on another line, we have to walk // Since the declaration might be on another line, we have to walk
// backwards through all tokens until we've found enough information. // backwards through all tokens until we've found enough information.
for token in segment.tokens.all.reversed() { for token in segment.tokens.all.reversed() {
if !foundOpeningBracket && token == "<" {
foundOpeningBracket = true
}
guard !declarationKeywords.contains(token) else { guard !declarationKeywords.contains(token) else {
return false // If it turns out that we weren't in fact inside of a generic
// declaration, (lacking "<"), then highlight the type as normal.
return !foundOpeningBracket
} }
guard !keywords.contains(token) else { guard !keywords.contains(token) else {

View File

@ -221,6 +221,23 @@ final class DeclarationTests: SyntaxHighlighterTestCase {
]) ])
} }
func testClassDeclarationWithMultipleProtocolConformances() {
let components = highlighter.highlight("class MyClass: ProtocolA, ProtocolB {}")
XCTAssertEqual(components, [
.token("class", .keyword),
.whitespace(" "),
.plainText("MyClass:"),
.whitespace(" "),
.token("ProtocolA", .type),
.plainText(","),
.whitespace(" "),
.token("ProtocolB", .type),
.whitespace(" "),
.plainText("{}")
])
}
func testSubclassDeclaration() { func testSubclassDeclaration() {
let components = highlighter.highlight("class ViewController: UIViewController { }") let components = highlighter.highlight("class ViewController: UIViewController { }")
@ -616,6 +633,7 @@ extension DeclarationTests {
("testGenericStructDeclaration", testGenericStructDeclaration), ("testGenericStructDeclaration", testGenericStructDeclaration),
("testClassDeclaration", testClassDeclaration), ("testClassDeclaration", testClassDeclaration),
("testCompactClassDeclarationWithInitializer", testCompactClassDeclarationWithInitializer), ("testCompactClassDeclarationWithInitializer", testCompactClassDeclarationWithInitializer),
("testClassDeclarationWithMultipleProtocolConformances", testClassDeclarationWithMultipleProtocolConformances),
("testSubclassDeclaration", testSubclassDeclaration), ("testSubclassDeclaration", testSubclassDeclaration),
("testProtocolDeclaration", testProtocolDeclaration), ("testProtocolDeclaration", testProtocolDeclaration),
("testExtensionDeclaration", testExtensionDeclaration), ("testExtensionDeclaration", testExtensionDeclaration),