filterNotNullTo supported
This commit is contained in:
+33
@@ -44,6 +44,10 @@ class AddToCollectionTransformation(
|
|||||||
FilterIndexedToTransformation.create(loop, previousTransformation.inputVariable, previousTransformation.indexVariable, targetCollection, previousTransformation.condition)
|
FilterIndexedToTransformation.create(loop, previousTransformation.inputVariable, previousTransformation.indexVariable, targetCollection, previousTransformation.condition)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
is FilterNotNullTransformation -> {
|
||||||
|
FilterNotNullToTransformation.create(loop, targetCollection)
|
||||||
|
}
|
||||||
|
|
||||||
is MapTransformation -> {
|
is MapTransformation -> {
|
||||||
MapToTransformation.create(loop, previousTransformation.inputVariable, targetCollection, previousTransformation.mapping)
|
MapToTransformation.create(loop, previousTransformation.inputVariable, targetCollection, previousTransformation.mapping)
|
||||||
}
|
}
|
||||||
@@ -253,6 +257,35 @@ class FilterIndexedToTransformation private constructor(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class FilterNotNullToTransformation private constructor(
|
||||||
|
loop: KtForExpression,
|
||||||
|
private val targetCollection: KtExpression
|
||||||
|
) : ReplaceLoopResultTransformation(loop) {
|
||||||
|
|
||||||
|
override val presentation: String
|
||||||
|
get() = "filterNotNullTo()"
|
||||||
|
|
||||||
|
override fun generateCode(chainedCallGenerator: ChainedCallGenerator): KtExpression {
|
||||||
|
return chainedCallGenerator.generate("filterNotNullTo($0)", targetCollection)
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
fun create(
|
||||||
|
loop: KtForExpression,
|
||||||
|
targetCollection: KtExpression
|
||||||
|
): ResultTransformation {
|
||||||
|
val initialization = targetCollection.detectInitializationBeforeLoop(loop, checkNoOtherUsagesInLoop = true)
|
||||||
|
if (initialization != null && initialization.initializer.hasNoSideEffect()) {
|
||||||
|
val transformation = FilterNotNullToTransformation(loop, initialization.initializer)
|
||||||
|
return AssignToVariableResultTransformation.createDelegated(transformation, initialization)
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return FilterNotNullToTransformation(loop, targetCollection)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
class MapToTransformation private constructor(
|
class MapToTransformation private constructor(
|
||||||
loop: KtForExpression,
|
loop: KtForExpression,
|
||||||
private val inputVariable: KtCallableDeclaration,
|
private val inputVariable: KtCallableDeclaration,
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
// INTENTION_TEXT: "Replace with 'filterNotNullTo()'"
|
||||||
|
fun foo(list: List<String?>, target: MutableCollection<String>) {
|
||||||
|
<caret>for (s in list) {
|
||||||
|
if (s != null) {
|
||||||
|
target.add(s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
// INTENTION_TEXT: "Replace with 'filterNotNullTo()'"
|
||||||
|
fun foo(list: List<String?>, target: MutableCollection<String>) {
|
||||||
|
<caret>list.filterNotNullTo(target)
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
// INTENTION_TEXT: "Replace with 'filterNotNullTo()'"
|
||||||
|
import java.util.ArrayList
|
||||||
|
|
||||||
|
fun foo(list: List<String?>) {
|
||||||
|
val target = ArrayList<String>(1000)
|
||||||
|
<caret>for (s in list) {
|
||||||
|
if (s != null) {
|
||||||
|
target.add(s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
// INTENTION_TEXT: "Replace with 'filterNotNullTo()'"
|
||||||
|
import java.util.ArrayList
|
||||||
|
|
||||||
|
fun foo(list: List<String?>) {
|
||||||
|
val <caret>target = list.filterNotNullTo(ArrayList<String>(1000))
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user