Skip to content

Commit d951a09

Browse files
authored
Improve syntax highlighting for property wrappers (#115)
This patch makes property wrappers highlight correctly when annotated with explicit types.
1 parent bda4fe7 commit d951a09

2 files changed

Lines changed: 64 additions & 2 deletions

File tree

Sources/Splash/Grammar/SwiftGrammar.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -433,8 +433,10 @@ private extension SwiftGrammar {
433433
}
434434

435435
// Handling generic lists for parameters, rather than declarations
436-
if foundOpeningBracket && token.isAny(of: ":", ">:") {
437-
return true
436+
if foundOpeningBracket {
437+
if token.isAny(of: ":", ">:") || token.first == "@" {
438+
return true
439+
}
438440
}
439441

440442
guard !declarationKeywords.contains(token) else {

Tests/SplashTests/Tests/DeclarationTests.swift

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,6 +1093,33 @@ final class DeclarationTests: SyntaxHighlighterTestCase {
10931093
])
10941094
}
10951095

1096+
func testPropertyWrapperDeclaration() {
1097+
let components = highlighter.highlight("""
1098+
@propertyWrapper
1099+
struct Wrapped<Value> {
1100+
var wrappedValue: Value
1101+
}
1102+
""")
1103+
1104+
XCTAssertEqual(components, [
1105+
.token("@propertyWrapper", .keyword),
1106+
.whitespace("\n"),
1107+
.token("struct", .keyword),
1108+
.whitespace(" "),
1109+
.plainText("Wrapped<Value>"),
1110+
.whitespace(" "),
1111+
.plainText("{"),
1112+
.whitespace("\n "),
1113+
.token("var", .keyword),
1114+
.whitespace(" "),
1115+
.plainText("wrappedValue:"),
1116+
.whitespace(" "),
1117+
.token("Value", .type),
1118+
.whitespace("\n"),
1119+
.plainText("}")
1120+
])
1121+
}
1122+
10961123
func testWrappedPropertyDeclarations() {
10971124
let components = highlighter.highlight("""
10981125
struct User {
@@ -1151,6 +1178,37 @@ final class DeclarationTests: SyntaxHighlighterTestCase {
11511178
])
11521179
}
11531180

1181+
func testWrappedPropertyDeclarationUsingExplicitType() {
1182+
let components = highlighter.highlight("""
1183+
struct Model {
1184+
@Wrapper<Bool>(key: "setting")
1185+
var setting
1186+
}
1187+
""")
1188+
1189+
XCTAssertEqual(components, [
1190+
.token("struct", .keyword),
1191+
.whitespace(" "),
1192+
.plainText("Model"),
1193+
.whitespace(" "),
1194+
.plainText("{"),
1195+
.whitespace("\n "),
1196+
.token("@Wrapper", .keyword),
1197+
.plainText("<"),
1198+
.token("Bool", .type),
1199+
.plainText(">(key:"),
1200+
.whitespace(" "),
1201+
.token(#""setting""#, .string),
1202+
.plainText(")"),
1203+
.whitespace("\n "),
1204+
.token("var", .keyword),
1205+
.whitespace(" "),
1206+
.plainText("setting"),
1207+
.whitespace("\n"),
1208+
.plainText("}")
1209+
])
1210+
}
1211+
11541212
func testGenericInitializerDeclaration() {
11551213
let components = highlighter.highlight("""
11561214
struct Box {
@@ -1232,8 +1290,10 @@ extension DeclarationTests {
12321290
("testPrefixFunctionDeclaration", testPrefixFunctionDeclaration),
12331291
("testEnumDeclarationWithSomeCase", testEnumDeclarationWithSomeCase),
12341292
("testIndirectEnumDeclaration", testIndirectEnumDeclaration),
1293+
("testPropertyWrapperDeclaration", testPropertyWrapperDeclaration),
12351294
("testWrappedPropertyDeclarations", testWrappedPropertyDeclarations),
12361295
("testWrappedPropertyDeclarationUsingNestedType", testWrappedPropertyDeclarationUsingNestedType),
1296+
("testWrappedPropertyDeclarationUsingExplicitType", testWrappedPropertyDeclarationUsingExplicitType),
12371297
("testGenericInitializerDeclaration", testGenericInitializerDeclaration)
12381298
]
12391299
}

0 commit comments

Comments
 (0)