Minor refactoring

This commit is contained in:
Valentin Kipyatkov
2016-08-11 21:07:27 +03:00
parent 407a3ddc8a
commit 50cd61ec9f
5 changed files with 10 additions and 15 deletions
@@ -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)
}
@@ -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)
}
@@ -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)
@@ -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)
}
@@ -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)