-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduces the first macro `@EnvironmentObservable` that removes code that is repeatable, and similar to swift `@Observable` ```swift @EnvironmentObservable final class Notes { @Persistent var name: String = "" var count: Int = 0 } ``` will expand to: <img width="734" alt="Screenshot 2023-12-31 at 13 57 02" src="https://github.com/MaximBazarov/Decide/assets/11208417/3f18a119-f2fb-44dd-afa9-2ba356771178">
- Loading branch information
1 parent
ffc2901
commit f96cb9d
Showing
6 changed files
with
125 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import DecideMacros | ||
|
||
@attached( | ||
member, | ||
names: named(environment), | ||
named(init(environment:)) | ||
) | ||
@attached(memberAttribute) | ||
@attached(extension, conformances: StateRoot) | ||
public macro EnvironmentObservable() = #externalMacro( | ||
module: "DecideMacros", | ||
type: "StorageMacro" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import XCTest | ||
import Decide | ||
|
||
final class Decide_Tests: XCTestCase { | ||
@EnvironmentObservable | ||
final class Notes { | ||
@Persistent | ||
var name: String = "" | ||
|
||
var count: Int = 0 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#if canImport(SwiftCompilerPlugin) | ||
import SwiftCompilerPlugin | ||
import SwiftSyntaxMacros | ||
|
||
@main | ||
struct DecideCompilerPlugin: CompilerPlugin { | ||
let providingMacros: [Macro.Type] = [ | ||
StorageMacro.self, | ||
// AtomicPropertyMacro.self, | ||
] | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import SwiftSyntax | ||
import SwiftSyntaxMacros | ||
|
||
public protocol ObservableState { | ||
init() | ||
} | ||
|
||
public struct StorageMacro: MemberMacro, MemberAttributeMacro { | ||
public static func expansion( | ||
of node: SwiftSyntax.AttributeSyntax, | ||
attachedTo declaration: some SwiftSyntax.DeclGroupSyntax, | ||
providingAttributesFor member: some SwiftSyntax.DeclSyntaxProtocol, | ||
in context: some SwiftSyntaxMacros.MacroExpansionContext | ||
) throws -> [SwiftSyntax.AttributeSyntax] { | ||
return [ | ||
AttributeSyntax( | ||
attributeName: IdentifierTypeSyntax( | ||
name: .identifier("ObservableValue") | ||
) | ||
) | ||
] | ||
} | ||
|
||
// MARK: - MemberMacro | ||
public static func expansion( | ||
of node: AttributeSyntax, | ||
providingMembersOf declaration: some DeclGroupSyntax, | ||
in context: some MacroExpansionContext | ||
) throws -> [DeclSyntax] { | ||
guard let _ = declaration.asProtocol(NamedDeclSyntax.self) else { | ||
return [] | ||
} | ||
|
||
let environment: DeclSyntax = | ||
""" | ||
public unowned let environment: Decide.SharedEnvironment | ||
public init(environment: Decide.SharedEnvironment) { | ||
self.environment = environment | ||
} | ||
""" | ||
|
||
return [environment] | ||
} | ||
} | ||
|
||
|
||
extension StorageMacro: ExtensionMacro { | ||
public static func expansion( | ||
of node: AttributeSyntax, | ||
attachedTo declaration: some DeclGroupSyntax, | ||
providingExtensionsOf type: some TypeSyntaxProtocol, | ||
conformingTo protocols: [TypeSyntax], | ||
in context: some MacroExpansionContext | ||
) throws -> [ExtensionDeclSyntax] { | ||
[try ExtensionDeclSyntax("extension \(type): Decide.StateRoot {}")] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters