Skip to content

Commit

Permalink
Special case MethodFlags so we get a nicer enum
Browse files Browse the repository at this point in the history
Fixes #23
  • Loading branch information
PadraigK committed Oct 2, 2023
1 parent 95e8661 commit ad629cb
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 12 deletions.
13 changes: 12 additions & 1 deletion Generator/Generator/StringOperations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,18 @@ extension String {
func dropPrefix(_ prefix: String) -> String {
guard hasPrefix(prefix) else { return self }
guard prefix != self else { return self }
let suffix = String(dropFirst(prefix.count))
let prefixSize: Int
// special-case for https://github.com/migueldeicaza/SwiftGodot/issues/23
if prefix == "METHOD_", contains("METHOD_FLAG") {
if self == "METHOD_FLAGS_DEFAULT" {
prefixSize = "METHOD_FLAGS_".count
} else {
prefixSize = "METHOD_FLAG_".count
}
} else {
prefixSize = prefix.count
}
let suffix = String(dropFirst(prefixSize))
return suffix.isValidSwiftName() ? suffix : self
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/SimpleExtension/Demo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ class SwiftSprite2: Sprite2D {
hintStr: "Some kind of food",
usage: .default)
]
classInfo.registerMethod(name: "demo_set_favorite_food", flags: .flagsDefault, returnValue: nil, arguments: foodArgs, function: SwiftSprite2.demoSetFavoriteFood)
classInfo.registerMethod(name: "demo_get_favorite_food", flags: .flagsDefault, returnValue: foodArgs [0], arguments: [], function: SwiftSprite2.demoGetFavoriteFood)
classInfo.registerMethod(name: "demo_set_favorite_food", flags: .default, returnValue: nil, arguments: foodArgs, function: SwiftSprite2.demoSetFavoriteFood)
classInfo.registerMethod(name: "demo_get_favorite_food", flags: .default, returnValue: foodArgs [0], arguments: [], function: SwiftSprite2.demoGetFavoriteFood)

let foodProp = PropInfo (propertyType: .string,
propertyName: "demo_favorite_food",
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftGodot/Core/SignalSupport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class SignalProxy: Object {

let s = ClassInfo<SignalProxy>(name: "SignalProxy")

s.registerMethod(name: SignalProxy.proxyName, flags: .flagsDefault, returnValue: nil, arguments: [], function: SignalProxy.proxyFunc)
s.registerMethod(name: SignalProxy.proxyName, flags: .default, returnValue: nil, arguments: [], function: SignalProxy.proxyFunc)
return true
} ()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,15 +250,15 @@ extension ClassInfo {

private func registerGetter(prefix: String, name: String, property: PropInfo, getter: @escaping ClassInfoFunction) {
registerMethod(name: StringName("\(prefix)_get_\(name)"),
flags: .flagsDefault,
flags: .default,
returnValue: property,
arguments: [],
function: getter)
}

private func registerSetter(prefix: String, name: String, property: PropInfo, setter: @escaping ClassInfoFunction) {
registerMethod(name: StringName("\(prefix)_set_\(name)"),
flags: .flagsDefault,
flags: .default,
returnValue: nil,
arguments: [property],
function: setter)
Expand Down
6 changes: 3 additions & 3 deletions Sources/SwiftGodotMacroLibrary/MacroGodot.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class GodotMacroProcessor {
funcArgs.append ("\t]\n")
}
ctor.append (funcArgs)
ctor.append ("\tclassInfo.registerMethod(name: StringName(\"\(funcName)\"), flags: .flagsDefault, returnValue: \(retProp ?? "nil"), arguments: \(funcArgs == "" ? "[]" : "\(funcName)Args"), function: \(className)._mproxy_\(funcName))")
ctor.append ("\tclassInfo.registerMethod(name: StringName(\"\(funcName)\"), flags: .default, returnValue: \(retProp ?? "nil"), arguments: \(funcArgs == "" ? "[]" : "\(funcName)Args"), function: \(className)._mproxy_\(funcName))")
}

func processVariable (_ varDecl: VariableDeclSyntax) throws {
Expand Down Expand Up @@ -151,8 +151,8 @@ class GodotMacroProcessor {
""")

ctor.append("\tclassInfo.registerMethod (name: \"\(getterName)\", flags: .flagsDefault, returnValue: \(pinfo), arguments: [], function: \(className).\(getterName))\n")
ctor.append("\tclassInfo.registerMethod (name: \"\(setterName)\", flags: .flagsDefault, returnValue: nil, arguments: [\(pinfo)], function: \(className).\(setterName))\n")
ctor.append("\tclassInfo.registerMethod (name: \"\(getterName)\", flags: .default, returnValue: \(pinfo), arguments: [], function: \(className).\(getterName))\n")
ctor.append("\tclassInfo.registerMethod (name: \"\(setterName)\", flags: .default, returnValue: nil, arguments: [\(pinfo)], function: \(className).\(setterName))\n")
ctor.append("\tclassInfo.registerProperty (\(pinfo), getter: \"\(getterName)\", setter: \"\(setterName)\")")
}
}
Expand Down
6 changes: 3 additions & 3 deletions Tests/SwiftGodotMacrosTests/MacroGodotTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ final class MacroGodotTests: XCTestCase {
static var _initClass: Void = {
let className = StringName("Castro")
let classInfo = ClassInfo<Castro> (name: className)
classInfo.registerMethod(name: StringName("deleteEpisode"), flags: .flagsDefault, returnValue: nil, arguments: [], function: Castro._mproxy_deleteEpisode)
classInfo.registerMethod(name: StringName("deleteEpisode"), flags: .default, returnValue: nil, arguments: [], function: Castro._mproxy_deleteEpisode)
} ()
}
""",
Expand Down Expand Up @@ -162,8 +162,8 @@ final class MacroGodotTests: XCTestCase {
hint: .none,
hintStr: "",
usage: .default)
classInfo.registerMethod (name: "_mproxy_get_goodName", flags: .flagsDefault, returnValue: _pgoodName, arguments: [], function: Hi._mproxy_get_goodName)
classInfo.registerMethod (name: "_mproxy_set_goodName", flags: .flagsDefault, returnValue: nil, arguments: [_pgoodName], function: Hi._mproxy_set_goodName)
classInfo.registerMethod (name: "_mproxy_get_goodName", flags: .default, returnValue: _pgoodName, arguments: [], function: Hi._mproxy_get_goodName)
classInfo.registerMethod (name: "_mproxy_set_goodName", flags: .default, returnValue: nil, arguments: [_pgoodName], function: Hi._mproxy_set_goodName)
classInfo.registerProperty (_pgoodName, getter: "_mproxy_get_goodName", setter: "_mproxy_set_goodName")
} ()
}
Expand Down

0 comments on commit ad629cb

Please sign in to comment.