-
Notifications
You must be signed in to change notification settings - Fork 1
Changes from Swift-App-Template #125
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
leogdion
wants to merge
1
commit into
v0.0.4
Choose a base branch
from
swift-app-template
base: v0.0.4
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| // | ||
| // IfCanImport.swift | ||
| // SyntaxKit | ||
| // | ||
| // Created by Leo Dion. | ||
| // Copyright © 2025 BrightDigit. | ||
| // | ||
| // Permission is hereby granted, free of charge, to any person | ||
| // obtaining a copy of this software and associated documentation | ||
| // files (the "Software"), to deal in the Software without | ||
| // restriction, including without limitation the rights to use, | ||
| // copy, modify, merge, publish, distribute, sublicense, and/or | ||
| // sell copies of the Software, and to permit persons to whom the | ||
| // Software is furnished to do so, subject to the following | ||
| // conditions: | ||
| // | ||
| // The above copyright notice and this permission notice shall be | ||
| // included in all copies or substantial portions of the Software. | ||
| // | ||
| // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
| // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES | ||
| // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
| // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | ||
| // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | ||
| // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
| // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | ||
| // OTHER DEALINGS IN THE SOFTWARE. | ||
| // | ||
|
|
||
| public import SwiftSyntax | ||
|
|
||
| /// A `#if canImport(Module)` … `#endif` conditional compilation block. | ||
| public struct IfCanImport: CodeBlock, Sendable { | ||
| private let moduleName: String | ||
| private let content: [any CodeBlock] | ||
|
|
||
| /// Creates a `#if canImport(moduleName)` block wrapping the given content. | ||
| /// - Parameters: | ||
| /// - moduleName: The module name passed to `canImport`. | ||
| /// - content: The code blocks to emit when the module is available. | ||
| public init(_ moduleName: String, @CodeBlockBuilderResult _ content: () -> [any CodeBlock]) { | ||
| self.moduleName = moduleName | ||
| self.content = content() | ||
| } | ||
|
|
||
| public var syntax: any SyntaxProtocol { | ||
| let condition = FunctionCallExprSyntax( | ||
| calledExpression: ExprSyntax(DeclReferenceExprSyntax( | ||
| baseName: .identifier("canImport") | ||
| )), | ||
| leftParen: .leftParenToken(), | ||
| arguments: LabeledExprListSyntax([ | ||
| LabeledExprSyntax( | ||
| expression: ExprSyntax(DeclReferenceExprSyntax( | ||
| baseName: .identifier(moduleName) | ||
| )) | ||
| ) | ||
| ]), | ||
| rightParen: .rightParenToken() | ||
| ) | ||
|
|
||
| let items = CodeBlockItemListSyntax( | ||
| content.compactMap { block -> CodeBlockItemSyntax? in | ||
| CodeBlockItemSyntax.Item.create(from: block.syntax).map { | ||
| CodeBlockItemSyntax(item: $0, trailingTrivia: .newline) | ||
| } | ||
| } | ||
| ) | ||
|
|
||
| let clause = IfConfigClauseSyntax( | ||
| poundKeyword: .poundIfToken(trailingTrivia: .space), | ||
| condition: ExprSyntax(condition).with(\.trailingTrivia, .newline), | ||
| elements: .statements(items) | ||
| ) | ||
|
|
||
| return IfConfigDeclSyntax( | ||
| clauses: IfConfigClauseListSyntax([clause]), | ||
| poundEndif: .poundEndifToken(leadingTrivia: .newline) | ||
| ) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| // | ||
| // Initializer.swift | ||
| // SyntaxKit | ||
| // | ||
| // Created by Leo Dion. | ||
| // Copyright © 2025 BrightDigit. | ||
| // | ||
| // Permission is hereby granted, free of charge, to any person | ||
| // obtaining a copy of this software and associated documentation | ||
| // files (the "Software"), to deal in the Software without | ||
| // restriction, including without limitation the rights to use, | ||
| // copy, modify, merge, publish, distribute, sublicense, and/or | ||
| // sell copies of the Software, and to permit persons to whom the | ||
| // Software is furnished to do so, subject to the following | ||
| // conditions: | ||
| // | ||
| // The above copyright notice and this permission notice shall be | ||
| // included in all copies or substantial portions of the Software. | ||
| // | ||
| // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
| // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES | ||
| // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
| // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | ||
| // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | ||
| // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
| // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | ||
| // OTHER DEALINGS IN THE SOFTWARE. | ||
| // | ||
|
|
||
| public import SwiftSyntax | ||
|
|
||
| /// A Swift `init` declaration. | ||
| public struct Initializer: CodeBlock, Sendable { | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. don't we already have this? |
||
| private let body: [any CodeBlock] | ||
| private var accessModifier: AccessModifier? | ||
| private var isAsync: Bool = false | ||
| private var isThrowing: Bool = false | ||
|
|
||
| /// The SwiftSyntax representation of this initializer declaration. | ||
| public var syntax: any SyntaxProtocol { | ||
| var modifiers: DeclModifierListSyntax = [] | ||
| if let access = accessModifier { | ||
| modifiers = DeclModifierListSyntax([ | ||
| DeclModifierSyntax(name: .keyword(access.keyword, trailingTrivia: .space)) | ||
| ]) | ||
| } | ||
|
|
||
| var effectSpecifiers: FunctionEffectSpecifiersSyntax? | ||
| if isAsync || isThrowing { | ||
| effectSpecifiers = FunctionEffectSpecifiersSyntax( | ||
| asyncSpecifier: isAsync | ||
| ? .keyword(.async, leadingTrivia: .space, trailingTrivia: .space) | ||
| : nil, | ||
| throwsSpecifier: isThrowing ? .keyword(.throws, leadingTrivia: .space) : nil | ||
| ) | ||
| } | ||
|
|
||
| let bodyBlock = CodeBlockSyntax( | ||
| leftBrace: .leftBraceToken(leadingTrivia: .space, trailingTrivia: .newline), | ||
| statements: CodeBlockItemListSyntax( | ||
| body.compactMap { item in | ||
| var codeBlockItem: CodeBlockItemSyntax? | ||
| if let decl = item.syntax.as(DeclSyntax.self) { | ||
| codeBlockItem = CodeBlockItemSyntax(item: .decl(decl)) | ||
| } else if let expr = item.syntax.as(ExprSyntax.self) { | ||
| codeBlockItem = CodeBlockItemSyntax(item: .expr(expr)) | ||
| } else if let stmt = item.syntax.as(StmtSyntax.self) { | ||
| codeBlockItem = CodeBlockItemSyntax(item: .stmt(stmt)) | ||
| } | ||
| return codeBlockItem?.with(\.trailingTrivia, .newline) | ||
| } | ||
| ), | ||
| rightBrace: .rightBraceToken(leadingTrivia: .newline) | ||
| ) | ||
|
|
||
| return InitializerDeclSyntax( | ||
| modifiers: modifiers, | ||
| initKeyword: .keyword(.`init`), | ||
| signature: FunctionSignatureSyntax( | ||
| parameterClause: FunctionParameterClauseSyntax( | ||
| leftParen: .leftParenToken(), | ||
| parameters: FunctionParameterListSyntax([]), | ||
| rightParen: .rightParenToken() | ||
| ), | ||
| effectSpecifiers: effectSpecifiers | ||
| ), | ||
| body: bodyBlock | ||
| ) | ||
| } | ||
|
|
||
| /// Creates an `init` declaration. | ||
| /// - Parameter content: A ``CodeBlockBuilder`` that provides the body of the initializer. | ||
| public init(@CodeBlockBuilderResult _ content: () throws -> [any CodeBlock]) rethrows { | ||
| self.body = try content() | ||
| } | ||
|
|
||
| /// Sets the access modifier for the initializer declaration. | ||
| /// - Parameter access: The access modifier. | ||
| /// - Returns: A copy of the initializer with the access modifier set. | ||
| public func access(_ access: AccessModifier) -> Self { | ||
| var copy = self | ||
| copy.accessModifier = access | ||
| return copy | ||
| } | ||
|
|
||
| /// Marks the initializer as `throws`. | ||
| /// - Returns: A copy of the initializer marked as `throws`. | ||
| public func throwing() -> Self { | ||
| var copy = self | ||
| copy.isThrowing = true | ||
| return copy | ||
| } | ||
|
|
||
| /// Marks the initializer as `async`. | ||
| /// - Returns: A copy of the initializer marked as `async`. | ||
| public func async() -> Self { | ||
| var copy = self | ||
| copy.isAsync = true | ||
| return copy | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
don't we already have a pound
#?