Skip to content

Commit

Permalink
Merge pull request #105 from angelolloqui/feature/private-setters
Browse files Browse the repository at this point in the history
#104 Support for private setters
  • Loading branch information
angelolloqui authored Nov 8, 2019
2 parents 2248ab9 + 7423a66 commit 7158abc
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 1 deletion.
6 changes: 6 additions & 0 deletions Assets/Tests/KotlinTokenizer/properties.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
7 changes: 7 additions & 0 deletions Assets/Tests/KotlinTokenizer/properties.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
15 changes: 15 additions & 0 deletions Sources/SwiftKotlinFramework/KotlinTokenizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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),
Expand Down
34 changes: 33 additions & 1 deletion Sources/SwiftKotlinFramework/utils/AST+Operations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -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 })
}
Expand All @@ -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 {
Expand Down

0 comments on commit 7158abc

Please sign in to comment.