Skip to content

Commit

Permalink
#22 Implemented didSet and willSet property observers
Browse files Browse the repository at this point in the history
  • Loading branch information
Angel Garcia authored and Angel Garcia committed Mar 24, 2018
1 parent b60576f commit 8d6c593
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 17 deletions.
55 changes: 51 additions & 4 deletions Sources/SwiftKotlinFramework/KotlinTokenizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -306,19 +306,47 @@ public class KotlinTokenizer: SwiftTokenizer {
}

open override func tokenize(_ body: VariableDeclaration.Body, node: ASTNode) -> [Token] {
let getterTokens = [
body.newToken(.keyword, "get()", node),
body.newToken(.space, " ", node)
]
switch body {
case let .codeBlock(name, typeAnnotation, codeBlock):
let getterTokens = [
body.newToken(.keyword, "get()", node),
body.newToken(.space, " ", node)
]
return body.newToken(.identifier, name, node) +
tokenize(typeAnnotation, node: node) +
body.newToken(.linebreak, "\n", node) +
indent(
getterTokens +
tokenize(codeBlock)
)

case let .willSetDidSetBlock(name, typeAnnotation, initExpr, block):
let newName = block.willSetClause?.name ?? "newValue"
let oldName = block.didSetClause?.name ?? "oldValue"
let fieldAssignmentExpression = AssignmentOperatorExpression(
leftExpression: IdentifierExpression(kind: IdentifierExpression.Kind.identifier("field", nil)),
rightExpression: IdentifierExpression(kind: IdentifierExpression.Kind.identifier(newName, nil))
)
let oldValueAssignmentExpression = ConstantDeclaration(initializerList: [
PatternInitializer(pattern: IdentifierPattern(identifier: oldName),
initializerExpression: IdentifierExpression(kind: IdentifierExpression.Kind.identifier("field", nil)))
])
let setterCodeBlock = CodeBlock(statements:
(block.didSetClause?.codeBlock.statements.count ?? 0 > 0 ? [oldValueAssignmentExpression] : []) +
(block.willSetClause?.codeBlock.statements ?? []) +
[fieldAssignmentExpression] +
(block.didSetClause?.codeBlock.statements ?? [])
)
let setterTokens = tokenize(GetterSetterBlock.SetterClause(name: newName, codeBlock: setterCodeBlock), node: node)
let typeAnnoTokens = typeAnnotation.map { tokenize($0, node: node) } ?? []
let initTokens = initExpr.map { body.newToken(.symbol, " = ", node) + tokenize($0) } ?? []
return [
body.newToken(.identifier, name, node)] +
typeAnnoTokens +
initTokens +
[body.newToken(.linebreak, "\n", node)] +
indent(setterTokens)

default:
return super.tokenize(body, node: node).removingTrailingSpaces()
}
Expand All @@ -343,6 +371,25 @@ public class KotlinTokenizer: SwiftTokenizer {
return super.tokenize(newSetter, node: node)
}

open override func tokenize(_ block: WillSetDidSetBlock, node: ASTNode) -> [Token] {
let name = block.willSetClause?.name ?? block.didSetClause?.name ?? "newValue"
let willSetBlock = block.willSetClause.map { tokenize($0.codeBlock) }?.tokensOnScope(depth: 1) ?? []
let didSetBlock = block.didSetClause.map { tokenize($0.codeBlock) }?.tokensOnScope(depth: 1) ?? []
let assignmentBlock = [
block.newToken(.identifier, "field", node),
block.newToken(.keyword, " = ", node),
block.newToken(.identifier, name, node)
]
return [
[block.newToken(.startOfScope, "{", node)],
willSetBlock,
indent(assignmentBlock),
didSetBlock,
[block.newToken(.endOfScope, "}", node)]
].joined(token: block.newToken(.linebreak, "\n", node))

}

open override func tokenize(_ declaration: ImportDeclaration) -> [Token] {
return []
}
Expand Down
16 changes: 16 additions & 0 deletions Sources/SwiftKotlinFramework/utils/Token+Operations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,22 @@ extension Array where Iterator.Element == Token {
return tokens
}

public func tokensOnScope(depth: Int) -> [Token] {
var tokens = [Token]()
var scope = 0
for token in self {
if token.kind == .endOfScope {
scope -= 1
}
if scope == depth {
tokens.append(token)
}
if token.kind == .startOfScope {
scope += 1
}
}
return tokens
}
}

extension ASTTokenizable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ class A {
private val name: String by lazy { ->
"abc"
}
var isLocating = false
set(newValue) {
val oldValue = field
field = newValue
delegate.set(isLocating = isLocating)
}
}

data class Rect(
Expand All @@ -58,3 +64,14 @@ data class Rect(
}
}

class StepCounter {
var totalSteps: Int = 0
set(newTotalSteps) {
val oldValue = field
print("About to set totalSteps to ${newTotalSteps}")
field = newTotalSteps
if (totalSteps > oldValue) {
print("Added ${totalSteps - oldValue} steps")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,16 @@ class A {
temporaryPlayers.append("John Doe")
return temporaryPlayers
}()

private lazy var name: String = {() -> String in
return "abc"
}()

var isLocating = false {
didSet {
delegate.set(isLocating: isLocating)
}
}
}

struct Rect {
Expand All @@ -65,16 +72,15 @@ struct Rect {
}
}

//class StepCounter {
// var totalSteps: Int = 0 {
// willSet(newTotalSteps) {
// print("About to set totalSteps to \(newTotalSteps)")
// }
// didSet {
// if totalSteps > oldValue {
// print("Added \(totalSteps - oldValue) steps")
// }
// }
// }
//}

class StepCounter {
var totalSteps: Int = 0 {
willSet(newTotalSteps) {
print("About to set totalSteps to \(newTotalSteps)")
}
didSet {
if totalSteps > oldValue {
print("Added \(totalSteps - oldValue) steps")
}
}
}
}

0 comments on commit 8d6c593

Please sign in to comment.