Merge pull request #37 from JohnSundell/multiple-protocols
Correctly highlight types conforming to multiple protocols
This commit is contained in:
commit
48449ae5b1
@ -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 {
|
||||||
|
@ -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),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user