Skip to content

Commit

Permalink
#20 Optimized body returns to single line
Browse files Browse the repository at this point in the history
  • Loading branch information
Angel Garcia authored and Angel Garcia committed Feb 3, 2018
1 parent b2fa60f commit e0a49e4
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 33 deletions.
15 changes: 15 additions & 0 deletions Sources/SwiftKotlinFramework/KotlinTokenizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,21 @@ public class KotlinTokenizer: SwiftTokenizer {

}

open override func tokenize(_ codeBlock: CodeBlock) -> [Token] {
guard codeBlock.statements.count == 1,
let returnStatement = codeBlock.statements.first as? ReturnStatement,
let parent = codeBlock.lexicalParent as? Declaration else {
return super.tokenize(codeBlock)
}
let sameLine = parent is VariableDeclaration
let separator = sameLine ? codeBlock.newToken(.space, " ") : codeBlock.newToken(.linebreak, "\n")
let tokens = Array(tokenize(returnStatement).dropFirst(2))
return [
[codeBlock.newToken(.symbol, "=")],
sameLine ? tokens : indent(tokens)
].joined(token: separator)
}

// MARK: - Statements

open override func tokenize(_ statement: GuardStatement) -> [Token] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,5 @@ fun collectCustomerProviders(customerProvider: () -> String) {
customerProviders.append(customerProvider)
}

fun foo(code: (() -> String)) : String {
return "foo ${bar(code)}"
}
fun foo(code: (() -> String)) : String =
"foo ${bar(code)}"
Original file line number Diff line number Diff line change
@@ -1,24 +1,15 @@
val Double.km: Double
get() {
return this * 1000.0
}
get() = this * 1000.0
val Double.m: Double
get() {
return this
}
get() = this

open fun Double.toKm() : Double {
return this * 1000.0
}
open fun Double.toKm() : Double =
this * 1000.0

fun Double.toMeter() : Double {
return this
}
fun Double.toMeter() : Double =
this

public fun Double.Companion.toKm() : Double {
return this * 1000.0
}
public fun Double.Companion.toKm() : Double =
this * 1000.0
public val Double.Companion.m: Double
get() {
return this
}
get() = this
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ fun method(param: String) : String {}

fun method(param: (Int) -> Unit) {}

fun findRestaurant(restaurantId: Int) : ServiceTask<Restaurant> {
return NetworkRequestServiceTask<Restaurant>(networkSession = networkSession, endpoint = "restaurants/")
}
fun findRestaurant(restaurantId: Int) : ServiceTask<Restaurant> =
NetworkRequestServiceTask<Restaurant>(networkSession = networkSession, endpoint = "restaurants/")
restaurantService.findRestaurant(restaurantId = restaurant.id, param = param)

fun tokenize(codeBlock: String?) : List<String> {
val statement = codeBlock ?: return listOf()
return someOtherMethod(statement = statement)
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,10 @@ func findRestaurant(restaurantId: Int) -> ServiceTask<Restaurant> {
}

restaurantService.findRestaurant(restaurantId: restaurant.id, param: param)

func tokenize(_ codeBlock: String?) -> [String] {
guard let statement = codeBlock else {
return []
}
return someOtherMethod(statement: statement)
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ interface Hello {

class A {
val stateObservable1: Observable<RestaurantsListState>
get() {
return state.asObservable()
}
get() = state.asObservable()
val stateObservable2: Observable<RestaurantsListState>
get() {
return state.asObservable()
Expand Down
10 changes: 4 additions & 6 deletions Tests/SwiftKotlinFrameworkTests/Tests/KotlinTokenizer/statics.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,11 @@ class A {

fun method() {}

fun create() : A? {
return null
}
fun create() : A? =
null

fun withParams(param: Int) : A? {
return null
}
fun withParams(param: Int) : A? =
null
}
}

Expand Down

0 comments on commit e0a49e4

Please sign in to comment.