@@ -204,9 +204,11 @@ class GodotMacroProcessor {
204204 ctor. append ( " \t classInfo.registerMethod(name: StringName( \" \( funcName) \" ), flags: .default, returnValue: \( retProp ?? " nil " ) , arguments: \( funcArgs == " " ? " [] " : " \( funcName) Args " ) , function: \( className) ._mproxy_ \( funcName) ) \n " )
205205 }
206206
207- func processVariable ( _ varDecl: VariableDeclSyntax , prefix: String ? ) throws {
207+ // Returns true if it used "tryCase"
208+ func processVariable ( _ varDecl: VariableDeclSyntax , prefix: String ? ) throws -> Bool {
209+ var usedTryCase = false
208210 guard hasExportAttribute ( varDecl. attributes) else {
209- return
211+ return false
210212 }
211213 guard let last = varDecl. bindings. last else {
212214 throw GodotMacroError . noVariablesFound
@@ -232,6 +234,13 @@ class GodotMacroProcessor {
232234 guard let ips = singleVar. pattern. as ( IdentifierPatternSyntax . self) else {
233235 throw GodotMacroError . expectedIdentifier ( singleVar)
234236 }
237+ guard let last = varDecl. bindings. last else {
238+ throw GodotMacroError . noVariablesFound
239+ }
240+ guard let ta = last. typeAnnotation? . type. description. trimmingCharacters ( in: CharacterSet . whitespacesAndNewlines) else {
241+ throw GodotMacroError . noTypeFound ( varDecl)
242+ }
243+
235244 let varNameWithPrefix = ips. identifier. text
236245 let varNameWithoutPrefix = String ( varNameWithPrefix. trimmingPrefix ( prefix ?? " " ) )
237246 let proxySetterName = " _mproxy_set_ \( varNameWithPrefix) "
@@ -275,16 +284,24 @@ class GodotMacroProcessor {
275284 }
276285 }
277286 }
278- let propType = godotTypeToProp ( typeName: typeName)
287+ let mappedType = godotTypeToProp ( typeName: typeName)
279288 let pinfo = " _p \( varNameWithPrefix) "
289+ let isEnum = firstLabeledExpression? . description == " enum "
290+
291+
292+ let propType = isEnum ? " .int " : mappedType
293+ let fallback = isEnum ? " tryCase ( \( ta) .self) " : " \" \" "
294+ if isEnum {
295+ usedTryCase = true
296+ }
280297 ctor. append (
281298 """
282299 let \( pinfo) = PropInfo (
283300 propertyType: \( propType) ,
284301 propertyName: " \( varNameWithPrefix) " ,
285302 className: className,
286303 hint: . \( firstLabeledExpression? . description ?? " none " ) ,
287- hintStr: \( secondLabeledExpression? . description ?? " \" \" " ) ,
304+ hintStr: \( secondLabeledExpression? . description ?? fallback ) ,
288305 usage: .default)
289306
290307 """ )
@@ -293,6 +310,10 @@ class GodotMacroProcessor {
293310 ctor. append ( " \t classInfo.registerMethod (name: \" \( setterName) \" , flags: .default, returnValue: nil, arguments: [ \( pinfo) ], function: \( className) . \( proxySetterName) ) \n " )
294311 ctor. append ( " \t classInfo.registerProperty ( \( pinfo) , getter: \" \( getterName) \" , setter: \" \( setterName) \" ) \n " )
295312 }
313+ if usedTryCase {
314+ return true
315+ }
316+ return false
296317 }
297318
298319 func processGArrayCollectionVariable( _ varDecl: VariableDeclSyntax , prefix: String ? ) throws {
@@ -402,7 +423,7 @@ class GodotMacroProcessor {
402423 """
403424 var previousGroupPrefix : String ? = nil
404425 var previousSubgroupPrefix : String ? = nil
405-
426+ var needTrycase = false
406427 for member in classDecl. memberBlock. members. enumerated ( ) {
407428 let decl = member. element. decl
408429
@@ -420,12 +441,23 @@ class GodotMacroProcessor {
420441 if varDecl. isGArrayCollection {
421442 try processGArrayCollectionVariable ( varDecl, prefix: previousSubgroupPrefix ?? previousGroupPrefix)
422443 } else {
423- try processVariable ( varDecl, prefix: previousSubgroupPrefix ?? previousGroupPrefix)
444+ if try processVariable ( varDecl, prefix: previousSubgroupPrefix ?? previousGroupPrefix) {
445+ needTrycase = true
446+ }
424447 }
425448 } else if let macroDecl = MacroExpansionDeclSyntax ( decl) {
426449 try classInitSignals ( macroDecl)
427450 }
428451 }
452+ if needTrycase {
453+ ctor. append (
454+ """
455+ func tryCase <T : RawRepresentable & CaseIterable> (_ type: T.Type) -> GString {
456+ GString (type.allCases.map { v in " \\ (v): \\ (v.rawValue) " }.joined(separator: " , " ))
457+ }
458+ func tryCase <T : RawRepresentable> (_ type: T.Type) -> String { " " }
459+ """ )
460+ }
429461 ctor. append ( " } () \n " )
430462 return ctor
431463 }
0 commit comments