filterNotTo supported
This commit is contained in:
+11
-7
@@ -37,7 +37,7 @@ class AddToCollectionTransformation(
|
|||||||
override fun mergeWithPrevious(previousTransformation: SequenceTransformation): ResultTransformation? {
|
override fun mergeWithPrevious(previousTransformation: SequenceTransformation): ResultTransformation? {
|
||||||
return when (previousTransformation) {
|
return when (previousTransformation) {
|
||||||
is FilterTransformation -> {
|
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 -> {
|
is FilterIndexedTransformation -> {
|
||||||
@@ -191,15 +191,18 @@ class FilterToTransformation private constructor(
|
|||||||
loop: KtForExpression,
|
loop: KtForExpression,
|
||||||
private val inputVariable: KtCallableDeclaration,
|
private val inputVariable: KtCallableDeclaration,
|
||||||
private val targetCollection: KtExpression,
|
private val targetCollection: KtExpression,
|
||||||
private val filter: KtExpression
|
private val filter: KtExpression,
|
||||||
|
isInverse: Boolean
|
||||||
) : ReplaceLoopResultTransformation(loop) {
|
) : ReplaceLoopResultTransformation(loop) {
|
||||||
|
|
||||||
|
private val functionName = if (isInverse) "filterNotTo" else "filterTo"
|
||||||
|
|
||||||
override val presentation: String
|
override val presentation: String
|
||||||
get() = "filterTo(){}"
|
get() = "$functionName(){}"
|
||||||
|
|
||||||
override fun generateCode(chainedCallGenerator: ChainedCallGenerator): KtExpression {
|
override fun generateCode(chainedCallGenerator: ChainedCallGenerator): KtExpression {
|
||||||
val lambda = generateLambda(inputVariable, filter)
|
val lambda = generateLambda(inputVariable, filter)
|
||||||
return chainedCallGenerator.generate("filterTo($0) $1:'{}'", targetCollection, lambda)
|
return chainedCallGenerator.generate("$functionName($0) $1:'{}'", targetCollection, lambda)
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
@@ -207,15 +210,16 @@ class FilterToTransformation private constructor(
|
|||||||
loop: KtForExpression,
|
loop: KtForExpression,
|
||||||
inputVariable: KtCallableDeclaration,
|
inputVariable: KtCallableDeclaration,
|
||||||
targetCollection: KtExpression,
|
targetCollection: KtExpression,
|
||||||
filter: KtExpression
|
filter: KtExpression,
|
||||||
|
isInverse: Boolean
|
||||||
): ResultTransformation {
|
): ResultTransformation {
|
||||||
val initialization = targetCollection.detectInitializationBeforeLoop(loop, checkNoOtherUsagesInLoop = true)
|
val initialization = targetCollection.detectInitializationBeforeLoop(loop, checkNoOtherUsagesInLoop = true)
|
||||||
if (initialization != null && initialization.initializer.hasNoSideEffect()) {
|
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)
|
return AssignToVariableResultTransformation.createDelegated(transformation, initialization)
|
||||||
}
|
}
|
||||||
else {
|
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?
|
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
|
override val affectsIndex: Boolean
|
||||||
get() = true
|
get() = true
|
||||||
|
|
||||||
override val presentation: String
|
override val presentation: String
|
||||||
get() = if (isInverse) "filterNot{}" else "filter{}"
|
get() = "$functionName{}"
|
||||||
|
|
||||||
override fun generateCode(chainedCallGenerator: ChainedCallGenerator): KtExpression {
|
override fun generateCode(chainedCallGenerator: ChainedCallGenerator): KtExpression {
|
||||||
val lambda = generateLambda(inputVariable, condition)
|
val lambda = generateLambda(inputVariable, condition)
|
||||||
val name = if (isInverse) "filterNot" else "filter"
|
return chainedCallGenerator.generate("$0$1:'{}'", functionName, lambda)
|
||||||
return chainedCallGenerator.generate("$0$1:'{}'", name, 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