|
| 1 | +// |
| 2 | +// LiveViewMacro.swift |
| 3 | +// |
| 4 | +// |
| 5 | +// Created by Carson Katri on 7/6/23. |
| 6 | +// |
| 7 | + |
| 8 | +import SwiftCompilerPlugin |
| 9 | +import SwiftSyntax |
| 10 | +import SwiftSyntaxBuilder |
| 11 | +import SwiftSyntaxMacros |
| 12 | + |
| 13 | +public enum LiveViewMacro {} |
| 14 | + |
| 15 | +extension LiveViewMacro: ExpressionMacro { |
| 16 | + public static func expansion<Node, Context>( |
| 17 | + of node: Node, |
| 18 | + in context: Context |
| 19 | + ) throws -> ExprSyntax where Node : FreestandingMacroExpansionSyntax, Context : MacroExpansionContext { |
| 20 | + let registryName = context.makeUniqueName("Registry") |
| 21 | + |
| 22 | + guard let addons = try node.argumentList.last? |
| 23 | + .expression.as(ArrayExprSyntax.self)? |
| 24 | + .elements.map(transformAddon(_:)) |
| 25 | + else { throw LiveViewMacroError.invalidAddonsSyntax } |
| 26 | + |
| 27 | + let registries: DeclSyntax |
| 28 | + switch addons.count { |
| 29 | + case 0: |
| 30 | + throw LiveViewMacroError.missingAddons |
| 31 | + case 1: |
| 32 | + registries = "typealias Registries = \(addons.first!)" |
| 33 | + default: |
| 34 | + func multiRegistry(_ addons: some RandomAccessCollection<SimpleTypeIdentifierSyntax>) -> SimpleTypeIdentifierSyntax { |
| 35 | + switch addons.count { |
| 36 | + case 2: |
| 37 | + return SimpleTypeIdentifierSyntax( |
| 38 | + name: "_MultiRegistry", |
| 39 | + genericArgumentClause: .init(arguments: .init([ |
| 40 | + .init(argumentType: addons.first!, trailingComma: .commaToken()), |
| 41 | + .init(argumentType: addons.last!) |
| 42 | + ])) |
| 43 | + ) |
| 44 | + default: |
| 45 | + return SimpleTypeIdentifierSyntax( |
| 46 | + name: "_MultiRegistry", |
| 47 | + genericArgumentClause: .init(arguments: .init([ |
| 48 | + .init(argumentType: addons.first!, trailingComma: .commaToken()), |
| 49 | + .init(argumentType: multiRegistry(addons.dropFirst())) |
| 50 | + ])) |
| 51 | + ) |
| 52 | + } |
| 53 | + } |
| 54 | + registries = "typealias Registries = \(multiRegistry(addons))" |
| 55 | + } |
| 56 | + |
| 57 | + let liveViewArguments = node.argumentList |
| 58 | + .removingLast() |
| 59 | + .replacing(childAt: node.argumentList.count - 2, with: node.argumentList.removingLast().last!.with(\.trailingComma, nil)) |
| 60 | + |
| 61 | + return """ |
| 62 | + { () -> AnyView in |
| 63 | + enum \(registryName): AggregateRegistry { |
| 64 | + \(registries) |
| 65 | + } |
| 66 | + |
| 67 | + return AnyView(LiveView<\(registryName)>(\(liveViewArguments))) |
| 68 | + }() |
| 69 | + """ |
| 70 | + } |
| 71 | + |
| 72 | + private static func transformAddon(_ element: ArrayElementSyntax) throws -> SimpleTypeIdentifierSyntax { |
| 73 | + guard let registry = element.expression.as(MemberAccessExprSyntax.self)?.base?.as(SpecializeExprSyntax.self), |
| 74 | + let name = registry.expression.as(IdentifierExprSyntax.self) |
| 75 | + else { throw LiveViewMacroError.invalidAddonElement } |
| 76 | + return SimpleTypeIdentifierSyntax( |
| 77 | + name: name.identifier, |
| 78 | + genericArgumentClause: .init(.init(arguments: .init([.init(argumentType: SimpleTypeIdentifierSyntax(name: .identifier("Self")))]))) |
| 79 | + ) |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +enum LiveViewMacroError: Error, CustomStringConvertible { |
| 84 | + case invalidAddonsSyntax |
| 85 | + case invalidAddonElement |
| 86 | + case missingAddons |
| 87 | + |
| 88 | + var description: String { |
| 89 | + switch self { |
| 90 | + case .invalidAddonsSyntax: |
| 91 | + return "Invalid value specified for 'addons'. Expected a static array literal." |
| 92 | + case .invalidAddonElement: |
| 93 | + return "Invalid addon provided. Expected a specialized registry type, such as 'AddonRegistry<Self>.self'" |
| 94 | + case .missingAddons: |
| 95 | + return "'addons' must not be empty." |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments