Skip to content

Commit

Permalink
Improve syntax highlighting for property wrappers (#115)
Browse files Browse the repository at this point in the history
This patch makes property wrappers highlight correctly when annotated
with explicit types.
  • Loading branch information
JohnSundell authored Aug 3, 2020
1 parent bda4fe7 commit d951a09
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 2 deletions.
6 changes: 4 additions & 2 deletions Sources/Splash/Grammar/SwiftGrammar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
60 changes: 60 additions & 0 deletions Tests/SplashTests/Tests/DeclarationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -1232,8 +1290,10 @@ extension DeclarationTests {
("testPrefixFunctionDeclaration", testPrefixFunctionDeclaration),
("testEnumDeclarationWithSomeCase", testEnumDeclarationWithSomeCase),
("testIndirectEnumDeclaration", testIndirectEnumDeclaration),
("testPropertyWrapperDeclaration", testPropertyWrapperDeclaration),
("testWrappedPropertyDeclarations", testWrappedPropertyDeclarations),
("testWrappedPropertyDeclarationUsingNestedType", testWrappedPropertyDeclarationUsingNestedType),
("testWrappedPropertyDeclarationUsingExplicitType", testWrappedPropertyDeclarationUsingExplicitType),
("testGenericInitializerDeclaration", testGenericInitializerDeclaration)
]
}
Expand Down

0 comments on commit d951a09

Please sign in to comment.