diff --git a/Assets/Tests/KotlinTokenizer/properties.kt b/Assets/Tests/KotlinTokenizer/properties.kt index 510c206..39f7eee 100644 --- a/Assets/Tests/KotlinTokenizer/properties.kt +++ b/Assets/Tests/KotlinTokenizer/properties.kt @@ -47,6 +47,12 @@ class A { field = newValue delegate.set(isLocating = isLocating) } + var anotherName: String? = null + private set + var anotherNameWithDidSet: String = "a value" + private set(newValue) { + field = newValue + } } data class Rect( diff --git a/Assets/Tests/KotlinTokenizer/properties.swift b/Assets/Tests/KotlinTokenizer/properties.swift index 8800b3c..f393d1c 100644 --- a/Assets/Tests/KotlinTokenizer/properties.swift +++ b/Assets/Tests/KotlinTokenizer/properties.swift @@ -53,6 +53,13 @@ class A { delegate.set(isLocating: isLocating) } } + + private(set) var anotherName: String? + + private(set) var anotherNameWithDidSet: String = "a value" { + didSet {} + } + } struct Rect { diff --git a/Sources/SwiftKotlinFramework/KotlinTokenizer.swift b/Sources/SwiftKotlinFramework/KotlinTokenizer.swift index 93de5e2..b34a8b4 100644 --- a/Sources/SwiftKotlinFramework/KotlinTokenizer.swift +++ b/Sources/SwiftKotlinFramework/KotlinTokenizer.swift @@ -234,6 +234,8 @@ public class KotlinTokenizer: SwiftTokenizer { switch modifier { case .static, .unowned, .unownedSafe, .unownedUnsafe, .weak, .convenience, .dynamic, .lazy: return [] + case .accessLevel(let mod) where mod.rawValue.contains("(set)"): + return [] default: return super.tokenize(modifier, node: node) } @@ -305,6 +307,19 @@ public class KotlinTokenizer: SwiftTokenizer { } } + if declaration.isPrivateSet || declaration.isProtectedSet { + let modifierToken = declaration.newToken(.keyword, declaration.isPrivateSet ? "private" : "protected") + // If there is already a setter, change its accesibility + if let setterIndex = bodyTokens.firstIndex(where: { $0.kind == .keyword && $0.value == "set" }) { + bodyTokens.insert(contentsOf: [modifierToken, spaceToken], at: setterIndex) + } else { // Else create modified setter + bodyTokens.append(contentsOf: + [declaration.newToken(.linebreak, "\n")] + + indent([modifierToken, spaceToken, declaration.newToken(.keyword, "set")]) + ) + } + } + return [ attrsTokenGroups.joined(token: spaceToken), modifierTokenGroups.joined(token: spaceToken), diff --git a/Sources/SwiftKotlinFramework/utils/AST+Operations.swift b/Sources/SwiftKotlinFramework/utils/AST+Operations.swift index c0fb1fd..0435185 100644 --- a/Sources/SwiftKotlinFramework/utils/AST+Operations.swift +++ b/Sources/SwiftKotlinFramework/utils/AST+Operations.swift @@ -31,7 +31,15 @@ extension VariableDeclaration { var isLazy: Bool { return modifiers.isLazy } - + + var isPrivateSet: Bool { + return modifiers.isPrivateSet + } + + var isProtectedSet: Bool { + return modifiers.isProtectedSet + } + var typeAnnotation: TypeAnnotation? { return initializerList? .compactMap { $0.pattern as? IdentifierPattern } @@ -122,6 +130,14 @@ extension Collection where Iterator.Element == DeclarationModifier { return self.contains(where: { $0.isLazy }) } + var isPrivateSet: Bool { + return self.contains(where: { $0.isPrivateSet }) + } + + var isProtectedSet: Bool { + return self.contains(where: { $0.isProtectedSet }) + } + var isOverride: Bool { return self.contains(where: { $0.isOverride }) } @@ -148,6 +164,22 @@ extension DeclarationModifier { default: return false } } + + var isPrivateSet: Bool { + switch self { + case .accessLevel(let modifier): + return modifier == .fileprivateSet || modifier == .privateSet + default: return false + } + } + + var isProtectedSet: Bool { + switch self { + case .accessLevel(let modifier): + return modifier == .openSet || modifier == .internalSet + default: return false + } + } } extension SuperclassExpression {