From 66d738848b63c1276c3b4176652e752fe30ece18 Mon Sep 17 00:00:00 2001 From: Toshiaki Kameyama Date: Mon, 9 Apr 2018 04:20:55 +0300 Subject: [PATCH] Convert foreach to for loop: place caret on the variable declaration So #KT-7822 Fixed --- .../ConvertForEachToForLoopIntention.kt | 23 +++++++++++-------- .../convertForEachToForLoop/simple.kt.after | 2 +- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertForEachToForLoopIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertForEachToForLoopIntention.kt index 0494664314f..edfdd52dfa2 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertForEachToForLoopIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertForEachToForLoopIntention.kt @@ -21,16 +21,20 @@ import org.jetbrains.kotlin.idea.caches.resolve.analyze import org.jetbrains.kotlin.idea.util.CommentSaver import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.psi.psiUtil.forEachDescendantOfType +import org.jetbrains.kotlin.psi.psiUtil.startOffset import org.jetbrains.kotlin.resolve.BindingContext import org.jetbrains.kotlin.resolve.DescriptorUtils import org.jetbrains.kotlin.resolve.bindingContextUtil.getTargetFunction import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall import org.jetbrains.kotlin.resolve.scopes.receivers.ExpressionReceiver -class ConvertForEachToForLoopIntention : SelfTargetingOffsetIndependentIntention(KtSimpleNameExpression::class.java, "Replace with a 'for' loop") { - private val FOR_EACH_NAME = "forEach" - private val FOR_EACH_FQ_NAMES = listOf("collections", "sequences", "text", "ranges").map { "kotlin.$it.$FOR_EACH_NAME" }.toSet() - +class ConvertForEachToForLoopIntention : SelfTargetingOffsetIndependentIntention( + KtSimpleNameExpression::class.java, "Replace with a 'for' loop" +) { + companion object { + private const val FOR_EACH_NAME = "forEach" + private val FOR_EACH_FQ_NAMES = listOf("collections", "sequences", "text", "ranges").map { "kotlin.$it.$FOR_EACH_NAME" }.toSet() + } override fun isApplicableTo(element: KtSimpleNameExpression): Boolean { if (element.getReferencedName() != FOR_EACH_NAME) return false @@ -48,16 +52,17 @@ class ConvertForEachToForLoopIntention : SelfTargetingOffsetIndependentIntention val commentSaver = CommentSaver(expressionToReplace) val loop = generateLoop(functionLiteral, receiver, context) - val result = expressionToReplace.replace(loop) + val result = expressionToReplace.replace(loop) as KtForExpression + result.loopParameter?.also { editor?.caretModel?.moveToOffset(it.startOffset) } commentSaver.restore(result) } private data class Data( - val expressionToReplace: KtExpression, - val receiver: KtExpression, - val functionLiteral: KtLambdaExpression, - val context: BindingContext + val expressionToReplace: KtExpression, + val receiver: KtExpression, + val functionLiteral: KtLambdaExpression, + val context: BindingContext ) private fun extractData(nameExpr: KtSimpleNameExpression): Data? { diff --git a/idea/testData/intentions/convertForEachToForLoop/simple.kt.after b/idea/testData/intentions/convertForEachToForLoop/simple.kt.after index f0b1a6a245a..35bc0518185 100644 --- a/idea/testData/intentions/convertForEachToForLoop/simple.kt.after +++ b/idea/testData/intentions/convertForEachToForLoop/simple.kt.after @@ -2,7 +2,7 @@ fun foo() { val x = 1..4 - for (it in x) { + for (it in x) { it.equals(1) } } \ No newline at end of file