|
| 1 | +// |
| 2 | +// Initializer.swift |
| 3 | +// SyntaxKit |
| 4 | +// |
| 5 | +// Created by Leo Dion. |
| 6 | +// Copyright © 2025 BrightDigit. |
| 7 | +// |
| 8 | +// Permission is hereby granted, free of charge, to any person |
| 9 | +// obtaining a copy of this software and associated documentation |
| 10 | +// files (the "Software"), to deal in the Software without |
| 11 | +// restriction, including without limitation the rights to use, |
| 12 | +// copy, modify, merge, publish, distribute, sublicense, and/or |
| 13 | +// sell copies of the Software, and to permit persons to whom the |
| 14 | +// Software is furnished to do so, subject to the following |
| 15 | +// conditions: |
| 16 | +// |
| 17 | +// The above copyright notice and this permission notice shall be |
| 18 | +// included in all copies or substantial portions of the Software. |
| 19 | +// |
| 20 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 21 | +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 22 | +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 23 | +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 24 | +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 25 | +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 26 | +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 27 | +// OTHER DEALINGS IN THE SOFTWARE. |
| 28 | +// |
| 29 | + |
| 30 | +public import SwiftSyntax |
| 31 | + |
| 32 | +/// A Swift `init` declaration. |
| 33 | +public struct Initializer: CodeBlock, Sendable { |
| 34 | + private let body: [any CodeBlock] |
| 35 | + private var accessModifier: AccessModifier? |
| 36 | + private var isAsync: Bool = false |
| 37 | + private var isThrowing: Bool = false |
| 38 | + |
| 39 | + /// The SwiftSyntax representation of this initializer declaration. |
| 40 | + public var syntax: any SyntaxProtocol { |
| 41 | + var modifiers: DeclModifierListSyntax = [] |
| 42 | + if let access = accessModifier { |
| 43 | + modifiers = DeclModifierListSyntax([ |
| 44 | + DeclModifierSyntax(name: .keyword(access.keyword, trailingTrivia: .space)) |
| 45 | + ]) |
| 46 | + } |
| 47 | + |
| 48 | + var effectSpecifiers: FunctionEffectSpecifiersSyntax? |
| 49 | + if isAsync || isThrowing { |
| 50 | + effectSpecifiers = FunctionEffectSpecifiersSyntax( |
| 51 | + asyncSpecifier: isAsync |
| 52 | + ? .keyword(.async, leadingTrivia: .space, trailingTrivia: .space) |
| 53 | + : nil, |
| 54 | + throwsSpecifier: isThrowing ? .keyword(.throws, leadingTrivia: .space) : nil |
| 55 | + ) |
| 56 | + } |
| 57 | + |
| 58 | + let bodyBlock = CodeBlockSyntax( |
| 59 | + leftBrace: .leftBraceToken(leadingTrivia: .space, trailingTrivia: .newline), |
| 60 | + statements: CodeBlockItemListSyntax( |
| 61 | + body.compactMap { item in |
| 62 | + var codeBlockItem: CodeBlockItemSyntax? |
| 63 | + if let decl = item.syntax.as(DeclSyntax.self) { |
| 64 | + codeBlockItem = CodeBlockItemSyntax(item: .decl(decl)) |
| 65 | + } else if let expr = item.syntax.as(ExprSyntax.self) { |
| 66 | + codeBlockItem = CodeBlockItemSyntax(item: .expr(expr)) |
| 67 | + } else if let stmt = item.syntax.as(StmtSyntax.self) { |
| 68 | + codeBlockItem = CodeBlockItemSyntax(item: .stmt(stmt)) |
| 69 | + } |
| 70 | + return codeBlockItem?.with(\.trailingTrivia, .newline) |
| 71 | + } |
| 72 | + ), |
| 73 | + rightBrace: .rightBraceToken(leadingTrivia: .newline) |
| 74 | + ) |
| 75 | + |
| 76 | + return InitializerDeclSyntax( |
| 77 | + modifiers: modifiers, |
| 78 | + initKeyword: .keyword(.`init`), |
| 79 | + signature: FunctionSignatureSyntax( |
| 80 | + parameterClause: FunctionParameterClauseSyntax( |
| 81 | + leftParen: .leftParenToken(), |
| 82 | + parameters: FunctionParameterListSyntax([]), |
| 83 | + rightParen: .rightParenToken() |
| 84 | + ), |
| 85 | + effectSpecifiers: effectSpecifiers |
| 86 | + ), |
| 87 | + body: bodyBlock |
| 88 | + ) |
| 89 | + } |
| 90 | + |
| 91 | + /// Creates an `init` declaration. |
| 92 | + /// - Parameter content: A ``CodeBlockBuilder`` that provides the body of the initializer. |
| 93 | + public init(@CodeBlockBuilderResult _ content: () throws -> [any CodeBlock]) rethrows { |
| 94 | + self.body = try content() |
| 95 | + } |
| 96 | + |
| 97 | + /// Sets the access modifier for the initializer declaration. |
| 98 | + /// - Parameter access: The access modifier. |
| 99 | + /// - Returns: A copy of the initializer with the access modifier set. |
| 100 | + public func access(_ access: AccessModifier) -> Self { |
| 101 | + var copy = self |
| 102 | + copy.accessModifier = access |
| 103 | + return copy |
| 104 | + } |
| 105 | + |
| 106 | + /// Marks the initializer as `throws`. |
| 107 | + /// - Returns: A copy of the initializer marked as `throws`. |
| 108 | + public func throwing() -> Self { |
| 109 | + var copy = self |
| 110 | + copy.isThrowing = true |
| 111 | + return copy |
| 112 | + } |
| 113 | + |
| 114 | + /// Marks the initializer as `async`. |
| 115 | + /// - Returns: A copy of the initializer marked as `async`. |
| 116 | + public func async() -> Self { |
| 117 | + var copy = self |
| 118 | + copy.isAsync = true |
| 119 | + return copy |
| 120 | + } |
| 121 | +} |
0 commit comments