Skip to content

Commit ac1103c

Browse files
author
Stephane Magne
authored
Merge pull request #9 from scribd/stephane/objc_custom_modifiers
Add prefix naming option to objc types.
2 parents d439993 + 0766840 commit ac1103c

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

Sources/Meta/Type.swift

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,8 @@ public struct Type: Hashable, FileBodyMember, TypeBodyMember {
194194

195195
public var objc = false
196196

197+
public var objcPrefix: String?
198+
197199
public init(name: TypeIdentifierName) {
198200
self.name = name
199201
}
@@ -290,9 +292,10 @@ public struct Type: Hashable, FileBodyMember, TypeBodyMember {
290292
return _self
291293
}
292294

293-
public func with(objc: Bool) -> Type {
295+
public func with(objc: Bool, prefix: String? = nil) -> Type {
294296
var _self = self
295297
_self.objc = objc
298+
_self.objcPrefix = prefix
296299
return _self
297300
}
298301
}
@@ -347,7 +350,16 @@ extension TypeIdentifier {
347350
extension Type {
348351

349352
public var swiftString: String {
350-
let objc = self.objc ? "@objc " : .empty
353+
let objcString: String
354+
if objc {
355+
if let objcPrefix = objcPrefix {
356+
objcString = "@objc(\(objcPrefix)\(name.swiftString))\n"
357+
} else {
358+
objcString = "@objc "
359+
}
360+
} else {
361+
objcString = .empty
362+
}
351363

352364
let genericParameters = self.genericParameters
353365
.map { $0.swiftString }
@@ -365,7 +377,7 @@ extension Type {
365377
.prefixed(" where ")
366378

367379
return """
368-
\(objc)\(accessLevel.swiftString.suffixed(" "))\(kind.swiftString) \(name.swiftString)\(genericParameters)\(inheritedTypes)\(constraints) {
380+
\(objcString)\(accessLevel.swiftString.suffixed(" "))\(kind.swiftString) \(name.swiftString)\(genericParameters)\(inheritedTypes)\(constraints) {
369381
\(body.map { $0.swiftString }.indented)\
370382
}
371383
"""

0 commit comments

Comments
 (0)