flatMapTo supported
This commit is contained in:
+25
-2
@@ -18,6 +18,7 @@ package org.jetbrains.kotlin.idea.intentions.loopToCallChain.result
|
||||
|
||||
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.psi.*
|
||||
import org.jetbrains.kotlin.psi.psiUtil.getCallNameExpression
|
||||
|
||||
@@ -28,8 +29,17 @@ class AddToCollectionTransformation(
|
||||
) : ReplaceLoopTransformation(loop, inputVariable) {
|
||||
|
||||
override fun mergeWithPrevious(previousTransformation: SequenceTransformation): ResultTransformation? {
|
||||
if (previousTransformation !is FilterTransformation) return null
|
||||
return FilterToTransformation(loop, inputVariable, targetCollection, previousTransformation.effectiveCondition()) //TODO: use filterNotTo?
|
||||
return when (previousTransformation) {
|
||||
is FilterTransformation -> {
|
||||
FilterToTransformation(loop, inputVariable, targetCollection, previousTransformation.effectiveCondition()) //TODO: use filterNotTo?
|
||||
}
|
||||
|
||||
is FlatMapTransformation -> {
|
||||
FlatMapToTransformation(loop, previousTransformation.inputVariable, targetCollection, previousTransformation.transform)
|
||||
}
|
||||
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
|
||||
override fun generateCode(chainedCallGenerator: ChainedCallGenerator): KtExpression {
|
||||
@@ -96,4 +106,17 @@ class MapToTransformation(
|
||||
val lambda = generateLambda(inputVariable, mapping)
|
||||
return chainedCallGenerator.generate("mapTo($0) $1:'{}'", targetCollection, lambda)
|
||||
}
|
||||
}
|
||||
|
||||
class FlatMapToTransformation(
|
||||
loop: KtForExpression,
|
||||
inputVariable: KtCallableDeclaration,
|
||||
private val targetCollection: KtExpression,
|
||||
private val transform: KtExpression
|
||||
) : ReplaceLoopTransformation(loop, inputVariable) {
|
||||
|
||||
override fun generateCode(chainedCallGenerator: ChainedCallGenerator): KtExpression {
|
||||
val lambda = generateLambda(inputVariable, transform)
|
||||
return chainedCallGenerator.generate("flatMapTo($0) $1:'{}'", targetCollection, lambda)
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -27,7 +27,7 @@ import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
|
||||
|
||||
class FlatMapTransformation(
|
||||
override val inputVariable: KtCallableDeclaration,
|
||||
private val transform: KtExpression
|
||||
val transform: KtExpression
|
||||
) : SequenceTransformation {
|
||||
|
||||
override val affectsIndex: Boolean
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
// WITH_RUNTIME
|
||||
fun foo(list: List<String>, target: MutableList<String>) {
|
||||
<caret>for (s in list) {
|
||||
for (line in s.lines()) {
|
||||
target.add(line)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
// WITH_RUNTIME
|
||||
fun foo(list: List<String>, target: MutableList<String>) {
|
||||
<caret>list.flatMapTo(target) { it.lines() }
|
||||
}
|
||||
Reference in New Issue
Block a user