|
| 1 | +// |
| 2 | +// Closure+Signature.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 | +import SwiftSyntax |
| 31 | + |
| 32 | +extension Closure { |
| 33 | + /// Builds the signature for the closure. |
| 34 | + internal func buildSignature(captureClause: ClosureCaptureClauseSyntax?) |
| 35 | + -> ClosureSignatureSyntax? |
| 36 | + { |
| 37 | + guard needsSignature else { |
| 38 | + return nil |
| 39 | + } |
| 40 | + |
| 41 | + return ClosureSignatureSyntax( |
| 42 | + attributes: buildAttributeList(), |
| 43 | + capture: captureClause, |
| 44 | + parameterClause: buildParameterClause().map { .parameterClause($0) }, |
| 45 | + effectSpecifiers: nil, |
| 46 | + returnClause: buildReturnClause(), |
| 47 | + inKeyword: .keyword(.in, leadingTrivia: .space, trailingTrivia: .space) |
| 48 | + ) |
| 49 | + } |
| 50 | + |
| 51 | + /// Builds the attribute list for the closure signature. |
| 52 | + private func buildAttributeList() -> AttributeListSyntax { |
| 53 | + guard !attributes.isEmpty else { |
| 54 | + return AttributeListSyntax([]) |
| 55 | + } |
| 56 | + |
| 57 | + return AttributeListSyntax( |
| 58 | + attributes.enumerated().map { idx, attr in |
| 59 | + AttributeListSyntax.Element( |
| 60 | + AttributeSyntax( |
| 61 | + atSign: .atSignToken(), |
| 62 | + attributeName: IdentifierTypeSyntax( |
| 63 | + name: .identifier(attr.name), |
| 64 | + trailingTrivia: (capture.isEmpty || idx != attributes.count - 1) |
| 65 | + ? Trivia() : .space |
| 66 | + ), |
| 67 | + leftParen: nil, |
| 68 | + arguments: nil, |
| 69 | + rightParen: nil |
| 70 | + ) |
| 71 | + ) |
| 72 | + } |
| 73 | + ) |
| 74 | + } |
| 75 | + |
| 76 | + /// Builds the parameter clause for the closure signature. |
| 77 | + private func buildParameterClause() -> ClosureParameterClauseSyntax? { |
| 78 | + guard !parameters.isEmpty else { |
| 79 | + return nil |
| 80 | + } |
| 81 | + |
| 82 | + return ClosureParameterClauseSyntax( |
| 83 | + leftParen: .leftParenToken(), |
| 84 | + parameters: ClosureParameterListSyntax( |
| 85 | + parameters.map(buildParameterSyntax) |
| 86 | + ), |
| 87 | + rightParen: .rightParenToken() |
| 88 | + ) |
| 89 | + } |
| 90 | + |
| 91 | + /// Builds parameter syntax from a closure parameter. |
| 92 | + private func buildParameterSyntax(from param: ClosureParameter) -> ClosureParameterSyntax { |
| 93 | + ClosureParameterSyntax( |
| 94 | + attributes: AttributeListSyntax([]), |
| 95 | + firstName: .identifier(param.name), |
| 96 | + secondName: nil, |
| 97 | + colon: param.name.isEmpty ? nil : .colonToken(trailingTrivia: .space), |
| 98 | + type: param.type?.typeSyntax as? TypeSyntax, |
| 99 | + ellipsis: nil, |
| 100 | + trailingComma: nil |
| 101 | + ) |
| 102 | + } |
| 103 | + |
| 104 | + /// Builds the return clause for the closure signature. |
| 105 | + private func buildReturnClause() -> ReturnClauseSyntax? { |
| 106 | + returnType.map { |
| 107 | + ReturnClauseSyntax( |
| 108 | + arrow: .arrowToken(trailingTrivia: .space), |
| 109 | + type: $0.typeSyntax |
| 110 | + ) |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments