Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
lukepistrol committed Jun 14, 2023
2 parents b3b000b + baeaae6 commit 65aad4e
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ enum Symbols: String {
case circle
case circleFill = "circle.fill"
case shareIcon = "square.and.arrow.up"
case globe
}
```

Expand All @@ -53,6 +54,7 @@ enum Symbols: String {
case circle
case circleFill = "circle.fill"
case shareIcon = "square.and.arrow.up"
case globe

var image: Image {
Image(systemName: self.rawValue)
Expand All @@ -78,7 +80,9 @@ In your code you can then call a symbol:
var body: some View {
VStack {
Symbols.circleFill.image
Text("Hello, World!)
Label("Globe", systemImage: Symbols.globe.name)
// the above can also be written as
Label("Globe", systemImage: Symbols.globe())
}
}
```
Expand Down
23 changes: 23 additions & 0 deletions Sources/SFSymbolsMacro/SFSymbol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,28 @@
//
// Created by Lukas Pistrol on 14.06.23.
//

/**
A Swift macro for *"type-safe"* SF Symbols.
## Initialization
```swift
@SFSymbol
enum Symbols: String {
case globe
case circleFill = "circle.fill"
}
```
## Usage
```swift
var body: some View {
Symbols.circleFill.image // ~= Image(systemName: "circle.fill")
Label("Globe", systemImage: Symbols.globe.name) // ~= Label("Globe", systemImage: "globe")
// the above can also be written as
Label("Globe", systemImage: Symbols.globe())
}
```
*/
@attached(member, names: arbitrary)
public macro SFSymbol() = #externalMacro(module: "SFSymbolsMacroImpl", type: "SFSymbolMacro")
5 changes: 5 additions & 0 deletions Sources/SFSymbolsMacroImpl/SFSymbolsMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ public struct SFSymbolMacro: MemberMacro {
return NSImage(systemSymbolName: self.rawValue, accessibilityDescription: accessibilityDescription)!
}
#endif
""",
"""
func callAsFunction() -> String {
return self.rawValue
}
"""
]
}
Expand Down
25 changes: 25 additions & 0 deletions Tests/SFSymbolsMacroTests/SFSymbolsMacroTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ final class SFSymbolsMacroTests: XCTestCase {
return NSImage(systemSymbolName: self.rawValue, accessibilityDescription: accessibilityDescription)!
}
#endif
func callAsFunction() -> String {
return self.rawValue
}
}
""",
macros: testMacros
Expand Down Expand Up @@ -66,4 +69,26 @@ final class SFSymbolsMacroTests: XCTestCase {
macros: testMacros
)
}

func testInvalidSFSymbolErrorExplicit() {
assertMacroExpansion(
"""
@SFSymbol
enum Symbols: String {
case xyz = "xyz"
}
""",
expandedSource:
"""
enum Symbols: String {
case xyz = "xyz"
}
""",
diagnostics: [
.init(message: "\"xyz\" is not a valid SF Symbol.", line: 3, column: 5)
],
macros: testMacros
)
}
}

0 comments on commit 65aad4e

Please sign in to comment.