More informative intention action text

This commit is contained in:
Valentin Kipyatkov
2016-04-20 14:52:00 +03:00
parent bbb6ef4fbc
commit fe7ddbcc0d
135 changed files with 273 additions and 30 deletions

View File

@@ -33,7 +33,23 @@ class LoopToCallChainIntention : SelfTargetingRangeIntention<KtForExpression>(
"Replace with stdlib operations"
) {
override fun applicabilityRange(element: KtForExpression): TextRange? {
return if (match(element) != null) element.forKeyword.textRange else null
val match = match(element)
text = if (match != null) "Replace with '${match.buildPresentation()}'" else defaultText
return if (match != null) element.forKeyword.textRange else null
}
private fun ResultTransformationMatch.buildPresentation(): String {
var transformations = sequenceTransformations + resultTransformation
val MAX = 3
var result: String? = null
if (transformations.size > MAX) {
transformations = transformations.drop(transformations.size - MAX)
result = ".."
}
for (transformation in transformations) {
result = transformation.buildPresentation(result)
}
return result!!
}
override fun applyTo(element: KtForExpression, editor: Editor?) {

View File

@@ -40,6 +40,15 @@ interface Transformation {
val inputVariable: KtCallableDeclaration
val loop: KtForExpression
val presentation: String
open fun buildPresentation(prevTransformationsPresentation: String?): String {
return if (prevTransformationsPresentation != null)
prevTransformationsPresentation + "." + presentation
else
presentation
}
fun generateCode(chainedCallGenerator: ChainedCallGenerator): KtExpression
}

View File

@@ -55,6 +55,16 @@ class AddToCollectionTransformation(
}
}
override val presentation: String
get() = "+="
override fun buildPresentation(prevTransformationsPresentation: String?): String {
return if (prevTransformationsPresentation != null)
"+= $prevTransformationsPresentation"
else
"+="
}
override fun generateCode(chainedCallGenerator: ChainedCallGenerator): KtExpression {
return KtPsiFactory(loop).createExpressionByPattern("$0 += $1", targetCollection, chainedCallGenerator.receiver)
}
@@ -170,6 +180,9 @@ class FilterToTransformation private constructor(
private val filter: KtExpression
) : ReplaceLoopResultTransformation(loop, inputVariable) {
override val presentation: String
get() = "filterTo(){}"
override fun generateCode(chainedCallGenerator: ChainedCallGenerator): KtExpression {
val lambda = generateLambda(inputVariable, filter)
return chainedCallGenerator.generate("filterTo($0) $1:'{}'", targetCollection, lambda)
@@ -200,6 +213,9 @@ class AssignFilterToTransformation(
private val filter: KtExpression
) : AssignToVariableResultTransformation(loop, inputVariable, targetCollectionInitialization) {
override val presentation: String
get() = "filterTo(){}"
override fun generateCode(chainedCallGenerator: ChainedCallGenerator): KtExpression {
val lambda = generateLambda(inputVariable, filter)
return chainedCallGenerator.generate("filterTo($0) $1:'{}'", initialization.initializer, lambda)
@@ -213,6 +229,9 @@ class MapToTransformation private constructor(
private val mapping: KtExpression
) : ReplaceLoopResultTransformation(loop, inputVariable) {
override val presentation: String
get() = "mapTo(){}"
override fun generateCode(chainedCallGenerator: ChainedCallGenerator): KtExpression {
val lambda = generateLambda(inputVariable, mapping)
return chainedCallGenerator.generate("mapTo($0) $1:'{}'", targetCollection, lambda)
@@ -243,6 +262,9 @@ class AssignMapToTransformation(
private val mapping: KtExpression
) : AssignToVariableResultTransformation(loop, inputVariable, targetCollectionInitialization) {
override val presentation: String
get() = "mapTo(){}"
override fun generateCode(chainedCallGenerator: ChainedCallGenerator): KtExpression {
val lambda = generateLambda(inputVariable, mapping)
return chainedCallGenerator.generate("mapTo($0) $1:'{}'", initialization.initializer, lambda)
@@ -256,6 +278,9 @@ class FlatMapToTransformation private constructor(
private val transform: KtExpression
) : ReplaceLoopResultTransformation(loop, inputVariable) {
override val presentation: String
get() = "flatMapTo(){}"
override fun generateCode(chainedCallGenerator: ChainedCallGenerator): KtExpression {
val lambda = generateLambda(inputVariable, transform)
return chainedCallGenerator.generate("flatMapTo($0) $1:'{}'", targetCollection, lambda)
@@ -286,6 +311,9 @@ class AssignFlatMapToTransformation(
private val transform: KtExpression
) : AssignToVariableResultTransformation(loop, inputVariable, targetCollectionInitialization) {
override val presentation: String
get() = "flatMapTo(){}"
override fun generateCode(chainedCallGenerator: ChainedCallGenerator): KtExpression {
val lambda = generateLambda(inputVariable, transform)
return chainedCallGenerator.generate("flatMapTo($0) $1:'{}'", initialization.initializer, lambda)
@@ -298,6 +326,9 @@ class AssignToListTransformation(
initialization: VariableInitialization
) : AssignToVariableResultTransformation(loop, inputVariable, initialization) {
override val presentation: String
get() = "toList()"
override fun mergeWithPrevious(previousTransformation: SequenceTransformation): ResultTransformation? {
if (previousTransformation !is FilterTransformation) return null
return AssignSequenceTransformationResultTransformation(previousTransformation, initialization)
@@ -314,6 +345,9 @@ class AssignToMutableListTransformation(
initialization: VariableInitialization
) : AssignToVariableResultTransformation(loop, inputVariable, initialization) {
override val presentation: String
get() = "toMutableList()"
override fun generateCode(chainedCallGenerator: ChainedCallGenerator): KtExpression {
return chainedCallGenerator.generate("toMutableList()")
}
@@ -325,6 +359,9 @@ class AssignToSetTransformation(
initialization: VariableInitialization
) : AssignToVariableResultTransformation(loop, inputVariable, initialization) {
override val presentation: String
get() = "toSet()"
override fun generateCode(chainedCallGenerator: ChainedCallGenerator): KtExpression {
return chainedCallGenerator.generate("toSet()")
}
@@ -336,6 +373,9 @@ class AssignToMutableSetTransformation(
initialization: VariableInitialization
) : AssignToVariableResultTransformation(loop, inputVariable, initialization) {
override val presentation: String
get() = "toMutableSet()"
override fun generateCode(chainedCallGenerator: ChainedCallGenerator): KtExpression {
return chainedCallGenerator.generate("toMutableSet()")
}

View File

@@ -29,7 +29,7 @@ import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
class FindAndAssignTransformation(
loop: KtForExpression,
inputVariable: KtCallableDeclaration,
private val generator: (chainedCallGenerator: ChainedCallGenerator, filter: KtExpression?) -> KtExpression,
private val generator: FindOperatorGenerator,
initialization: VariableInitialization,
private val filter: KtExpression? = null
) : AssignToVariableResultTransformation(loop, inputVariable, initialization) {
@@ -40,8 +40,11 @@ class FindAndAssignTransformation(
return FindAndAssignTransformation(loop, previousTransformation.inputVariable, generator, initialization, previousTransformation.effectiveCondition())
}
override val presentation: String
get() = generator.functionName + (if (filter != null) "{}" else "()")
override fun generateCode(chainedCallGenerator: ChainedCallGenerator): KtExpression {
return generator(chainedCallGenerator, filter)
return generator.generate(chainedCallGenerator, filter)
}
/**

View File

@@ -27,7 +27,7 @@ import org.jetbrains.kotlin.psi.psiUtil.PsiChildRange
class FindAndReturnTransformation(
override val loop: KtForExpression,
override val inputVariable: KtCallableDeclaration,
private val generator: (chainedCallGenerator: ChainedCallGenerator, filter: KtExpression?) -> KtExpression,
private val generator: FindOperatorGenerator,
private val endReturn: KtReturnExpression,
private val filter: KtExpression? = null
) : ResultTransformation {
@@ -44,8 +44,11 @@ class FindAndReturnTransformation(
override fun commentRestoringRange(convertLoopResult: KtExpression) = commentRestoringRange
override val presentation: String
get() = generator.functionName + (if (filter != null) "{}" else "()")
override fun generateCode(chainedCallGenerator: ChainedCallGenerator): KtExpression {
return generator(chainedCallGenerator, filter)
return generator.generate(chainedCallGenerator, filter)
}
override fun convertLoop(resultCallChain: KtExpression): KtExpression {

View File

@@ -43,6 +43,9 @@ class FilterTransformation(
override val affectsIndex: Boolean
get() = true
override val presentation: String
get() = if (isInverse) "filterNot{}" else "filter{}"
override fun generateCode(chainedCallGenerator: ChainedCallGenerator): KtExpression {
val lambda = generateLambda(inputVariable, condition)
val name = if (isInverse) "filterNot" else "filter"
@@ -128,6 +131,9 @@ class FilterIsInstanceTransformation(
override val affectsIndex: Boolean
get() = true
override val presentation: String
get() = "filterIsInstance<>()"
override fun generateCode(chainedCallGenerator: ChainedCallGenerator): KtExpression {
return chainedCallGenerator.generate("filterIsInstance<$0>()", type)
}
@@ -141,6 +147,9 @@ class FilterNotNullTransformation(
override val affectsIndex: Boolean
get() = true
override val presentation: String
get() = "filterNotNull()"
override fun generateCode(chainedCallGenerator: ChainedCallGenerator): KtExpression {
return chainedCallGenerator.generate("filterNotNull()")
}

View File

@@ -34,6 +34,9 @@ class FlatMapTransformation(
override val affectsIndex: Boolean
get() = true
override val presentation: String
get() = "flatMap{}"
override fun generateCode(chainedCallGenerator: ChainedCallGenerator): KtExpression {
val lambda = generateLambda(inputVariable, transform)
return chainedCallGenerator.generate("flatMap$0:'{}'", lambda)

View File

@@ -31,6 +31,9 @@ class MapTransformation(
override val affectsIndex: Boolean
get() = false
override val presentation: String
get() = "map{}"
override fun generateCode(chainedCallGenerator: ChainedCallGenerator): KtExpression {
val lambda = generateLambda(inputVariable, mapping)
return chainedCallGenerator.generate("map$0:'{}'", lambda)

View File

@@ -112,12 +112,17 @@ fun KtProperty.hasWriteUsages(): Boolean {
}
}
interface FindOperatorGenerator {
val functionName: String
fun generate(chainedCallGenerator: ChainedCallGenerator, filter: KtExpression?): KtExpression
}
fun buildFindOperationGenerator(
valueIfFound: KtExpression,
valueIfNotFound: KtExpression,
inputVariable: KtCallableDeclaration,
findFirst: Boolean
): ((chainedCallGenerator: ChainedCallGenerator, filter: KtExpression?) -> KtExpression)? {
): FindOperatorGenerator? {
assert(valueIfFound.isPhysical)
assert(valueIfNotFound.isPhysical)
@@ -131,36 +136,40 @@ fun buildFindOperationGenerator(
}
}
class SimpleGenerator(override val functionName: String) : FindOperatorGenerator {
override fun generate(chainedCallGenerator: ChainedCallGenerator, filter: KtExpression?): KtExpression {
return generateChainedCall(functionName, chainedCallGenerator, filter)
}
}
val inputVariableCanHoldNull = (inputVariable.resolveToDescriptor() as VariableDescriptor).type.nullability() != TypeNullability.NOT_NULL
fun ((chainedCallGenerator: ChainedCallGenerator, filter: KtExpression?) -> KtExpression).useElvisOperatorIfNeeded(): ((chainedCallGenerator: ChainedCallGenerator, filter: KtExpression?) -> KtExpression)? {
fun FindOperatorGenerator.useElvisOperatorIfNeeded(): FindOperatorGenerator? {
if (valueIfNotFound.isNullExpression()) return this
// we cannot use ?: if found value can be null
if (inputVariableCanHoldNull) return null
return { chainedCallGenerator, filter ->
val generated = this(chainedCallGenerator, filter)
KtPsiFactory(generated).createExpressionByPattern("$0 ?: $1", generated, valueIfNotFound)
return object: FindOperatorGenerator {
override val functionName: String
get() = this@useElvisOperatorIfNeeded.functionName
override fun generate(chainedCallGenerator: ChainedCallGenerator, filter: KtExpression?): KtExpression {
val generated = this@useElvisOperatorIfNeeded.generate(chainedCallGenerator, filter)
return KtPsiFactory(generated).createExpressionByPattern("$0 ?: $1", generated, valueIfNotFound)
}
}
}
when {
valueIfFound.isVariableReference(inputVariable) -> {
val stdlibFunName = if (findFirst) "firstOrNull" else "lastOrNull"
val generator = { chainedCallGenerator: ChainedCallGenerator, filter: KtExpression? ->
generateChainedCall(stdlibFunName, chainedCallGenerator, filter)
}
val generator = SimpleGenerator(if (findFirst) "firstOrNull" else "lastOrNull")
return generator.useElvisOperatorIfNeeded()
}
valueIfFound.isTrueConstant() && valueIfNotFound.isFalseConstant() -> {
return { chainedCallGenerator, filter -> generateChainedCall("any", chainedCallGenerator, filter) }
}
valueIfFound.isTrueConstant() && valueIfNotFound.isFalseConstant() -> return SimpleGenerator("any")
valueIfFound.isFalseConstant() && valueIfNotFound.isTrueConstant() -> {
return { chainedCallGenerator, filter -> generateChainedCall("none", chainedCallGenerator, filter) }
}
valueIfFound.isFalseConstant() && valueIfNotFound.isTrueConstant() -> return SimpleGenerator("none")
inputVariable.hasUsages(valueIfFound) -> {
if (!findFirst) return null // too dangerous because of side effects
@@ -171,9 +180,14 @@ fun buildFindOperationGenerator(
val receiver = qualifiedExpression.receiverExpression
val selector = qualifiedExpression.selectorExpression
if (receiver.isVariableReference(inputVariable) && selector != null && !inputVariable.hasUsages(selector)) {
return { chainedCallGenerator: ChainedCallGenerator, filter: KtExpression? ->
val findFirstCall = generateChainedCall("firstOrNull", chainedCallGenerator, filter)
KtPsiFactory(findFirstCall).createExpressionByPattern("$0?.$1", findFirstCall, selector)
return object: FindOperatorGenerator {
override val functionName: String
get() = "firstOrNull"
override fun generate(chainedCallGenerator: ChainedCallGenerator, filter: KtExpression?): KtExpression {
val findFirstCall = generateChainedCall(functionName, chainedCallGenerator, filter)
return KtPsiFactory(findFirstCall).createExpressionByPattern("$0?.$1", findFirstCall, selector)
}
}.useElvisOperatorIfNeeded()
}
}
@@ -181,17 +195,27 @@ fun buildFindOperationGenerator(
// in case of nullable input variable we cannot distinguish by the result of "firstOrNull" whether nothing was found or 'null' was found
if (inputVariableCanHoldNull) return null
return { chainedCallGenerator: ChainedCallGenerator, filter: KtExpression? ->
val findFirstCall = generateChainedCall("firstOrNull", chainedCallGenerator, filter)
val letBody = generateLambda(inputVariable, valueIfFound)
KtPsiFactory(findFirstCall).createExpressionByPattern("$0?.let $1:'{}'", findFirstCall, letBody)
return object: FindOperatorGenerator {
override val functionName: String
get() = "firstOrNull" //TODO
override fun generate(chainedCallGenerator: ChainedCallGenerator, filter: KtExpression?): KtExpression {
val findFirstCall = generateChainedCall(functionName, chainedCallGenerator, filter)
val letBody = generateLambda(inputVariable, valueIfFound)
return KtPsiFactory(findFirstCall).createExpressionByPattern("$0?.let $1:'{}'", findFirstCall, letBody)
}
}.useElvisOperatorIfNeeded()
}
else -> {
return { chainedCallGenerator, filter ->
val chainedCall = generateChainedCall("any", chainedCallGenerator, filter)
KtPsiFactory(chainedCall).createExpressionByPattern("if ($0) $1 else $2", chainedCall, valueIfFound, valueIfNotFound)
return object: FindOperatorGenerator {
override val functionName: String
get() = "any"
override fun generate(chainedCallGenerator: ChainedCallGenerator, filter: KtExpression?): KtExpression {
val chainedCall = generateChainedCall(functionName, chainedCallGenerator, filter)
return KtPsiFactory(chainedCall).createExpressionByPattern("if ($0) $1 else $2", chainedCall, valueIfFound, valueIfNotFound)
}
}
}
}
@@ -313,6 +337,13 @@ class AssignSequenceTransformationResultTransformation(
initialization: VariableInitialization
) : AssignToVariableResultTransformation(sequenceTransformation.loop, sequenceTransformation.inputVariable, initialization) {
override val presentation: String
get() = sequenceTransformation.presentation
override fun buildPresentation(prevTransformationsPresentation: String?): String {
return sequenceTransformation.buildPresentation(prevTransformationsPresentation)
}
override fun generateCode(chainedCallGenerator: ChainedCallGenerator): KtExpression {
return sequenceTransformation.generateCode(chainedCallGenerator)
}

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with '+='"
fun foo(list: List<String>, target: MutableList<String>) {
<caret>for (s in list) {
target.add(s)

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with '+='"
fun foo(list: List<String>, target: MutableList<String>) {
<caret>target += list
}

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'any{}'"
fun foo(list: List<String>) {
var found = false
<caret>for (s in list) {

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'any{}'"
fun foo(list: List<String>) {
<caret>val found = list.any { it.length > 0 }
}

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'any{}'"
fun foo(list: List<String>) {
var found = false
<caret>for (s in list) {

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'any{}'"
fun foo(list: List<String>) {
<caret>val found = list.any { it.length > 0 }
}

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'any{}'"
fun foo(list: List<String>) {
var result = 0
<caret>for (s in list) {

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'any{}'"
fun foo(list: List<String>) {
<caret>val result = if (list.any { it.length > 0 }) 1 else 0
}

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'any{}'"
fun foo(list: List<String>): Boolean {
<caret>for (s in list) {
if (s.length > 0) {

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'any{}'"
fun foo(list: List<String>): Boolean {
<caret>return list.any { it.length > 0 }
}

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'any{}'"
fun foo(list: List<String>): Int {
<caret>for (s in list) {
if (s.length > 0) {

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'any{}'"
fun foo(list: List<String>): Int {
<caret>return if (list.any { it.length > 0 }) -1 else takeInt()
}

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'any()'"
fun foo(list: List<String>): Boolean {
<caret>for (s in list) {
return true

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'any()'"
fun foo(list: List<String>): Boolean {
<caret>return list.any()
}

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'filter{}'"
import java.util.ArrayList
fun foo(list: List<String>): List<String> {

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'filter{}'"
import java.util.ArrayList
fun foo(list: List<String>): List<String> {

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'filter{}'"
import java.util.ArrayList
fun foo(list: List<String>, p: Int): List<String> {

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'filter{}'"
import java.util.ArrayList
fun foo(list: List<String>, p: Int): List<String> {

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'filterTo(){}'"
import java.util.ArrayList
fun foo(list: List<String>): ArrayList<String> {

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'filterTo(){}'"
import java.util.ArrayList
fun foo(list: List<String>): ArrayList<String> {

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'filterTo(){}'"
import java.util.ArrayList
fun foo(list: List<String>, p: Int): ArrayList<String> {

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'filterTo(){}'"
import java.util.ArrayList
fun foo(list: List<String>, p: Int): ArrayList<String> {

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'filterTo(){}'"
import java.util.*
fun foo(list: List<String>): ArrayList<String> {

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'filterTo(){}'"
import java.util.*
fun foo(list: List<String>): ArrayList<String> {

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'filter{}.toMutableList()'"
import java.util.ArrayList
fun foo(list: List<String>): MutableList<String> {

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'filter{}.toMutableList()'"
import java.util.ArrayList
fun foo(list: List<String>): MutableList<String> {

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'filter{}'"
import java.util.ArrayList
fun foo(): List<String> {

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'filter{}'"
import java.util.ArrayList
fun foo(): List<String> {

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'filter{}.map{}'"
import java.util.ArrayList
fun foo(list: List<String>): List<Int> {

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'filter{}.map{}'"
import java.util.ArrayList
fun foo(list: List<String>): List<Int> {

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'filterIsInstance<>().map{}.firstOrNull()'"
fun foo(list: List<Any>): Int? {
<caret>for (o in list) {
if (o is String) {

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'filterIsInstance<>().map{}.firstOrNull()'"
fun foo(list: List<Any>): Int? {
return list
.filterIsInstance<String>()

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'filterIsInstance<>().map{}.firstOrNull()'"
fun foo(list: List<Any>): Int? {
<caret>for (o in list) {
if (o !is String) continue

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'filterIsInstance<>().map{}.firstOrNull()'"
fun foo(list: List<Any>): Int? {
return list
.filterIsInstance<String>()

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'filterNotNull().map{}.firstOrNull()'"
fun foo(list: List<String?>): Int? {
<caret>for (s in list) {
if (s != null) {

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'filterNotNull().map{}.firstOrNull()'"
fun foo(list: List<String?>): Int? {
<caret>return list
.filterNotNull()

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'filterNotNull().map{}.firstOrNull()'"
fun foo(list: List<Any?>): Int? {
<caret>for (o in list) {
if (o == null) continue

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'filterNotNull().map{}.firstOrNull()'"
fun foo(list: List<Any?>): Int? {
<caret>return list
.filterNotNull()

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'filterNot{}.map{}.firstOrNull()'"
fun foo(list: List<String>): Int? {
<caret>for (s in list) {
if (s.isEmpty()) continue

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'filterNot{}.map{}.firstOrNull()'"
fun foo(list: List<String>): Int? {
<caret>return list
.filterNot { it.isEmpty() }

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'filterTo(){}'"
fun foo(list: List<String>, target: MutableList<String>) {
<caret>for (s in list) {
if (s.length > 0)

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'filterTo(){}'"
fun foo(list: List<String>, target: MutableList<String>) {
list.filterTo(target) { it.length > 0 }
}

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'filterTo(){}'"
fun foo(list: List<String>) {
val target = createCollection()
<caret>for (s in list) {

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'filterTo(){}'"
fun foo(list: List<String>) {
val target = createCollection()
<caret>list.filterTo(target) { it.length > 0 }

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'firstOrNull{}'"
fun foo(list: List<String>): String? {
<caret>for (s in list) {
if (s.isEmpty()) continue

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'firstOrNull{}'"
fun foo(list: List<String>): String? {
<caret>return list.firstOrNull { !it.isEmpty() }
}

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'firstOrNull{}'"
fun foo(list: List<String>): String? {
<caret>for (s in list) {
if (!s.isEmpty()) continue

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'firstOrNull{}'"
fun foo(list: List<String>): String? {
<caret>return list.firstOrNull { it.isEmpty() }
}

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'firstOrNull{}'"
fun foo(list: List<String>): String? {
<caret>for (s in list) {
if (s.isEmpty()) {

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'firstOrNull{}'"
fun foo(list: List<String>): String? {
<caret>return list.firstOrNull { !it.isEmpty() }
}

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'filter{}.map{}.firstOrNull()'"
fun foo(list: List<String>): String? {
<caret>for (s in list) {
if (s.isEmpty()) continue

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'filter{}.map{}.firstOrNull()'"
fun foo(list: List<String>): String? {
return list
.filter { !it.isEmpty() && it.length < 10 && it != "abc" && it != "def" }

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'firstOrNull{}'"
fun foo(list: List<String>) {
var result: String? = ""

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'firstOrNull{}'"
fun foo(list: List<String>) {
var result: String? = ""

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'firstOrNull{}'"
fun foo(list: List<String>) {
var result: String? = null
<caret>for (s in list) {

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'firstOrNull{}'"
fun foo(list: List<String>) {
<caret>val result: String? = list.firstOrNull { it.length > 0 }
}

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'firstOrNull{}'"
fun foo(list: List<String>) {
var result: String? = null
<caret>for (s in list) {

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'firstOrNull{}'"
fun foo(list: List<String>) {
<caret>var result: String? = list.firstOrNull { it.length > 0 }

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'firstOrNull{}'"
fun foo(list: List<String>) {
var result: String? = null
<caret>for (s in list) { // search for first non-empty string in the list

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'firstOrNull{}'"
fun foo(list: List<String>) {
<caret>val result: String? = list.firstOrNull { // search for first non-empty string in the list
it.length > 0

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'firstOrNull{}'"
fun foo(list: List<String>): String? {
<caret>for (s in list) {
if (s.length > 0) {

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'firstOrNull{}'"
fun foo(list: List<String>): String? {
<caret>return list.firstOrNull { it.length > 0 }
}

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'firstOrNull{}'"
fun foo(list: List<String>) {
var result: String? = null
<caret>for (s in list) {

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'firstOrNull{}'"
fun foo(list: List<String>) {
<caret>val result: String? = list.firstOrNull { it.length > 0 }?.let { bar(it) }
}

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'firstOrNull{}'"
fun foo(list: List<String>) {
var result: String? = null
<caret>for (s in list) {

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'firstOrNull{}'"
fun foo(list: List<String>) {
<caret>val result: String? = list.firstOrNull { it.length > 0 }?.let { it.substring(0, it.length - 1) }
}

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'firstOrNull{}'"
fun foo(list: List<String>) {
var result = ""
<caret>for (s in list) {

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'firstOrNull{}'"
fun foo(list: List<String>) {
<caret>val result = list.firstOrNull { it.length > 0 }?.let { bar(it) } ?: ""
}

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'firstOrNull()'"
fun foo(list: List<String>): String? {
<caret>for (s in list) {
return s

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'firstOrNull()'"
fun foo(list: List<String>): String? {
<caret>return list.firstOrNull()
}

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'firstOrNull{}'"
fun foo(list: List<String>): Int? {
<caret>for (s in list) {
if (s.isNotEmpty()) {

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'firstOrNull{}'"
fun foo(list: List<String>): Int? {
<caret>return list.firstOrNull { it.isNotEmpty() }?.length
}

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'firstOrNull{}'"
fun foo(list: List<String>): Int {
<caret>for (s in list) {
if (s.isNotEmpty()) {

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'firstOrNull{}'"
fun foo(list: List<String>): Int {
<caret>return list.firstOrNull { it.isNotEmpty() }?.length ?: -1
}

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'firstOrNull{}'"
fun foo(list: List<String>): String {
<caret>for (s in list) {
if (s.isNotEmpty()) {

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'firstOrNull{}'"
fun foo(list: List<String>): String {
<caret>return list.firstOrNull { it.isNotEmpty() } ?: ""
}

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'firstOrNull()'"
fun foo(list: List<String>): String? {
<caret>for (s in list) {
return s

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'firstOrNull()'"
fun foo(list: List<String>): String? {
// return null if not found
return list.firstOrNull()

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'firstOrNull{}'"
fun foo(list: List<String?>) {
var result: String? = null
<caret>for (s in list) {

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'firstOrNull{}'"
fun foo(list: List<String?>) {
<caret>val result: String? = list.firstOrNull { it != "" }?.substring(1)
}

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'firstOrNull{}'"
fun foo(list: List<String>): String? {
<caret>for (s in list) {
if (s.isEmpty()) continue

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'firstOrNull{}'"
fun foo(list: List<String>): String? {
<caret>return list.firstOrNull { !it.isEmpty() && it.length < 10 && it != "abc" && it != "def" }
}

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'flatMap{}.firstOrNull{}'"
fun foo(list: List<String>): String? {
<caret>for (s in list) {
for (line in s.lines()) {

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'flatMap{}.firstOrNull{}'"
fun foo(list: List<String>): String? {
<caret>return list
.flatMap { it.lines() }

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'flatMapTo(){}'"
fun foo(list: List<String>, target: MutableList<String>) {
<caret>for (s in list) {
for (line in s.lines()) {

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'flatMapTo(){}'"
fun foo(list: List<String>, target: MutableList<String>) {
<caret>list.flatMapTo(target) { it.lines() }
}

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'flatMapTo(){}'"
import java.util.ArrayList
fun foo(list: List<String>): List<String> {

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'flatMapTo(){}'"
import java.util.ArrayList
fun foo(list: List<String>): List<String> {

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'flatMapTo(){}'"
fun foo(list: List<String>): List<String> {
val target = createCollection()
<caret>for (s in list) {

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'flatMapTo(){}'"
fun foo(list: List<String>): List<String> {
val target = createCollection()
<caret>list.flatMapTo(target) { it.lines() }

View File

@@ -1,4 +1,5 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'flatMap{}.firstOrNull{}'"
fun foo(list: List<String>): String? {
var result: String? = null
MainLoop@

Some files were not shown because too many files have changed in this diff Show More