sum and sumBy supported
This commit is contained in:
@@ -51,6 +51,16 @@ fun KtExpression?.isTrueConstant()
|
|||||||
fun KtExpression?.isFalseConstant()
|
fun KtExpression?.isFalseConstant()
|
||||||
= this != null && node?.elementType == KtNodeTypes.BOOLEAN_CONSTANT && text == "false"
|
= this != null && node?.elementType == KtNodeTypes.BOOLEAN_CONSTANT && text == "false"
|
||||||
|
|
||||||
|
private val ZERO_VALUES = setOf(0, 0L, 0f, 0.0)
|
||||||
|
|
||||||
|
fun KtExpression.isZeroConstant(): Boolean {
|
||||||
|
if (this !is KtConstantExpression) return false
|
||||||
|
val bindingContext = analyze(BodyResolveMode.PARTIAL)
|
||||||
|
val type = bindingContext.getType(this) ?: return false
|
||||||
|
val constant = ConstantExpressionEvaluator.getConstant(this, bindingContext) ?: return false
|
||||||
|
return constant.getValue(type) in ZERO_VALUES
|
||||||
|
}
|
||||||
|
|
||||||
fun KtExpression?.isVariableReference(variable: KtCallableDeclaration): Boolean {
|
fun KtExpression?.isVariableReference(variable: KtCallableDeclaration): Boolean {
|
||||||
return this is KtNameReferenceExpression && this.mainReference.isReferenceTo(variable)
|
return this is KtNameReferenceExpression && this.mainReference.isReferenceTo(variable)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -45,6 +45,7 @@ object MatcherRegistrar {
|
|||||||
FindTransformationMatcher,
|
FindTransformationMatcher,
|
||||||
AddToCollectionTransformation.Matcher,
|
AddToCollectionTransformation.Matcher,
|
||||||
CountTransformation.Matcher,
|
CountTransformation.Matcher,
|
||||||
|
SumTransformationBase.Matcher,
|
||||||
MaxOrMinTransformation.Matcher,
|
MaxOrMinTransformation.Matcher,
|
||||||
IntroduceIndexMatcher,
|
IntroduceIndexMatcher,
|
||||||
FilterTransformation.Matcher,
|
FilterTransformation.Matcher,
|
||||||
|
|||||||
+1
-1
@@ -49,7 +49,7 @@ class CountTransformation(
|
|||||||
chainedCallGenerator.generate("count()")
|
chainedCallGenerator.generate("count()")
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((initialization.initializer as? KtConstantExpression)?.text == "0") {
|
if (initialization.initializer.isZeroConstant()) {
|
||||||
return call
|
return call
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|||||||
+3
-4
@@ -100,9 +100,8 @@ class MaxOrMinTransformation(
|
|||||||
private fun matchMathMaxOrMin(state: MatchingState): TransformationMatch.Result? {
|
private fun matchMathMaxOrMin(state: MatchingState): TransformationMatch.Result? {
|
||||||
val assignment = state.statements.singleOrNull() as? KtBinaryExpression ?: return null
|
val assignment = state.statements.singleOrNull() as? KtBinaryExpression ?: return null
|
||||||
if (assignment.operationToken != KtTokens.EQ) return null
|
if (assignment.operationToken != KtTokens.EQ) return null
|
||||||
val assignmentTarget = assignment.left ?: return null
|
|
||||||
|
|
||||||
val variableInitialization = assignmentTarget.isVariableInitializedBeforeLoop(state.outerLoop, checkNoOtherUsagesInLoop = false)
|
val variableInitialization = assignment.left.isVariableInitializedBeforeLoop(state.outerLoop, checkNoOtherUsagesInLoop = false)
|
||||||
?: return null
|
?: return null
|
||||||
|
|
||||||
return matchMathMaxOrMin(variableInitialization, assignment, state, isMax = true)
|
return matchMathMaxOrMin(variableInitialization, assignment, state, isMax = true)
|
||||||
@@ -160,8 +159,8 @@ class MaxOrMinTransformation(
|
|||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
val variableInitialization = (otherHand.isVariableInitializedBeforeLoop(loop, checkNoOtherUsagesInLoop = false)
|
val variableInitialization = otherHand.isVariableInitializedBeforeLoop(loop, checkNoOtherUsagesInLoop = false)
|
||||||
?: return null)
|
?: return null
|
||||||
|
|
||||||
if (!assignmentTarget.isVariableReference(variableInitialization.variable)) return null
|
if (!assignmentTarget.isVariableReference(variableInitialization.variable)) return null
|
||||||
|
|
||||||
|
|||||||
+170
@@ -0,0 +1,170 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2016 JetBrains s.r.o.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.jetbrains.kotlin.idea.intentions.loopToCallChain.result
|
||||||
|
|
||||||
|
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
|
||||||
|
import org.jetbrains.kotlin.descriptors.VariableDescriptor
|
||||||
|
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||||
|
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny
|
||||||
|
import org.jetbrains.kotlin.idea.intentions.loopToCallChain.*
|
||||||
|
import org.jetbrains.kotlin.idea.intentions.loopToCallChain.sequence.MapTransformation
|
||||||
|
import org.jetbrains.kotlin.lexer.KtTokens
|
||||||
|
import org.jetbrains.kotlin.psi.*
|
||||||
|
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
|
||||||
|
import org.jetbrains.kotlin.types.KotlinType
|
||||||
|
|
||||||
|
abstract class SumTransformationBase(
|
||||||
|
loop: KtForExpression,
|
||||||
|
initialization: VariableInitialization
|
||||||
|
) : AssignToVariableResultTransformation(loop, initialization) {
|
||||||
|
|
||||||
|
override fun generateCode(chainedCallGenerator: ChainedCallGenerator): KtExpression {
|
||||||
|
val call = generateCall(chainedCallGenerator)
|
||||||
|
|
||||||
|
if (initialization.initializer.isZeroConstant()) {
|
||||||
|
return call
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return KtPsiFactory(call).createExpressionByPattern("$0 + $1", initialization.initializer, call)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract fun generateCall(chainedCallGenerator: ChainedCallGenerator): KtExpression
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Matches:
|
||||||
|
* val variable = <initial>
|
||||||
|
* for (...) {
|
||||||
|
* ...
|
||||||
|
* variable += <expression>
|
||||||
|
* }
|
||||||
|
*/
|
||||||
|
object Matcher : TransformationMatcher {
|
||||||
|
override val indexVariableAllowed: Boolean
|
||||||
|
get() = true
|
||||||
|
|
||||||
|
override fun match(state: MatchingState): TransformationMatch.Result? {
|
||||||
|
val statement = state.statements.singleOrNull() as? KtBinaryExpression ?: return null
|
||||||
|
if (statement.operationToken != KtTokens.PLUSEQ) return null
|
||||||
|
|
||||||
|
val variableInitialization = statement.left.isVariableInitializedBeforeLoop(state.outerLoop, checkNoOtherUsagesInLoop = true)
|
||||||
|
?: return null
|
||||||
|
|
||||||
|
val value = statement.right ?: return null
|
||||||
|
|
||||||
|
val valueType = value.analyze(BodyResolveMode.PARTIAL).getType(value)?.toSupportedType() ?: return null
|
||||||
|
val sumType = (variableInitialization.variable.resolveToDescriptorIfAny() as? VariableDescriptor)?.type?.toSupportedType() ?: return null
|
||||||
|
|
||||||
|
val conversionFunctionName = when (sumType) {
|
||||||
|
SupportedType.INT -> {
|
||||||
|
val needConversion = when (valueType) {
|
||||||
|
SupportedType.INT -> false
|
||||||
|
|
||||||
|
SupportedType.BYTE, SupportedType.SHORT -> {
|
||||||
|
// we don't need conversion to Int to use "sum" function but need it for "sumBy"
|
||||||
|
!value.isVariableReference(state.inputVariable) && state.indexVariable == null
|
||||||
|
}
|
||||||
|
|
||||||
|
else -> true
|
||||||
|
}
|
||||||
|
if (needConversion) "toInt" else null
|
||||||
|
}
|
||||||
|
|
||||||
|
SupportedType.LONG -> if (valueType != SupportedType.LONG) "toLong" else null
|
||||||
|
|
||||||
|
SupportedType.FLOAT -> if (valueType != SupportedType.FLOAT) "toFloat" else null
|
||||||
|
|
||||||
|
SupportedType.DOUBLE -> if (valueType != SupportedType.DOUBLE) "toDouble" else null
|
||||||
|
|
||||||
|
SupportedType.BYTE, SupportedType.SHORT -> return null // cannot use sum or sumBy to get Byte or Short result
|
||||||
|
}
|
||||||
|
|
||||||
|
val byExpression = if (conversionFunctionName != null)
|
||||||
|
KtPsiFactory(value).createExpressionByPattern("$0.$conversionFunctionName()", value)
|
||||||
|
else
|
||||||
|
value
|
||||||
|
|
||||||
|
if (byExpression.isVariableReference(state.inputVariable)) {
|
||||||
|
val transformation = SumTransformation(state.outerLoop, variableInitialization)
|
||||||
|
return TransformationMatch.Result(transformation)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (state.indexVariable != null) {
|
||||||
|
val mapTransformation = MapTransformation(state.outerLoop, state.inputVariable, state.indexVariable, byExpression, mapNotNull = false)
|
||||||
|
val sumTransformation = SumTransformation(state.outerLoop, variableInitialization)
|
||||||
|
return TransformationMatch.Result(sumTransformation, mapTransformation)
|
||||||
|
}
|
||||||
|
|
||||||
|
val sumByFunctionName = when (sumType) {
|
||||||
|
SupportedType.INT -> "sumBy"
|
||||||
|
|
||||||
|
SupportedType.DOUBLE -> "sumByDouble"
|
||||||
|
|
||||||
|
else -> {
|
||||||
|
val mapTransformation = MapTransformation(state.outerLoop, state.inputVariable, null, byExpression, mapNotNull = false)
|
||||||
|
val sumTransformation = SumTransformation(state.outerLoop, variableInitialization)
|
||||||
|
return TransformationMatch.Result(sumTransformation, mapTransformation)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
val transformation = SumByTransformation(state.outerLoop, variableInitialization, state.inputVariable, byExpression, sumByFunctionName)
|
||||||
|
return TransformationMatch.Result(transformation)
|
||||||
|
}
|
||||||
|
|
||||||
|
private enum class SupportedType {
|
||||||
|
INT, LONG, SHORT, BYTE, DOUBLE, FLOAT
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun KotlinType.toSupportedType(): SupportedType? {
|
||||||
|
return when {
|
||||||
|
KotlinBuiltIns.isInt(this) -> SupportedType.INT
|
||||||
|
KotlinBuiltIns.isLong(this) -> SupportedType.LONG
|
||||||
|
KotlinBuiltIns.isShort(this) -> SupportedType.SHORT
|
||||||
|
KotlinBuiltIns.isByte(this) -> SupportedType.BYTE
|
||||||
|
KotlinBuiltIns.isDouble(this) -> SupportedType.DOUBLE
|
||||||
|
KotlinBuiltIns.isFloat(this) -> SupportedType.FLOAT
|
||||||
|
else -> null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class SumTransformation(loop: KtForExpression, initialization: VariableInitialization) : SumTransformationBase(loop, initialization) {
|
||||||
|
override val presentation: String
|
||||||
|
get() = "sum()"
|
||||||
|
|
||||||
|
override fun generateCall(chainedCallGenerator: ChainedCallGenerator): KtExpression {
|
||||||
|
return chainedCallGenerator.generate("sum()")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class SumByTransformation(
|
||||||
|
loop: KtForExpression,
|
||||||
|
initialization: VariableInitialization,
|
||||||
|
private val inputVariable: KtCallableDeclaration,
|
||||||
|
private val byExpression: KtExpression,
|
||||||
|
private val functionName: String
|
||||||
|
) : SumTransformationBase(loop, initialization) {
|
||||||
|
|
||||||
|
override val presentation: String
|
||||||
|
get() = "$functionName{}"
|
||||||
|
|
||||||
|
override fun generateCall(chainedCallGenerator: ChainedCallGenerator): KtExpression {
|
||||||
|
val lambda = generateLambda(inputVariable, byExpression)
|
||||||
|
return chainedCallGenerator.generate("$functionName $0:'{}'", lambda)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -141,7 +141,7 @@ data class VariableInitialization(
|
|||||||
val initializer: KtExpression)
|
val initializer: KtExpression)
|
||||||
|
|
||||||
//TODO: we need more correctness checks (if variable is non-local or is local but can be changed by some local functions)
|
//TODO: we need more correctness checks (if variable is non-local or is local but can be changed by some local functions)
|
||||||
fun KtExpression.isVariableInitializedBeforeLoop(
|
fun KtExpression?.isVariableInitializedBeforeLoop(
|
||||||
loop: KtForExpression,
|
loop: KtForExpression,
|
||||||
checkNoOtherUsagesInLoop: Boolean
|
checkNoOtherUsagesInLoop: Boolean
|
||||||
): VariableInitialization? {
|
): VariableInitialization? {
|
||||||
|
|||||||
@@ -0,0 +1,10 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
// INTENTION_TEXT: "Replace with 'sum()'"
|
||||||
|
// IS_APPLICABLE_2: false
|
||||||
|
fun foo(list: List<Byte>): Int {
|
||||||
|
var s = 0
|
||||||
|
<caret>for (item in list) {
|
||||||
|
s += item
|
||||||
|
}
|
||||||
|
return s
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
// INTENTION_TEXT: "Replace with 'sum()'"
|
||||||
|
// IS_APPLICABLE_2: false
|
||||||
|
fun foo(list: List<Byte>): Int {
|
||||||
|
val <caret>s = list.sum()
|
||||||
|
return s
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
// INTENTION_TEXT: "Replace with 'sum()'"
|
||||||
|
// IS_APPLICABLE_2: false
|
||||||
|
fun foo(list: List<Double>): Double {
|
||||||
|
var s = 0.0
|
||||||
|
<caret>for (item in list) {
|
||||||
|
s += item
|
||||||
|
}
|
||||||
|
return s
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
// INTENTION_TEXT: "Replace with 'sum()'"
|
||||||
|
// IS_APPLICABLE_2: false
|
||||||
|
fun foo(list: List<Double>): Double {
|
||||||
|
val <caret>s = list.sum()
|
||||||
|
return s
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
// INTENTION_TEXT: "Replace with 'sum()'"
|
||||||
|
// IS_APPLICABLE_2: false
|
||||||
|
fun foo(list: List<Float>): Float {
|
||||||
|
var s = 0.0f
|
||||||
|
<caret>for (item in list) {
|
||||||
|
s += item
|
||||||
|
}
|
||||||
|
return s
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
// INTENTION_TEXT: "Replace with 'sum()'"
|
||||||
|
// IS_APPLICABLE_2: false
|
||||||
|
fun foo(list: List<Float>): Float {
|
||||||
|
val <caret>s = list.sum()
|
||||||
|
return s
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
// INTENTION_TEXT: "Replace with 'sumByDouble{}'"
|
||||||
|
// IS_APPLICABLE_2: false
|
||||||
|
fun foo(list: List<Float>): Double {
|
||||||
|
var s = 0.0
|
||||||
|
<caret>for (item in list) {
|
||||||
|
s += item
|
||||||
|
}
|
||||||
|
return s
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
// INTENTION_TEXT: "Replace with 'sumByDouble{}'"
|
||||||
|
// IS_APPLICABLE_2: false
|
||||||
|
fun foo(list: List<Float>): Double {
|
||||||
|
val <caret>s = list.sumByDouble { it.toDouble() }
|
||||||
|
return s
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
// INTENTION_TEXT: "Replace with 'mapIndexed{}.sum()'"
|
||||||
|
// INTENTION_TEXT_2: "Replace with 'asSequence().mapIndexed{}.sum()'"
|
||||||
|
fun foo(list: List<Int>): Int {
|
||||||
|
var s = 0
|
||||||
|
<caret>for ((index, item) in list.withIndex()) {
|
||||||
|
s += item * index
|
||||||
|
}
|
||||||
|
return s
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
// INTENTION_TEXT: "Replace with 'mapIndexed{}.sum()'"
|
||||||
|
// INTENTION_TEXT_2: "Replace with 'asSequence().mapIndexed{}.sum()'"
|
||||||
|
fun foo(list: List<Int>): Int {
|
||||||
|
val <caret>s = list
|
||||||
|
.mapIndexed { index, item -> item * index }
|
||||||
|
.sum()
|
||||||
|
return s
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
// INTENTION_TEXT: "Replace with 'mapIndexed{}.sum()'"
|
||||||
|
// INTENTION_TEXT_2: "Replace with 'asSequence().mapIndexed{}.sum()'"
|
||||||
|
fun foo(list: List<Int>): Int {
|
||||||
|
val <caret>s = list
|
||||||
|
.asSequence()
|
||||||
|
.mapIndexed { index, item -> item * index }
|
||||||
|
.sum()
|
||||||
|
return s
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
// INTENTION_TEXT: "Replace with 'mapIndexed{}.sum()'"
|
||||||
|
// INTENTION_TEXT_2: "Replace with 'asSequence().mapIndexed{}.sum()'"
|
||||||
|
fun foo(list: List<Any>): Int {
|
||||||
|
var s = 0
|
||||||
|
<caret>for ((index, item) in list.withIndex()) {
|
||||||
|
s += getShort(index, item)
|
||||||
|
}
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getShort(index: Int, item: Any): Short = TODO()
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
// INTENTION_TEXT: "Replace with 'mapIndexed{}.sum()'"
|
||||||
|
// INTENTION_TEXT_2: "Replace with 'asSequence().mapIndexed{}.sum()'"
|
||||||
|
fun foo(list: List<Any>): Int {
|
||||||
|
val s = list
|
||||||
|
.mapIndexed { index, item -> getShort(index, item) }
|
||||||
|
.sum()
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getShort(index: Int, item: Any): Short = TODO()
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
// INTENTION_TEXT: "Replace with 'mapIndexed{}.sum()'"
|
||||||
|
// INTENTION_TEXT_2: "Replace with 'asSequence().mapIndexed{}.sum()'"
|
||||||
|
fun foo(list: List<Any>): Int {
|
||||||
|
val s = list
|
||||||
|
.asSequence()
|
||||||
|
.mapIndexed { index, item -> getShort(index, item) }
|
||||||
|
.sum()
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getShort(index: Int, item: Any): Short = TODO()
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
// INTENTION_TEXT: "Replace with 'sum()'"
|
||||||
|
// IS_APPLICABLE_2: false
|
||||||
|
fun foo(list: List<Int>): Int {
|
||||||
|
var s = 0
|
||||||
|
<caret>for (item in list) {
|
||||||
|
s += item
|
||||||
|
}
|
||||||
|
return s
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
// INTENTION_TEXT: "Replace with 'sum()'"
|
||||||
|
// IS_APPLICABLE_2: false
|
||||||
|
fun foo(list: List<Int>): Int {
|
||||||
|
val <caret>s = list.sum()
|
||||||
|
return s
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
// INTENTION_TEXT: "Replace with 'sum()'"
|
||||||
|
// IS_APPLICABLE_2: false
|
||||||
|
fun foo(list: List<Long>): Long {
|
||||||
|
var s = 0L
|
||||||
|
<caret>for (item in list) {
|
||||||
|
s += item
|
||||||
|
}
|
||||||
|
return s
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
// INTENTION_TEXT: "Replace with 'sum()'"
|
||||||
|
// IS_APPLICABLE_2: false
|
||||||
|
fun foo(list: List<Long>): Long {
|
||||||
|
val <caret>s = list.sum()
|
||||||
|
return s
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
// IS_APPLICABLE: false
|
||||||
|
// IS_APPLICABLE_2: false
|
||||||
|
class C
|
||||||
|
|
||||||
|
operator fun C.plus(other: C): C = TODO()
|
||||||
|
|
||||||
|
fun foo(list: List<C>): C {
|
||||||
|
var s = C()
|
||||||
|
<caret>for (item in list) {
|
||||||
|
s += item
|
||||||
|
}
|
||||||
|
return s
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
// INTENTION_TEXT: "Replace with 'sum()'"
|
||||||
|
// IS_APPLICABLE_2: false
|
||||||
|
fun foo(list: List<Int>): Int {
|
||||||
|
var s = 1
|
||||||
|
<caret>for (item in list) {
|
||||||
|
s += item
|
||||||
|
}
|
||||||
|
return s
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
// INTENTION_TEXT: "Replace with 'sum()'"
|
||||||
|
// IS_APPLICABLE_2: false
|
||||||
|
fun foo(list: List<Int>): Int {
|
||||||
|
val <caret>s = 1 + list.sum()
|
||||||
|
return s
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
// INTENTION_TEXT: "Replace with 'sum()'"
|
||||||
|
// IS_APPLICABLE_2: false
|
||||||
|
fun foo(list: List<Short>): Int {
|
||||||
|
var s = 0
|
||||||
|
<caret>for (item in list) {
|
||||||
|
s += item
|
||||||
|
}
|
||||||
|
return s
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
// INTENTION_TEXT: "Replace with 'sum()'"
|
||||||
|
// IS_APPLICABLE_2: false
|
||||||
|
fun foo(list: List<Short>): Int {
|
||||||
|
val <caret>s = list.sum()
|
||||||
|
return s
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
// INTENTION_TEXT: "Replace with 'sumByDouble{}'"
|
||||||
|
// IS_APPLICABLE_2: false
|
||||||
|
fun foo(list: List<String>): Double {
|
||||||
|
var s = 0.0
|
||||||
|
<caret>for (item in list) {
|
||||||
|
s += item.length
|
||||||
|
}
|
||||||
|
return s
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
// INTENTION_TEXT: "Replace with 'sumByDouble{}'"
|
||||||
|
// IS_APPLICABLE_2: false
|
||||||
|
fun foo(list: List<String>): Double {
|
||||||
|
val <caret>s = list.sumByDouble { it.length.toDouble() }
|
||||||
|
return s
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
// INTENTION_TEXT: "Replace with 'sumBy{}'"
|
||||||
|
// IS_APPLICABLE_2: false
|
||||||
|
fun foo(list: List<String>): Int {
|
||||||
|
var l = 0
|
||||||
|
<caret>for (item in list) {
|
||||||
|
l += item.length
|
||||||
|
}
|
||||||
|
return l
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
// INTENTION_TEXT: "Replace with 'sumBy{}'"
|
||||||
|
// IS_APPLICABLE_2: false
|
||||||
|
fun foo(list: List<String>): Int {
|
||||||
|
val <caret>l = list.sumBy { it.length }
|
||||||
|
return l
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
// INTENTION_TEXT: "Replace with 'map{}.sum()'"
|
||||||
|
// INTENTION_TEXT_2: "Replace with 'asSequence().map{}.sum()'"
|
||||||
|
fun foo(list: List<Int>): Long {
|
||||||
|
var s = 0L
|
||||||
|
<caret>for (item in list) {
|
||||||
|
s += item
|
||||||
|
}
|
||||||
|
return s
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
// INTENTION_TEXT: "Replace with 'map{}.sum()'"
|
||||||
|
// INTENTION_TEXT_2: "Replace with 'asSequence().map{}.sum()'"
|
||||||
|
fun foo(list: List<Int>): Long {
|
||||||
|
val <caret>s = list
|
||||||
|
.map { it.toLong() }
|
||||||
|
.sum()
|
||||||
|
return s
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
// INTENTION_TEXT: "Replace with 'map{}.sum()'"
|
||||||
|
// INTENTION_TEXT_2: "Replace with 'asSequence().map{}.sum()'"
|
||||||
|
fun foo(list: List<Int>): Long {
|
||||||
|
val <caret>s = list
|
||||||
|
.asSequence()
|
||||||
|
.map { it.toLong() }
|
||||||
|
.sum()
|
||||||
|
return s
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
// INTENTION_TEXT: "Replace with 'sumBy{}'"
|
||||||
|
// IS_APPLICABLE_2: false
|
||||||
|
fun foo(list: List<String>): Int {
|
||||||
|
var l = 0
|
||||||
|
<caret>for (item in list) {
|
||||||
|
l += item.getShort()
|
||||||
|
}
|
||||||
|
return l
|
||||||
|
}
|
||||||
|
|
||||||
|
fun String.getShort(): Short = TODO()
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
// INTENTION_TEXT: "Replace with 'sumBy{}'"
|
||||||
|
// IS_APPLICABLE_2: false
|
||||||
|
fun foo(list: List<String>): Int {
|
||||||
|
val <caret>l = list.sumBy { it.getShort().toInt() }
|
||||||
|
return l
|
||||||
|
}
|
||||||
|
|
||||||
|
fun String.getShort(): Short = TODO()
|
||||||
@@ -1180,4 +1180,103 @@ public class IntentionTest2Generated extends AbstractIntentionTest2 {
|
|||||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/twoInitializationsBeforeLoop.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/twoInitializationsBeforeLoop.kt");
|
||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("idea/testData/intentions/loopToCallChain/sum")
|
||||||
|
@TestDataPath("$PROJECT_ROOT")
|
||||||
|
@RunWith(JUnit3RunnerWithInners.class)
|
||||||
|
public static class Sum extends AbstractIntentionTest2 {
|
||||||
|
public void testAllFilesPresentInSum() throws Exception {
|
||||||
|
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/loopToCallChain/sum"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("bytes.kt")
|
||||||
|
public void testBytes() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/sum/bytes.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("doubles.kt")
|
||||||
|
public void testDoubles() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/sum/doubles.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("floats.kt")
|
||||||
|
public void testFloats() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/sum/floats.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("floatsIntoDouble.kt")
|
||||||
|
public void testFloatsIntoDouble() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/sum/floatsIntoDouble.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("indexUsed.kt")
|
||||||
|
public void testIndexUsed() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/sum/indexUsed.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("indexUsed2.kt")
|
||||||
|
public void testIndexUsed2() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/sum/indexUsed2.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("ints.kt")
|
||||||
|
public void testInts() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/sum/ints.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("longs.kt")
|
||||||
|
public void testLongs() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/sum/longs.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("nonNumbers.kt")
|
||||||
|
public void testNonNumbers() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/sum/nonNumbers.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("nonZeroInitial.kt")
|
||||||
|
public void testNonZeroInitial() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/sum/nonZeroInitial.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("short.kt")
|
||||||
|
public void testShort() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/sum/short.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("sumByDouble.kt")
|
||||||
|
public void testSumByDouble() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/sum/sumByDouble.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("sumByInts.kt")
|
||||||
|
public void testSumByInts() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/sum/sumByInts.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("sumByIntsIntoLong.kt")
|
||||||
|
public void testSumByIntsIntoLong() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/sum/sumByIntsIntoLong.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("sumByShorts.kt")
|
||||||
|
public void testSumByShorts() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/sum/sumByShorts.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8340,6 +8340,105 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
|||||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/twoInitializationsBeforeLoop.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/twoInitializationsBeforeLoop.kt");
|
||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("idea/testData/intentions/loopToCallChain/sum")
|
||||||
|
@TestDataPath("$PROJECT_ROOT")
|
||||||
|
@RunWith(JUnit3RunnerWithInners.class)
|
||||||
|
public static class Sum extends AbstractIntentionTest {
|
||||||
|
public void testAllFilesPresentInSum() throws Exception {
|
||||||
|
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/loopToCallChain/sum"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("bytes.kt")
|
||||||
|
public void testBytes() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/sum/bytes.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("doubles.kt")
|
||||||
|
public void testDoubles() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/sum/doubles.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("floats.kt")
|
||||||
|
public void testFloats() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/sum/floats.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("floatsIntoDouble.kt")
|
||||||
|
public void testFloatsIntoDouble() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/sum/floatsIntoDouble.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("indexUsed.kt")
|
||||||
|
public void testIndexUsed() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/sum/indexUsed.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("indexUsed2.kt")
|
||||||
|
public void testIndexUsed2() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/sum/indexUsed2.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("ints.kt")
|
||||||
|
public void testInts() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/sum/ints.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("longs.kt")
|
||||||
|
public void testLongs() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/sum/longs.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("nonNumbers.kt")
|
||||||
|
public void testNonNumbers() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/sum/nonNumbers.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("nonZeroInitial.kt")
|
||||||
|
public void testNonZeroInitial() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/sum/nonZeroInitial.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("short.kt")
|
||||||
|
public void testShort() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/sum/short.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("sumByDouble.kt")
|
||||||
|
public void testSumByDouble() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/sum/sumByDouble.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("sumByInts.kt")
|
||||||
|
public void testSumByInts() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/sum/sumByInts.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("sumByIntsIntoLong.kt")
|
||||||
|
public void testSumByIntsIntoLong() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/sum/sumByIntsIntoLong.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("sumByShorts.kt")
|
||||||
|
public void testSumByShorts() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/sum/sumByShorts.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@TestMetadata("idea/testData/intentions/moveAssignmentToInitializer")
|
@TestMetadata("idea/testData/intentions/moveAssignmentToInitializer")
|
||||||
|
|||||||
Reference in New Issue
Block a user