@@ -36,28 +36,32 @@ public struct Class: CodeBlock {
3636 private var inheritance : [ String ] = [ ]
3737 private var genericParameters : [ String ] = [ ]
3838 private var isFinal : Bool = false
39+ private var attributes : [ AttributeInfo ] = [ ]
3940
4041 /// Creates a `class` declaration.
4142 /// - Parameters:
4243 /// - name: The name of the class.
43- /// - generics: A list of generic parameters for the class.
4444 /// - content: A ``CodeBlockBuilder`` that provides the members of the class.
45- public init (
46- _ name: String ,
47- generics: [ String ] = [ ] ,
48- @CodeBlockBuilderResult _ content: ( ) -> [ CodeBlock ]
49- ) {
45+ public init ( _ name: String , @CodeBlockBuilderResult _ content: ( ) -> [ CodeBlock ] ) {
5046 self . name = name
5147 self . members = content ( )
52- self . genericParameters = generics
5348 }
5449
55- /// Sets one or more inherited types (superclass first followed by any protocols).
56- /// - Parameter types: The list of types to inherit from.
50+ /// Sets the generic parameters for the class.
51+ /// - Parameter generics: The list of generic parameter names.
52+ /// - Returns: A copy of the class with the generic parameters set.
53+ public func generic( _ generics: String ... ) -> Self {
54+ var copy = self
55+ copy. genericParameters = generics
56+ return copy
57+ }
58+
59+ /// Sets the inheritance for the class.
60+ /// - Parameter type: The type to inherit from.
5761 /// - Returns: A copy of the class with the inheritance set.
58- public func inherits( _ types : String ... ) -> Self {
62+ public func inherits( _ type : String ) -> Self {
5963 var copy = self
60- copy. inheritance = types
64+ copy. inheritance = [ type ]
6165 return copy
6266 }
6367
@@ -69,10 +73,24 @@ public struct Class: CodeBlock {
6973 return copy
7074 }
7175
76+ /// Adds an attribute to the class declaration.
77+ /// - Parameters:
78+ /// - attribute: The attribute name (without the @ symbol).
79+ /// - arguments: The arguments for the attribute, if any.
80+ /// - Returns: A copy of the class with the attribute added.
81+ public func attribute( _ attribute: String , arguments: [ String ] = [ ] ) -> Self {
82+ var copy = self
83+ copy. attributes. append ( AttributeInfo ( name: attribute, arguments: arguments) )
84+ return copy
85+ }
86+
7287 public var syntax : SyntaxProtocol {
7388 let classKeyword = TokenSyntax . keyword ( . class, trailingTrivia: . space)
7489 let identifier = TokenSyntax . identifier ( name)
7590
91+ // Build attributes
92+ let attributeList = buildAttributeList ( from: attributes)
93+
7694 // Generic parameter clause
7795 var genericParameterClause : GenericParameterClauseSyntax ?
7896 if !genericParameters. isEmpty {
@@ -139,6 +157,7 @@ public struct Class: CodeBlock {
139157 }
140158
141159 return ClassDeclSyntax (
160+ attributes: attributeList,
142161 modifiers: modifiers,
143162 classKeyword: classKeyword,
144163 name: identifier,
@@ -147,4 +166,51 @@ public struct Class: CodeBlock {
147166 memberBlock: memberBlock
148167 )
149168 }
169+
170+ private func buildAttributeList( from attributes: [ AttributeInfo ] ) -> AttributeListSyntax {
171+ if attributes. isEmpty {
172+ return AttributeListSyntax ( [ ] )
173+ }
174+
175+ let attributeElements = attributes. map { attribute in
176+ let arguments = attribute. arguments
177+
178+ var leftParen : TokenSyntax ?
179+ var rightParen : TokenSyntax ?
180+ var argumentsSyntax : AttributeSyntax . Arguments ?
181+
182+ if !arguments. isEmpty {
183+ leftParen = . leftParenToken( )
184+ rightParen = . rightParenToken( )
185+
186+ let argumentList = arguments. map { argument in
187+ DeclReferenceExprSyntax ( baseName: . identifier( argument) )
188+ }
189+
190+ argumentsSyntax = . argumentList(
191+ LabeledExprListSyntax (
192+ argumentList. enumerated ( ) . map { index, expr in
193+ var element = LabeledExprSyntax ( expression: ExprSyntax ( expr) )
194+ if index < argumentList. count - 1 {
195+ element = element. with ( \. trailingComma, . commaToken( trailingTrivia: . space) )
196+ }
197+ return element
198+ }
199+ )
200+ )
201+ }
202+
203+ return AttributeListSyntax . Element (
204+ AttributeSyntax (
205+ atSign: . atSignToken( ) ,
206+ attributeName: IdentifierTypeSyntax ( name: . identifier( attribute. name) ) ,
207+ leftParen: leftParen,
208+ arguments: argumentsSyntax,
209+ rightParen: rightParen
210+ )
211+ )
212+ }
213+
214+ return AttributeListSyntax ( attributeElements)
215+ }
150216}
0 commit comments