Skip to content

Commit b745d78

Browse files
committed
Categorize Macro Interfaces and Standardize Playground
This commit extends the categorization to macro interfaces and adds MARK comments and sorts examples in the main playground file for consistent organization and quicker navigation
1 parent e887f7c commit b745d78

File tree

7 files changed

+276
-178
lines changed

7 files changed

+276
-178
lines changed

Examples/Sources/MacroExamples/Implementation/Member/CustomCodable.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,14 @@ public enum CustomCodable: MemberMacro {
5454
return [codingKeys]
5555
}
5656
}
57+
58+
public struct CodableKey: PeerMacro {
59+
public static func expansion(
60+
of node: AttributeSyntax,
61+
providingPeersOf declaration: some DeclSyntaxProtocol,
62+
in context: some MacroExpansionContext
63+
) throws -> [DeclSyntax] {
64+
// Does nothing, used only to decorate members with data
65+
return []
66+
}
67+
}

Examples/Sources/MacroExamples/Interface/Macros.swift renamed to Examples/Sources/MacroExamples/Interface/ComplexMacros.swift

Lines changed: 4 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -10,46 +10,7 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
import Foundation
14-
15-
/// "Stringify" the provided value and produce a tuple that includes both the
16-
/// original value as well as the source code that generated it.
17-
@freestanding(expression) public macro stringify<T>(_ value: T) -> (T, String) = #externalMacro(module: "MacroExamplesImplementation", type: "StringifyMacro")
18-
19-
/// Macro that produces a warning on "+" operators within the expression, and
20-
/// suggests changing them to "-".
21-
@freestanding(expression) public macro addBlocker<T>(_ value: T) -> T = #externalMacro(module: "MacroExamplesImplementation", type: "AddBlocker")
22-
23-
/// Macro that produces a warning, as a replacement for the built-in
24-
/// #warning("...").
25-
@freestanding(expression) public macro myWarning(_ message: String) = #externalMacro(module: "MacroExamplesImplementation", type: "WarningMacro")
26-
27-
public enum FontWeight {
28-
case thin
29-
case normal
30-
case medium
31-
case semiBold
32-
case bold
33-
}
34-
35-
public protocol ExpressibleByFontLiteral {
36-
init(fontLiteralName: String, size: Int, weight: FontWeight)
37-
}
38-
39-
/// Font literal similar to, e.g., #colorLiteral.
40-
@freestanding(expression) public macro fontLiteral<T>(name: String, size: Int, weight: FontWeight) -> T =
41-
#externalMacro(module: "MacroExamplesImplementation", type: "FontLiteralMacro")
42-
where T: ExpressibleByFontLiteral
43-
44-
/// Check if provided string literal is a valid URL and produce a non-optional
45-
/// URL value. Emit error otherwise.
46-
@freestanding(expression) public macro URL(_ stringLiteral: String) -> URL = #externalMacro(module: "MacroExamplesImplementation", type: "URLMacro")
47-
48-
/// Apply the specified attribute to each of the stored properties within the
49-
/// type or member to which the macro is attached. The string can be
50-
/// any attribute (without the `@`).
51-
@attached(memberAttribute)
52-
public macro wrapStoredProperties(_ attributeName: String) = #externalMacro(module: "MacroExamplesImplementation", type: "WrapStoredPropertiesMacro")
13+
// MARK: - Dictionary Storage
5314

5415
/// Wrap up the stored properties of the given type in a dictionary,
5516
/// turning them into computed properties.
@@ -66,6 +27,8 @@ public macro DictionaryStorage() = #externalMacro(module: "MacroExamplesImplemen
6627
@attached(accessor)
6728
public macro DictionaryStorageProperty() = #externalMacro(module: "MacroExamplesImplementation", type: "DictionaryStoragePropertyMacro")
6829

30+
// MARK: - Observable
31+
6932
public protocol Observable {}
7033

7134
public protocol Observer<Subject> {
@@ -108,29 +71,7 @@ public macro Observable() = #externalMacro(module: "MacroExamplesImplementation"
10871
@attached(accessor)
10972
public macro ObservableProperty() = #externalMacro(module: "MacroExamplesImplementation", type: "ObservablePropertyMacro")
11073

111-
/// Adds a "completionHandler" variant of an async function, which creates a new
112-
/// task , calls thh original async function, and delivers its result to the completion
113-
/// handler.
114-
@attached(peer, names: overloaded)
115-
public macro AddCompletionHandler() =
116-
#externalMacro(module: "MacroExamplesImplementation", type: "AddCompletionHandlerMacro")
117-
118-
@attached(peer, names: overloaded)
119-
public macro AddAsync() =
120-
#externalMacro(module: "MacroExamplesImplementation", type: "AddAsyncMacro")
121-
122-
/// Add computed properties named `is<Case>` for each case element in the enum.
123-
@attached(member, names: arbitrary)
124-
public macro CaseDetection() = #externalMacro(module: "MacroExamplesImplementation", type: "CaseDetectionMacro")
125-
126-
@attached(member, names: named(Meta))
127-
public macro MetaEnum() = #externalMacro(module: "MacroExamplesImplementation", type: "MetaEnumMacro")
128-
129-
@attached(peer)
130-
public macro CodableKey(name: String) = #externalMacro(module: "MacroExamplesImplementation", type: "CodableKey")
131-
132-
@attached(member, names: named(CodingKeys))
133-
public macro CustomCodable() = #externalMacro(module: "MacroExamplesImplementation", type: "CustomCodable")
74+
// MARK: - Option Set
13475

13576
/// Create an option set from a struct that contains a nested `Options` enum.
13677
///
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2023 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 Foundation
14+
15+
// MARK: - Add Blocker
16+
17+
/// Macro that produces a warning on "+" operators within the expression, and
18+
/// suggests changing them to "-".
19+
@freestanding(expression)
20+
public macro addBlocker<T>(_ value: T) -> T = #externalMacro(module: "MacroExamplesImplementation", type: "AddBlocker")
21+
22+
// MARK: - Font Literal
23+
24+
public enum FontWeight {
25+
case thin
26+
case normal
27+
case medium
28+
case semiBold
29+
case bold
30+
}
31+
32+
public protocol ExpressibleByFontLiteral {
33+
init(fontLiteralName: String, size: Int, weight: FontWeight)
34+
}
35+
36+
/// Font literal similar to, e.g., #colorLiteral.
37+
@freestanding(expression)
38+
public macro fontLiteral<T>(name: String, size: Int, weight: FontWeight) -> T = #externalMacro(
39+
module: "MacroExamplesImplementation",
40+
type: "FontLiteralMacro"
41+
) where T: ExpressibleByFontLiteral
42+
43+
// MARK: - Stringify Macro
44+
45+
/// "Stringify" the provided value and produce a tuple that includes both the
46+
/// original value as well as the source code that generated it.
47+
@freestanding(expression)
48+
public macro stringify<T>(_ value: T) -> (T, String) = #externalMacro(module: "MacroExamplesImplementation", type: "StringifyMacro")
49+
50+
// MARK: - URL
51+
52+
/// Check if provided string literal is a valid URL and produce a non-optional
53+
/// URL value. Emit error otherwise.
54+
@freestanding(expression)
55+
public macro URL(_ stringLiteral: String) -> URL = #externalMacro(module: "MacroExamplesImplementation", type: "URLMacro")
56+
57+
// MARK: - Warning
58+
59+
/// Macro that produces a warning, as a replacement for the built-in
60+
/// #warning("...").
61+
@freestanding(expression)
62+
public macro myWarning(_ message: String) = #externalMacro(module: "MacroExamplesImplementation", type: "WarningMacro")

Examples/Sources/MacroExamples/Implementation/Peer/CodableKey.swift renamed to Examples/Sources/MacroExamples/Interface/MemberAttributeMacros.swift

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,10 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
import SwiftSyntax
14-
import SwiftSyntaxMacros
13+
// MARK: - Wrap Stored Properties
1514

16-
public struct CodableKey: PeerMacro {
17-
public static func expansion(
18-
of node: AttributeSyntax,
19-
providingPeersOf declaration: some DeclSyntaxProtocol,
20-
in context: some MacroExpansionContext
21-
) throws -> [DeclSyntax] {
22-
// Does nothing, used only to decorate members with data
23-
return []
24-
}
25-
}
15+
/// Apply the specified attribute to each of the stored properties within the
16+
/// type or member to which the macro is attached. The string can be
17+
/// any attribute (without the `@`).
18+
@attached(memberAttribute)
19+
public macro wrapStoredProperties(_ attributeName: String) = #externalMacro(module: "MacroExamplesImplementation", type: "WrapStoredPropertiesMacro")

Examples/Sources/MacroExamples/Interface/NewType.swift renamed to Examples/Sources/MacroExamples/Interface/MemberMacros.swift

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,28 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
@attached(
14-
member,
15-
names: named(RawValue),
16-
named(rawValue),
17-
arbitrary // should be named(init(_:)) but that doesn't compile as of swift-DEVELOPMENT-SNAPSHOT-2023-02-02-a
18-
)
13+
// MARK: - Case Detection
14+
15+
/// Add computed properties named `is<Case>` for each case element in the enum.
16+
@attached(member, names: arbitrary)
17+
public macro CaseDetection() = #externalMacro(module: "MacroExamplesImplementation", type: "CaseDetectionMacro")
18+
19+
// MARK: - Custom Codable
20+
21+
@attached(member, names: named(CodingKeys))
22+
public macro CustomCodable() = #externalMacro(module: "MacroExamplesImplementation", type: "CustomCodable")
23+
24+
@attached(peer)
25+
public macro CodableKey(name: String) = #externalMacro(module: "MacroExamplesImplementation", type: "CodableKey")
26+
27+
// MARK: - Meta Enum
28+
29+
@attached(member, names: named(Meta))
30+
public macro MetaEnum() = #externalMacro(module: "MacroExamplesImplementation", type: "MetaEnumMacro")
31+
32+
// MARK: - New Type
33+
34+
@attached(member, names: named(RawValue), named(rawValue), named(init(_:)))
1935
public macro NewType<T>(_: T.Type) = #externalMacro(module: "MacroExamplesImplementation", type: "NewTypeMacro")
2036

2137
public protocol NewTypeProtocol: RawRepresentable {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2023 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: - Add Async
14+
15+
@attached(peer, names: overloaded)
16+
public macro AddAsync() = #externalMacro(module: "MacroExamplesImplementation", type: "AddAsyncMacro")
17+
18+
// MARK: - Add Completion Handler
19+
20+
/// Adds a "completionHandler" variant of an async function, which creates a new
21+
/// task , calls thh original async function, and delivers its result to the completion
22+
/// handler.
23+
@attached(peer, names: overloaded)
24+
public macro AddCompletionHandler() = #externalMacro(module: "MacroExamplesImplementation", type: "AddCompletionHandlerMacro")

0 commit comments

Comments
 (0)