Supported filterIndexedTo

This commit is contained in:
Valentin Kipyatkov
2016-04-21 20:51:33 +03:00
parent 7c473e7159
commit 318f3dfdd8
7 changed files with 78 additions and 8 deletions
@@ -22,10 +22,7 @@ import org.jetbrains.kotlin.descriptors.VariableDescriptor
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptor
import org.jetbrains.kotlin.idea.imports.importableFqName
import org.jetbrains.kotlin.idea.intentions.loopToCallChain.*
import org.jetbrains.kotlin.idea.intentions.loopToCallChain.sequence.FilterTransformation
import org.jetbrains.kotlin.idea.intentions.loopToCallChain.sequence.FlatMapTransformation
import org.jetbrains.kotlin.idea.intentions.loopToCallChain.sequence.MapIndexedTransformation
import org.jetbrains.kotlin.idea.intentions.loopToCallChain.sequence.MapTransformation
import org.jetbrains.kotlin.idea.intentions.loopToCallChain.sequence.*
import org.jetbrains.kotlin.idea.util.IdeDescriptorRenderers
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.psi.*
@@ -43,6 +40,10 @@ class AddToCollectionTransformation(
FilterToTransformation.create(loop, previousTransformation.inputVariable, targetCollection, previousTransformation.effectiveCondition()) //TODO: use filterNotTo?
}
is FilterIndexedTransformation -> {
FilterIndexedToTransformation.create(loop, previousTransformation.inputVariable, previousTransformation.indexVariable, targetCollection, previousTransformation.condition)
}
is MapTransformation -> {
MapToTransformation.create(loop, previousTransformation.inputVariable, targetCollection, previousTransformation.mapping)
}
@@ -216,6 +217,42 @@ class FilterToTransformation private constructor(
}
}
class FilterIndexedToTransformation private constructor(
loop: KtForExpression,
private val inputVariable: KtCallableDeclaration,
private val indexVariable: KtCallableDeclaration,
private val targetCollection: KtExpression,
private val filter: KtExpression
) : ReplaceLoopResultTransformation(loop) {
override val presentation: String
get() = "filterIndexedTo(){}"
override fun generateCode(chainedCallGenerator: ChainedCallGenerator): KtExpression {
val lambda = generateLambda(filter, indexVariable, inputVariable)
return chainedCallGenerator.generate("filterIndexedTo($0) $1:'{}'", targetCollection, lambda)
}
companion object {
fun create(
loop: KtForExpression,
inputVariable: KtCallableDeclaration,
indexVariable: KtCallableDeclaration,
targetCollection: KtExpression,
filter: KtExpression
): ResultTransformation {
val initialization = targetCollection.detectInitializationBeforeLoop(loop, checkNoOtherUsagesInLoop = true)
if (initialization != null && initialization.initializer.hasNoSideEffect()) {
val transformation = FilterIndexedToTransformation(loop, inputVariable, indexVariable, initialization.initializer, filter)
return AssignToVariableResultTransformation.createDelegated(transformation, initialization)
}
else {
return FilterIndexedToTransformation(loop, inputVariable, indexVariable, targetCollection, filter)
}
}
}
}
class MapToTransformation private constructor(
loop: KtForExpression,
private val inputVariable: KtCallableDeclaration,
@@ -0,0 +1,8 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'filterIndexedTo(){}'"
fun foo(list: List<String>, target: MutableList<String>) {
<caret>for ((index, s) in list.withIndex()) {
if (s.length > index)
target.add(s)
}
}
@@ -0,0 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'filterIndexedTo(){}'"
fun foo(list: List<String>, target: MutableList<String>) {
<caret>list.filterIndexedTo(target) { index, s -> s.length > index }
}
@@ -0,0 +1,12 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'filterIndexedTo(){}'"
import java.util.ArrayList
fun foo(list: List<String>): List<String> {
val result = ArrayList<String>(1000)
<caret>for ((index, s) in list.withIndex()) {
if (s.length > index)
result.add(s)
}
return result
}
@@ -0,0 +1,8 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'filterIndexedTo(){}'"
import java.util.ArrayList
fun foo(list: List<String>): List<String> {
val <caret>result = list.filterIndexedTo(ArrayList<String>(1000)) { index, s -> s.length > index }
return result
}
@@ -1,5 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with '+= filterIndexed{}.filterIndexed{}'"
// INTENTION_TEXT: "Replace with 'filterIndexed{}.filterIndexedTo(){}'"
fun foo(list: List<String>, target: MutableCollection<String>) {
var j = 0
<caret>for ((i, s) in list.withIndex()) {
@@ -1,7 +1,7 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with '+= filterIndexed{}.filterIndexed{}'"
// INTENTION_TEXT: "Replace with 'filterIndexed{}.filterIndexedTo(){}'"
fun foo(list: List<String>, target: MutableCollection<String>) {
<caret>target += list
<caret>list
.filterIndexed { i, s -> s.length <= i }
.filterIndexed { j, s -> s.length % j == 0 }
.filterIndexedTo(target) { j, s -> s.length % j == 0 }
}