File tree 6 files changed +150
-0
lines changed
Tests/MacroExamples/Implementation/Accessor
6 files changed +150
-0
lines changed Original file line number Diff line number Diff line change
1
+ //===----------------------------------------------------------------------===//
2
+ //
3
+ // This source file is part of the Swift.org open source project
4
+ //
5
+ // Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors
6
+ // Licensed under Apache License v2.0 with Runtime Library Exception
7
+ //
8
+ // See https://swift.org/LICENSE.txt for license information
9
+ // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10
+ //
11
+ //===----------------------------------------------------------------------===//
12
+
13
+ import SwiftSyntax
14
+ import SwiftSyntaxMacros
15
+
16
+ public struct EnvironmentValueMacro : AccessorMacro {
17
+ public static func expansion(
18
+ of node: AttributeSyntax ,
19
+ providingAccessorsOf declaration: some DeclSyntaxProtocol ,
20
+ in context: some MacroExpansionContext
21
+ ) throws -> [ AccessorDeclSyntax ] {
22
+ guard
23
+ case let . argumentList( arguments) = node. arguments,
24
+ let argument = arguments. first
25
+ else { return [ ] }
26
+
27
+ return [
28
+ """
29
+ get { self[ \( argument. expression) ] }
30
+ """ ,
31
+ """
32
+ set { self[ \( argument. expression) ] = newValue }
33
+ """ ,
34
+ ]
35
+ }
36
+ }
Original file line number Diff line number Diff line change @@ -26,6 +26,7 @@ struct MyPlugin: CompilerPlugin {
26
26
DefaultFatalErrorImplementationMacro . self,
27
27
DictionaryStorageMacro . self,
28
28
DictionaryStoragePropertyMacro . self,
29
+ EnvironmentValueMacro . self,
29
30
EquatableExtensionMacro . self,
30
31
FontLiteralMacro . self,
31
32
FuncUniqueMacro . self,
Original file line number Diff line number Diff line change
1
+ //===----------------------------------------------------------------------===//
2
+ //
3
+ // This source file is part of the Swift.org open source project
4
+ //
5
+ // Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors
6
+ // Licensed under Apache License v2.0 with Runtime Library Exception
7
+ //
8
+ // See https://swift.org/LICENSE.txt for license information
9
+ // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10
+ //
11
+ //===----------------------------------------------------------------------===//
12
+
13
+ #if canImport(SwiftUI)
14
+
15
+ import SwiftUI
16
+
17
+ // MARK: - EnvironmentValue Accessor
18
+
19
+ /// Adds getter / setter to an attached environment value with specified EnvironmentKey
20
+ @attached ( accessor)
21
+ public macro EnvironmentValue( for key: any EnvironmentKey . Type ) =
22
+ #externalMacro( module: " MacroExamplesImplementation " , type: " EnvironmentValueMacro " )
23
+
24
+ #endif
Original file line number Diff line number Diff line change
1
+ //===----------------------------------------------------------------------===//
2
+ //
3
+ // This source file is part of the Swift.org open source project
4
+ //
5
+ // Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors
6
+ // Licensed under Apache License v2.0 with Runtime Library Exception
7
+ //
8
+ // See https://swift.org/LICENSE.txt for license information
9
+ // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10
+ //
11
+ //===----------------------------------------------------------------------===//
12
+
13
+ // MARK: - EnvironmentValue Accessor
14
+
15
+ import MacroExamplesInterface
16
+
17
+ #if canImport(SwiftUI)
18
+
19
+ import SwiftUI
20
+
21
+ private struct MyEnvironmentKey : EnvironmentKey {
22
+ static let defaultValue : String = " Default value "
23
+ }
24
+
25
+ extension EnvironmentValues {
26
+ @EnvironmentValue ( for: MyEnvironmentKey . self)
27
+ var myCustomValue : String
28
+ }
29
+
30
+ func runEnvironmentValueAccessorMacroPlayground( ) {
31
+ var environmentValues = EnvironmentValues ( )
32
+ print ( " Default myCustomValue: \( environmentValues. myCustomValue) " )
33
+ environmentValues. myCustomValue = " New value "
34
+ print ( " New myCustomValue: \( environmentValues. myCustomValue) " )
35
+ }
36
+
37
+ #endif
Original file line number Diff line number Diff line change @@ -47,3 +47,11 @@ runMemberMacrosPlayground()
47
47
// MARK: - Peer Macros
48
48
49
49
runPeerMacrosPlayground ( )
50
+
51
+ // MARK: - Accessor Macros
52
+
53
+ #if canImport(SwiftUI)
54
+
55
+ runEnvironmentValueAccessorMacroPlayground ( )
56
+
57
+ #endif
Original file line number Diff line number Diff line change
1
+ //===----------------------------------------------------------------------===//
2
+ //
3
+ // This source file is part of the Swift.org open source project
4
+ //
5
+ // Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors
6
+ // Licensed under Apache License v2.0 with Runtime Library Exception
7
+ //
8
+ // See https://swift.org/LICENSE.txt for license information
9
+ // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10
+ //
11
+ //===----------------------------------------------------------------------===//
12
+
13
+ import MacroExamplesImplementation
14
+ import SwiftSyntaxMacros
15
+ import SwiftSyntaxMacrosTestSupport
16
+ import XCTest
17
+
18
+ final class EnvironmentValueMacroMacroTests : XCTestCase {
19
+ private let macros = [ " EnvironmentValue " : EnvironmentValueMacro . self]
20
+
21
+ func testEnvironmentValue( ) {
22
+ assertMacroExpansion (
23
+ """
24
+ extension EnvironmentValues {
25
+ @EnvironmentValue(for: MyEnvironmentKey.self)
26
+ var myCustomValue: String
27
+ }
28
+ """ ,
29
+ expandedSource: """
30
+ extension EnvironmentValues {
31
+ var myCustomValue: String {
32
+ get {
33
+ self[MyEnvironmentKey.self]
34
+ }
35
+ set {
36
+ self[MyEnvironmentKey.self] = newValue
37
+ }
38
+ }
39
+ }
40
+ """ ,
41
+ macros: macros
42
+ )
43
+ }
44
+ }
You can’t perform that action at this time.
0 commit comments