Convert foreach to for loop: place caret on the variable declaration
So #KT-7822 Fixed
This commit is contained in:
committed by
Mikhail Glukhikh
parent
3221fbdca9
commit
66d738848b
@@ -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>(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>(
|
||||
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? {
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
fun foo() {
|
||||
val x = 1..4
|
||||
|
||||
for (it in x) {
|
||||
for (<caret>it in x) {
|
||||
it.equals(1)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user