From 50cd61ec9fa78dc4a9f0b1b83a271237affbed05 Mon Sep 17 00:00:00 2001 From: Valentin Kipyatkov Date: Thu, 11 Aug 2016 21:07:27 +0300 Subject: [PATCH] Minor refactoring --- .../result/AddToCollectionTransformation.kt | 7 ++----- .../loopToCallChain/result/ForEachTransformation.kt | 5 +---- .../loopToCallChain/sequence/FilterTransformation.kt | 2 +- .../loopToCallChain/sequence/MapTransformation.kt | 5 +---- .../kotlin/idea/intentions/loopToCallChain/utils.kt | 6 +++++- 5 files changed, 10 insertions(+), 15 deletions(-) diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/result/AddToCollectionTransformation.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/result/AddToCollectionTransformation.kt index 135de745bb8..f9b219e4992 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/result/AddToCollectionTransformation.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/result/AddToCollectionTransformation.kt @@ -215,7 +215,7 @@ class FilterToTransformation private constructor( override fun generateCode(chainedCallGenerator: ChainedCallGenerator): KtExpression { val lambda = if (indexVariable != null) - generateLambda(effectiveCondition(), indexVariable, inputVariable) + generateLambda(inputVariable, indexVariable, effectiveCondition()) else generateLambda(inputVariable, condition) return chainedCallGenerator.generate("$functionName($0) $1:'{}'", targetCollection, lambda) @@ -289,10 +289,7 @@ class MapToTransformation private constructor( get() = "$functionName(){}" override fun generateCode(chainedCallGenerator: ChainedCallGenerator): KtExpression { - val lambda = if (indexVariable != null) - generateLambda(mapping, indexVariable, inputVariable) - else - generateLambda(inputVariable, mapping) + val lambda = generateLambda(inputVariable, indexVariable, mapping) return chainedCallGenerator.generate("$functionName($0) $1:'{}'", targetCollection, lambda) } diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/result/ForEachTransformation.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/result/ForEachTransformation.kt index 4480d461386..dd5dfcf8e99 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/result/ForEachTransformation.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/result/ForEachTransformation.kt @@ -34,10 +34,7 @@ class ForEachTransformation( get() = functionName + "{}" override fun generateCode(chainedCallGenerator: ChainedCallGenerator): KtExpression { - val lambda = if (indexVariable != null) - generateLambda(statement, indexVariable, inputVariable) - else - generateLambda(inputVariable, statement) + val lambda = generateLambda(inputVariable, indexVariable, statement) return chainedCallGenerator.generate("$functionName $0:'{}'", lambda) } 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 1e3a2f0d8d9..4aedd0abb31 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 @@ -48,7 +48,7 @@ class FilterTransformation( override fun generateCode(chainedCallGenerator: ChainedCallGenerator): KtExpression { val lambda = if (indexVariable != null) - generateLambda(effectiveCondition(), indexVariable, inputVariable) + generateLambda(inputVariable, indexVariable, effectiveCondition()) else generateLambda(inputVariable, condition) return chainedCallGenerator.generate("$0$1:'{}'", functionName, lambda) diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/sequence/MapTransformation.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/sequence/MapTransformation.kt index 1e83a923dac..0cd97b8050a 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/sequence/MapTransformation.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/sequence/MapTransformation.kt @@ -40,10 +40,7 @@ class MapTransformation( get() = "$functionName{}" override fun generateCode(chainedCallGenerator: ChainedCallGenerator): KtExpression { - val lambda = if (indexVariable != null) - generateLambda(mapping, indexVariable, inputVariable) - else - generateLambda(inputVariable, mapping) + val lambda = generateLambda(inputVariable, indexVariable, mapping) return chainedCallGenerator.generate("$functionName$0:'{}'", lambda) } diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/utils.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/utils.kt index dd96bf07913..c1010a75f48 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/utils.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/loopToCallChain/utils.kt @@ -66,7 +66,11 @@ fun generateLambda(inputVariable: KtCallableDeclaration, expression: KtExpressio return psiFactory.createExpressionByPattern("{ $0 }", lambdaExpression.bodyExpression!!) as KtLambdaExpression } -fun generateLambda(expression: KtExpression, indexVariable: KtCallableDeclaration, inputVariable: KtCallableDeclaration): KtLambdaExpression { +fun generateLambda(inputVariable: KtCallableDeclaration, indexVariable: KtCallableDeclaration?, expression: KtExpression): KtLambdaExpression { + if (indexVariable == null) { + return generateLambda(inputVariable, expression) + } + val lambdaExpression = generateLambda(expression, *arrayOf(indexVariable, inputVariable)) // replace "index++" with "index" or "index + 1" (see IntroduceIndexMatcher)