Improve syntax highlighting for property wrappers (#115)

This patch makes property wrappers highlight correctly when annotated
with explicit types.
This commit is contained in:
John Sundell 2020-08-03 12:31:53 +02:00 committed by GitHub
parent bda4fe71b6
commit d951a0906c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 64 additions and 2 deletions

View File

@ -433,8 +433,10 @@ private extension SwiftGrammar {
}
// Handling generic lists for parameters, rather than declarations
if foundOpeningBracket && token.isAny(of: ":", ">:") {
return true
if foundOpeningBracket {
if token.isAny(of: ":", ">:") || token.first == "@" {
return true
}
}
guard !declarationKeywords.contains(token) else {

View File

@ -1093,6 +1093,33 @@ final class DeclarationTests: SyntaxHighlighterTestCase {
])
}
func testPropertyWrapperDeclaration() {
let components = highlighter.highlight("""
@propertyWrapper
struct Wrapped<Value> {
var wrappedValue: Value
}
""")
XCTAssertEqual(components, [
.token("@propertyWrapper", .keyword),
.whitespace("\n"),
.token("struct", .keyword),
.whitespace(" "),
.plainText("Wrapped<Value>"),
.whitespace(" "),
.plainText("{"),
.whitespace("\n "),
.token("var", .keyword),
.whitespace(" "),
.plainText("wrappedValue:"),
.whitespace(" "),
.token("Value", .type),
.whitespace("\n"),
.plainText("}")
])
}
func testWrappedPropertyDeclarations() {
let components = highlighter.highlight("""
struct User {
@ -1151,6 +1178,37 @@ final class DeclarationTests: SyntaxHighlighterTestCase {
])
}
func testWrappedPropertyDeclarationUsingExplicitType() {
let components = highlighter.highlight("""
struct Model {
@Wrapper<Bool>(key: "setting")
var setting
}
""")
XCTAssertEqual(components, [
.token("struct", .keyword),
.whitespace(" "),
.plainText("Model"),
.whitespace(" "),
.plainText("{"),
.whitespace("\n "),
.token("@Wrapper", .keyword),
.plainText("<"),
.token("Bool", .type),
.plainText(">(key:"),
.whitespace(" "),
.token(#""setting""#, .string),
.plainText(")"),
.whitespace("\n "),
.token("var", .keyword),
.whitespace(" "),
.plainText("setting"),
.whitespace("\n"),
.plainText("}")
])
}
func testGenericInitializerDeclaration() {
let components = highlighter.highlight("""
struct Box {
@ -1232,8 +1290,10 @@ extension DeclarationTests {
("testPrefixFunctionDeclaration", testPrefixFunctionDeclaration),
("testEnumDeclarationWithSomeCase", testEnumDeclarationWithSomeCase),
("testIndirectEnumDeclaration", testIndirectEnumDeclaration),
("testPropertyWrapperDeclaration", testPropertyWrapperDeclaration),
("testWrappedPropertyDeclarations", testWrappedPropertyDeclarations),
("testWrappedPropertyDeclarationUsingNestedType", testWrappedPropertyDeclarationUsingNestedType),
("testWrappedPropertyDeclarationUsingExplicitType", testWrappedPropertyDeclarationUsingExplicitType),
("testGenericInitializerDeclaration", testGenericInitializerDeclaration)
]
}