Supported forEachIndexed

This commit is contained in:
Valentin Kipyatkov
2016-05-11 22:52:46 +03:00
parent f28dca1fd5
commit 4094d77782
7 changed files with 54 additions and 6 deletions
@@ -24,15 +24,21 @@ import org.jetbrains.kotlin.psi.KtForExpression
class ForEachTransformation(
loop: KtForExpression,
private val inputVariable: KtCallableDeclaration,
private val indexVariable: KtCallableDeclaration?,
private val statement: KtExpression
) : ReplaceLoopResultTransformation(loop) {
private val functionName = if (indexVariable != null) "forEachIndexed" else "forEach"
override val presentation: String
get() = "forEach{}"
get() = functionName + "{}"
override fun generateCode(chainedCallGenerator: ChainedCallGenerator): KtExpression {
val lambda = generateLambda(inputVariable, statement)
return chainedCallGenerator.generate("forEach $0:'{}'", lambda)
val lambda = if (indexVariable != null)
generateLambda(statement, indexVariable, inputVariable)
else
generateLambda(inputVariable, statement)
return chainedCallGenerator.generate("$functionName $0:'{}'", lambda)
}
/**
@@ -44,13 +50,13 @@ class ForEachTransformation(
*/
object Matcher : TransformationMatcher {
override val indexVariableAllowed: Boolean
get() = false //TODO: support forEachIndexed
get() = true
override fun match(state: MatchingState): TransformationMatch.Result? {
if (state.previousTransformations.isEmpty()) return null // do not suggest conversion to just ".forEach{}"
if (state.previousTransformations.isEmpty() && state.indexVariable == null) return null // do not suggest conversion to just ".forEach{}"
val statement = state.statements.singleOrNull() ?: return null
val transformation = ForEachTransformation(state.outerLoop, state.inputVariable, statement)
val transformation = ForEachTransformation(state.outerLoop, state.inputVariable, state.indexVariable, statement)
return TransformationMatch.Result(transformation)
}
}
@@ -0,0 +1,8 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'map{}.forEachIndexed{}'"
fun foo(list: List<String>) {
<caret>for ((index, s) in list.withIndex()) {
val s1 = s.substring(1)
println(s1.hashCode() * index)
}
}
@@ -0,0 +1,7 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'map{}.forEachIndexed{}'"
fun foo(list: List<String>) {
list
.map { it.substring(1) }
.forEachIndexed { index, s1 -> println(s1.hashCode() * index) }
}
@@ -0,0 +1,7 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'forEachIndexed{}'"
fun foo(list: List<String>) {
<caret>for ((index, s) in list.withIndex()) {
println(s.hashCode() * index)
}
}
@@ -0,0 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'forEachIndexed{}'"
fun foo(list: List<String>) {
list.forEachIndexed { index, s -> println(s.hashCode() * index) }
}
@@ -0,0 +1,8 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'mapIndexed{}.forEach{}'"
fun foo(list: List<String>) {
<caret>for ((index, s) in list.withIndex()) {
val x = s.length * index
println(x)
}
}
@@ -0,0 +1,7 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'mapIndexed{}.forEach{}'"
fun foo(list: List<String>) {
list
.mapIndexed { index, s -> s.length * index }
.forEach { println(it) }
}