filterNotTo supported
This commit is contained in:
+11
-7
@@ -37,7 +37,7 @@ class AddToCollectionTransformation(
|
||||
override fun mergeWithPrevious(previousTransformation: SequenceTransformation): ResultTransformation? {
|
||||
return when (previousTransformation) {
|
||||
is FilterTransformation -> {
|
||||
FilterToTransformation.create(loop, previousTransformation.inputVariable, targetCollection, previousTransformation.effectiveCondition()) //TODO: use filterNotTo?
|
||||
FilterToTransformation.create(loop, previousTransformation.inputVariable, targetCollection, previousTransformation.condition, previousTransformation.isInverse)
|
||||
}
|
||||
|
||||
is FilterIndexedTransformation -> {
|
||||
@@ -191,15 +191,18 @@ class FilterToTransformation private constructor(
|
||||
loop: KtForExpression,
|
||||
private val inputVariable: KtCallableDeclaration,
|
||||
private val targetCollection: KtExpression,
|
||||
private val filter: KtExpression
|
||||
private val filter: KtExpression,
|
||||
isInverse: Boolean
|
||||
) : ReplaceLoopResultTransformation(loop) {
|
||||
|
||||
private val functionName = if (isInverse) "filterNotTo" else "filterTo"
|
||||
|
||||
override val presentation: String
|
||||
get() = "filterTo(){}"
|
||||
get() = "$functionName(){}"
|
||||
|
||||
override fun generateCode(chainedCallGenerator: ChainedCallGenerator): KtExpression {
|
||||
val lambda = generateLambda(inputVariable, filter)
|
||||
return chainedCallGenerator.generate("filterTo($0) $1:'{}'", targetCollection, lambda)
|
||||
return chainedCallGenerator.generate("$functionName($0) $1:'{}'", targetCollection, lambda)
|
||||
}
|
||||
|
||||
companion object {
|
||||
@@ -207,15 +210,16 @@ class FilterToTransformation private constructor(
|
||||
loop: KtForExpression,
|
||||
inputVariable: KtCallableDeclaration,
|
||||
targetCollection: KtExpression,
|
||||
filter: KtExpression
|
||||
filter: KtExpression,
|
||||
isInverse: Boolean
|
||||
): ResultTransformation {
|
||||
val initialization = targetCollection.detectInitializationBeforeLoop(loop, checkNoOtherUsagesInLoop = true)
|
||||
if (initialization != null && initialization.initializer.hasNoSideEffect()) {
|
||||
val transformation = FilterToTransformation(loop, inputVariable, initialization.initializer, filter)
|
||||
val transformation = FilterToTransformation(loop, inputVariable, initialization.initializer, filter, isInverse)
|
||||
return AssignToVariableResultTransformation.createDelegated(transformation, initialization)
|
||||
}
|
||||
else {
|
||||
return FilterToTransformation(loop, inputVariable, targetCollection, filter)
|
||||
return FilterToTransformation(loop, inputVariable, targetCollection, filter, isInverse)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+4
-3
@@ -40,16 +40,17 @@ class FilterTransformation(
|
||||
return FilterTransformation(loop, inputVariable, mergedCondition, isInverse = false) //TODO: build filterNot in some cases?
|
||||
}
|
||||
|
||||
private val functionName = if (isInverse) "filterNot" else "filter"
|
||||
|
||||
override val affectsIndex: Boolean
|
||||
get() = true
|
||||
|
||||
override val presentation: String
|
||||
get() = if (isInverse) "filterNot{}" else "filter{}"
|
||||
get() = "$functionName{}"
|
||||
|
||||
override fun generateCode(chainedCallGenerator: ChainedCallGenerator): KtExpression {
|
||||
val lambda = generateLambda(inputVariable, condition)
|
||||
val name = if (isInverse) "filterNot" else "filter"
|
||||
return chainedCallGenerator.generate("$0$1:'{}'", name, lambda)
|
||||
return chainedCallGenerator.generate("$0$1:'{}'", functionName, lambda)
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
// WITH_RUNTIME
|
||||
// INTENTION_TEXT: "Replace with 'filterNotTo(){}'"
|
||||
fun foo(list: List<String>, target: MutableList<String>) {
|
||||
<caret>for (s in list) {
|
||||
if (s.length == 0) continue
|
||||
target.add(s)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
// WITH_RUNTIME
|
||||
// INTENTION_TEXT: "Replace with 'filterNotTo(){}'"
|
||||
fun foo(list: List<String>, target: MutableList<String>) {
|
||||
<caret>list.filterNotTo(target) { it.length == 0 }
|
||||
}
|
||||
Reference in New Issue
Block a user