diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/matchAndConvert.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/matchAndConvert.kt index c95fede381b..218d81a0609 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/matchAndConvert.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/matchAndConvert.kt @@ -48,7 +48,7 @@ object MatcherRegistrar { SumTransformationBase.Matcher, MaxOrMinTransformation.Matcher, IntroduceIndexMatcher, - FilterTransformation.Matcher, + FilterTransformationBase.Matcher, MapTransformation.Matcher, FlatMapTransformation.Matcher, ForEachTransformation.Matcher diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/sequence/FilterTransformation.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/sequence/FilterTransformation.kt index 07bc406fb87..dab393968ba 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/sequence/FilterTransformation.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/sequence/FilterTransformation.kt @@ -31,36 +31,6 @@ abstract class FilterTransformationBase : SequenceTransformation { override val affectsIndex: Boolean get() = true -} - -class FilterTransformation( - override val loop: KtForExpression, - override val inputVariable: KtCallableDeclaration, - override val indexVariable: KtCallableDeclaration?, - val condition: KtExpression, - val isInverse: Boolean -) : FilterTransformationBase() { - - override val effectiveCondition: KtExpression by lazy { - if (isInverse) condition.negate() else condition - } - - private val functionName = when { - indexVariable != null -> "filterIndexed" - isInverse -> "filterNot" - else -> "filter" - } - - override val presentation: String - get() = "$functionName{}" - - override fun generateCode(chainedCallGenerator: ChainedCallGenerator): KtExpression { - val lambda = if (indexVariable != null) - generateLambda(inputVariable, indexVariable, effectiveCondition) - else - generateLambda(inputVariable, condition) - return chainedCallGenerator.generate("$0$1:'{}'", functionName, lambda) - } /** * Matches: @@ -161,7 +131,7 @@ class FilterTransformation( indexVariable: KtCallableDeclaration?, condition: KtExpression, isInverse: Boolean - ): SequenceTransformation { + ): FilterTransformationBase { val effectiveCondition = if (isInverse) condition.negate() else condition @@ -192,6 +162,36 @@ class FilterTransformation( } } +class FilterTransformation( + override val loop: KtForExpression, + override val inputVariable: KtCallableDeclaration, + override val indexVariable: KtCallableDeclaration?, + val condition: KtExpression, + val isInverse: Boolean +) : FilterTransformationBase() { + + override val effectiveCondition: KtExpression by lazy { + if (isInverse) condition.negate() else condition + } + + private val functionName = when { + indexVariable != null -> "filterIndexed" + isInverse -> "filterNot" + else -> "filter" + } + + override val presentation: String + get() = "$functionName{}" + + override fun generateCode(chainedCallGenerator: ChainedCallGenerator): KtExpression { + val lambda = if (indexVariable != null) + generateLambda(inputVariable, indexVariable, effectiveCondition) + else + generateLambda(inputVariable, condition) + return chainedCallGenerator.generate("$0$1:'{}'", functionName, lambda) + } +} + class FilterIsInstanceTransformation( override val loop: KtForExpression, override val inputVariable: KtCallableDeclaration,