Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions Sources/SyntaxKit/Declarations/Class.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public struct Class: CodeBlock, Sendable {
private var genericParameters: [String] = []
private var isFinal: Bool = false
private var attributes: [AttributeInfo] = []
private var accessModifier: AccessModifier?

/// The SwiftSyntax representation of this class declaration.
public var syntax: any SyntaxProtocol {
Expand Down Expand Up @@ -107,11 +108,16 @@ public struct Class: CodeBlock, Sendable {

// Modifiers
var modifiers: DeclModifierListSyntax = []
if isFinal {
if let access = accessModifier {
modifiers = DeclModifierListSyntax([
DeclModifierSyntax(name: .keyword(.final, trailingTrivia: .space))
DeclModifierSyntax(name: .keyword(access.keyword, trailingTrivia: .space))
])
}
if isFinal {
modifiers = DeclModifierListSyntax(
modifiers + [DeclModifierSyntax(name: .keyword(.final, trailingTrivia: .space))]
)
}

return ClassDeclSyntax(
attributes: attributeList,
Expand Down Expand Up @@ -161,6 +167,15 @@ public struct Class: CodeBlock, Sendable {
return copy
}

/// Sets the access modifier for the class declaration.
/// - Parameter access: The access modifier.
/// - Returns: A copy of the class with the access modifier set.
public func access(_ access: AccessModifier) -> Self {
var copy = self
copy.accessModifier = access
return copy
}

/// Adds an attribute to the class declaration.
/// - Parameters:
/// - attribute: The attribute name (without the @ symbol).
Expand Down Expand Up @@ -189,13 +204,13 @@ public struct Class: CodeBlock, Sendable {
rightParen = .rightParenToken()

let argumentList = arguments.map { argument in
DeclReferenceExprSyntax(baseName: .identifier(argument))
buildAttributeArgumentExpr(from: argument)
}

argumentsSyntax = .argumentList(
LabeledExprListSyntax(
argumentList.enumerated().map { index, expr in
var element = LabeledExprSyntax(expression: ExprSyntax(expr))
var element = LabeledExprSyntax(expression: expr)
if index < argumentList.count - 1 {
element = element.with(\.trailingComma, .commaToken(trailingTrivia: .space))
}
Expand All @@ -213,6 +228,7 @@ public struct Class: CodeBlock, Sendable {
arguments: argumentsSyntax,
rightParen: rightParen
)
.with(\.trailingTrivia, .newline)
)
}

Expand Down
5 changes: 3 additions & 2 deletions Sources/SyntaxKit/Declarations/Enum.swift
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,13 @@ public struct Enum: CodeBlock, Sendable {
rightParen = .rightParenToken()

let argumentList = arguments.map { argument in
DeclReferenceExprSyntax(baseName: .identifier(argument))
buildAttributeArgumentExpr(from: argument)
}

argumentsSyntax = .argumentList(
LabeledExprListSyntax(
argumentList.enumerated().map { index, expr in
var element = LabeledExprSyntax(expression: ExprSyntax(expr))
var element = LabeledExprSyntax(expression: expr)
if index < argumentList.count - 1 {
element = element.with(\.trailingComma, .commaToken(trailingTrivia: .space))
}
Expand All @@ -158,6 +158,7 @@ public struct Enum: CodeBlock, Sendable {
arguments: argumentsSyntax,
rightParen: rightParen
)
.with(\.trailingTrivia, .newline)
)
}
return AttributeListSyntax(attributeElements)
Expand Down
5 changes: 3 additions & 2 deletions Sources/SyntaxKit/Declarations/Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,13 @@ public struct Extension: CodeBlock, Sendable {
rightParen = .rightParenToken()

let argumentList = arguments.map { argument in
DeclReferenceExprSyntax(baseName: .identifier(argument))
buildAttributeArgumentExpr(from: argument)
}

argumentsSyntax = .argumentList(
LabeledExprListSyntax(
argumentList.enumerated().map { index, expr in
var element = LabeledExprSyntax(expression: ExprSyntax(expr))
var element = LabeledExprSyntax(expression: expr)
if index < argumentList.count - 1 {
element = element.with(\.trailingComma, .commaToken(trailingTrivia: .space))
}
Expand All @@ -154,6 +154,7 @@ public struct Extension: CodeBlock, Sendable {
arguments: argumentsSyntax,
rightParen: rightParen
)
.with(\.trailingTrivia, .newline)
)
}
return AttributeListSyntax(attributeElements)
Expand Down
81 changes: 81 additions & 0 deletions Sources/SyntaxKit/Declarations/IfCanImport.swift
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 {
Copy link
Copy Markdown
Member Author

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 #?

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)
)
}
}
5 changes: 3 additions & 2 deletions Sources/SyntaxKit/Declarations/Import.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,13 @@ public struct Import: CodeBlock, Sendable {
rightParen = .rightParenToken()

let argumentList = arguments.map { argument in
DeclReferenceExprSyntax(baseName: .identifier(argument))
buildAttributeArgumentExpr(from: argument)
}

argumentsSyntax = .argumentList(
LabeledExprListSyntax(
argumentList.enumerated().map { index, expr in
var element = LabeledExprSyntax(expression: ExprSyntax(expr))
var element = LabeledExprSyntax(expression: expr)
if index < argumentList.count - 1 {
element = element.with(\.trailingComma, .commaToken(trailingTrivia: .space))
}
Expand All @@ -124,6 +124,7 @@ public struct Import: CodeBlock, Sendable {
arguments: argumentsSyntax,
rightParen: rightParen
)
.with(\.trailingTrivia, .space)
)
}
return AttributeListSyntax(attributeElements)
Expand Down
121 changes: 121 additions & 0 deletions Sources/SyntaxKit/Declarations/Initializer.swift
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 {
Copy link
Copy Markdown
Member Author

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 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
}
}
5 changes: 3 additions & 2 deletions Sources/SyntaxKit/Declarations/Protocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,13 @@ public struct Protocol: CodeBlock, Sendable {
rightParen = .rightParenToken()

let argumentList = arguments.map { argument in
DeclReferenceExprSyntax(baseName: .identifier(argument))
buildAttributeArgumentExpr(from: argument)
}

argumentsSyntax = .argumentList(
LabeledExprListSyntax(
argumentList.enumerated().map { index, expr in
var element = LabeledExprSyntax(expression: ExprSyntax(expr))
var element = LabeledExprSyntax(expression: expr)
if index < argumentList.count - 1 {
element = element.with(\.trailingComma, .commaToken(trailingTrivia: .space))
}
Expand All @@ -159,6 +159,7 @@ public struct Protocol: CodeBlock, Sendable {
arguments: argumentsSyntax,
rightParen: rightParen
)
.with(\.trailingTrivia, .newline)
)
}
return AttributeListSyntax(attributeElements)
Expand Down
5 changes: 3 additions & 2 deletions Sources/SyntaxKit/Declarations/Struct.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@ public struct Struct: CodeBlock, Sendable {
rightParen = .rightParenToken()

let argumentList = arguments.map { argument in
DeclReferenceExprSyntax(baseName: .identifier(argument))
buildAttributeArgumentExpr(from: argument)
}

argumentsSyntax = .argumentList(
LabeledExprListSyntax(
argumentList.enumerated().map { index, expr in
var element = LabeledExprSyntax(expression: ExprSyntax(expr))
var element = LabeledExprSyntax(expression: expr)
if index < argumentList.count - 1 {
element = element.with(\.trailingComma, .commaToken(trailingTrivia: .space))
}
Expand All @@ -94,6 +94,7 @@ public struct Struct: CodeBlock, Sendable {
arguments: argumentsSyntax,
rightParen: rightParen
)
.with(\.trailingTrivia, .newline)
)
}
return AttributeListSyntax(attributeElements)
Expand Down
Loading
Loading