Skip to content

Commit

Permalink
Properties in protocols
Browse files Browse the repository at this point in the history
  • Loading branch information
angelolloqui committed Sep 16, 2017
1 parent 7e2f593 commit 447ed59
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 49 deletions.
8 changes: 8 additions & 0 deletions Sources/SwiftKotlinFramework/AST+Operations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ extension VariableDeclaration {
var isStatic: Bool {
return modifiers.isStatic
}
var isImplicitlyUnwrapped: Bool {
switch body {
case .initializerList(let patters):
return patters.first?.initializerExpression == nil
default:
return false
}
}
}

extension FunctionDeclaration {
Expand Down
44 changes: 23 additions & 21 deletions Sources/SwiftKotlinFramework/SwiftKotlin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,18 @@ public class KotlinTokenizer: SwiftTokenizer {
with: [declaration.newToken(.keyword, "interface")])
}

open override func tokenize(_ member: ProtocolDeclaration.PropertyMember, node: ASTNode) -> [Token] {
let attrsTokens = tokenize(member.attributes, node: node)
let modifiersTokens = tokenize(member.modifiers, node: node)

return [
attrsTokens,
modifiersTokens,
[member.newToken(.keyword, member.getterSetterKeywordBlock.setter == nil ? "val" : "var", node)],
member.newToken(.identifier, member.name, node) + tokenize(member.typeAnnotation, node: node),
].joined(token: member.newToken(.space, " ", node))
}

open override func tokenize(_ modifier: AccessLevelModifier, node: ASTNode) -> [Token] {
return [modifier.newToken(
.keyword,
Expand Down Expand Up @@ -184,6 +196,13 @@ public class KotlinTokenizer: SwiftTokenizer {
if readOnly {
tokens = tokens.replacing({ $0.value == "var" }, with: [declaration.body.newToken(.keyword, "val", declaration)], amount: 1)
}

if declaration.isImplicitlyUnwrapped {
tokens.insert(contentsOf: [
declaration.newToken(.keyword, "lateinit"),
declaration.newToken(.space, " ")
], at: 0)
}
return tokens
}

Expand All @@ -201,27 +220,6 @@ public class KotlinTokenizer: SwiftTokenizer {
getterTokens +
tokenize(codeBlock)
)
// case let .getterSetterBlock(name, typeAnnotation, block):
// return body.newToken(.identifier, name, node) +
// tokenize(typeAnnotation, node: node) +
// body.newToken(.linebreak, "\n", node) +
// indent(
// getterTokens +
// tokenize(block, node: node)
// )
// case let .getterSetterKeywordBlock(name, typeAnnotation, block):
// return body.newToken(.identifier, name, node) +
// tokenize(typeAnnotation, node: node) +
// body.newToken(.space, " ", node) +
// tokenize(block, node: node)
// case let .willSetDidSetBlock(name, typeAnnotation, initExpr, block):
// 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(.space, " ", node)] +
// tokenize(block, node: node)
default:
return super.tokenize(body, node: node).removingTrailingSpaces()
}
Expand Down Expand Up @@ -461,6 +459,10 @@ public class KotlinTokenizer: SwiftTokenizer {
type.genericArgumentClause.map { tokenize($0, node: node) }
}

open override func tokenize(_ type: ImplicitlyUnwrappedOptionalType, node: ASTNode) -> [Token] {
return tokenize(type.wrappedType, node: node)
}

// MARK: - Patterns


Expand Down
21 changes: 12 additions & 9 deletions Tests/SwiftKotlinFrameworkTests/Tests/extensions.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
var Double.km: Double {
return this * 1000.0
}
var Double.m: Double {
return this
}
val Double.km: Double
get() {
return this * 1000.0
}
val Double.m: Double
get() {
return this
}
open fun Double.toKm() : Double {
return this * 1000.0
}
Expand All @@ -13,6 +15,7 @@ fun Double.toMeter() : Double {
public fun Double.Companion.toKm() : Double {
return this * 1000.0
}
public var Double.Companion.m: Double {
return this
}
public val Double.Companion.m: Double
get() {
return this
}
14 changes: 7 additions & 7 deletions Tests/SwiftKotlinFrameworkTests/Tests/foundation_types.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
var boolean: Boolean
lateinit var boolean: Boolean
var anyObject: Any? = null
var any: Any? = null
var array: List<String>?
var array: Promise<List<String>>?
var array: List<Promise<List<String>>>
lateinit var array: List<String>?
lateinit var array: Promise<List<String>>?
lateinit var array: List<Promise<List<String>>>
var array = listOf("1", "2")
var map: Map<Int, String>?
var map: Promise<Map<Int, String>>?
var map: Map<Int, Promise<Map<String, String>>>
lateinit var map: Map<Int, String>?
lateinit var map: Promise<Map<Int, String>>?
lateinit var map: Map<Int, Promise<Map<String, String>>>
var map = mapOf(1 to "a", 2 to "b")
10 changes: 4 additions & 6 deletions Tests/SwiftKotlinFrameworkTests/Tests/properties.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
interface Hello {
val foo: String
var bar: String
}
class A {
val stateObservable1: Observable<RestaurantsListState>
get() {
Expand Down Expand Up @@ -27,11 +31,5 @@ class A {
origin.y = 0
origin.x = val.x
}
var numberOfEdits = 0
private set
lateinit var subject: TestSubject
}
interface Hello {
val foo: String
var bar: String
}
29 changes: 23 additions & 6 deletions Tests/SwiftKotlinFrameworkTests/Tests/properties.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@

protocol Hello {
var foo: String { get }
var bar: String { get set }
}

class A {
var stateObservable1: Observable<RestaurantsListState> { return state.asObservable() }
var stateObservable2: Observable<RestaurantsListState> {
Expand Down Expand Up @@ -29,12 +35,23 @@ class A {
}
}

private(set) var numberOfEdits = 0

var subject: TestSubject!
}

protocol Hello {
var foo: String { get }
var bar: String { get set }
// private(set) var numberOfEdits = 0
// lazy var importer = DataImporter()

}

//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 447ed59

Please sign in to comment.