Rename: fix renaming of label by label reference in loop
#KT-29796 Fixed
This commit is contained in:
committed by
Alexander Podkhalyuzin
parent
76bdfddfea
commit
b4789b95ef
@@ -11,7 +11,7 @@
|
||||
<renameHandler implementation="org.jetbrains.kotlin.idea.refactoring.rename.RenameJvmNameHandler"/>
|
||||
<renameHandler implementation="org.jetbrains.kotlin.idea.refactoring.rename.RenameBackingFieldReferenceHandler"/>
|
||||
<renameHandler implementation="org.jetbrains.kotlin.idea.refactoring.rename.RenameClassByCompanionObjectShortReferenceHandler"/>
|
||||
<renameHandler implementation="org.jetbrains.kotlin.idea.refactoring.rename.RenameByLabeledReferenceInLambdaArgumentHandler"/>
|
||||
<renameHandler implementation="org.jetbrains.kotlin.idea.refactoring.rename.RenameByLabeledReferenceHandler"/>
|
||||
<renameHandler implementation="org.jetbrains.kotlin.idea.refactoring.rename.RenameImportAliasByReferenceHandler"/>
|
||||
<renameHandler implementation="org.jetbrains.kotlin.idea.refactoring.rename.KotlinMemberInplaceRenameHandler"/>
|
||||
<renameHandler implementation="org.jetbrains.kotlin.idea.refactoring.rename.KotlinVariableInplaceRenameHandler"/>
|
||||
|
||||
+8
-14
@@ -17,34 +17,28 @@
|
||||
package org.jetbrains.kotlin.idea.refactoring.rename
|
||||
|
||||
import com.intellij.openapi.actionSystem.DataContext
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.psi.PsiElement
|
||||
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||
import org.jetbrains.kotlin.idea.codeInsight.DescriptorToSourceUtilsIde
|
||||
import org.jetbrains.kotlin.idea.refactoring.project
|
||||
import org.jetbrains.kotlin.idea.references.getCalleeByLambdaArgument
|
||||
import org.jetbrains.kotlin.psi.KtExpression
|
||||
import org.jetbrains.kotlin.psi.KtFunction
|
||||
import org.jetbrains.kotlin.psi.KtLabelReferenceExpression
|
||||
import org.jetbrains.kotlin.psi.psiUtil.getLabeledParent
|
||||
import org.jetbrains.kotlin.resolve.BindingContext
|
||||
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
|
||||
|
||||
class RenameByLabeledReferenceInLambdaArgumentHandler :
|
||||
AbstractReferenceSubstitutionRenameHandler(KotlinVariableInplaceRenameHandler()) {
|
||||
class RenameByLabeledReferenceHandler : AbstractReferenceSubstitutionRenameHandler(KotlinVariableInplaceRenameHandler()) {
|
||||
override fun getElementToRename(dataContext: DataContext): PsiElement? {
|
||||
val refExpr = getReferenceExpression(dataContext) as? KtLabelReferenceExpression ?: return null
|
||||
val context = refExpr.analyze(BodyResolveMode.PARTIAL)
|
||||
val lambda = context[BindingContext.LABEL_TARGET, refExpr] as? KtFunction ?: return null
|
||||
val labeledParent = lambda.getLabeledParent(refExpr.getReferencedName())
|
||||
return if (labeledParent != null) {
|
||||
labeledParent
|
||||
}
|
||||
else {
|
||||
val calleeExpression = lambda.getCalleeByLambdaArgument() ?: return null
|
||||
val descriptor = context[BindingContext.REFERENCE_TARGET, calleeExpression] as? FunctionDescriptor ?: return null
|
||||
DescriptorToSourceUtilsIde.getAnyDeclaration(dataContext.project, descriptor)
|
||||
}
|
||||
val labelTarget = context[BindingContext.LABEL_TARGET, refExpr] as? KtExpression ?: return null
|
||||
val labeledParent = labelTarget.getLabeledParent(refExpr.getReferencedName())
|
||||
if (labelTarget !is KtFunction || labeledParent != null) return labeledParent
|
||||
val calleeExpression = labelTarget.getCalleeByLambdaArgument() ?: return null
|
||||
val descriptor = context[BindingContext.REFERENCE_TARGET, calleeExpression] as? FunctionDescriptor ?: return null
|
||||
return DescriptorToSourceUtilsIde.getAnyDeclaration(dataContext.project, descriptor)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
fun test() {
|
||||
bar@ for (n in 1..10) {
|
||||
if (n == 5) continue@bar
|
||||
if (n > 8) break@bar
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
fun test() {
|
||||
foo@ for (n in 1..10) {
|
||||
if (n == 5) continue@foo
|
||||
if (n > 8) break@/*rename*/foo
|
||||
}
|
||||
}
|
||||
Vendored
+5
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"type": "AUTO_DETECT",
|
||||
"mainFile": "test.kt",
|
||||
"newName": "bar"
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
fun test() {
|
||||
bar@ for (n in 1..10) {
|
||||
if (n == 5) continue@bar
|
||||
if (n > 8) break@bar
|
||||
}
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
fun test() {
|
||||
foo@ for (n in 1..10) {
|
||||
if (n == 5) continue@/*rename*/foo
|
||||
if (n > 8) break@foo
|
||||
}
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"type": "AUTO_DETECT",
|
||||
"mainFile": "test.kt",
|
||||
"newName": "bar"
|
||||
}
|
||||
+10
@@ -289,11 +289,21 @@ public class RenameTestGenerated extends AbstractRenameTest {
|
||||
runTest("idea/testData/refactoring/rename/labeledLoopByLabel/labeledLoopByLabel.test");
|
||||
}
|
||||
|
||||
@TestMetadata("labeledLoopByLabelRefInBreak2/labeledLoopByLabelRefInBreak.test")
|
||||
public void testLabeledLoopByLabelRefInBreak2_LabeledLoopByLabelRefInBreak() throws Exception {
|
||||
runTest("idea/testData/refactoring/rename/labeledLoopByLabelRefInBreak2/labeledLoopByLabelRefInBreak.test");
|
||||
}
|
||||
|
||||
@TestMetadata("labeledLoopByLabelRefInBreak/labeledLoopByLabelRefInBreak.test")
|
||||
public void testLabeledLoopByLabelRefInBreak_LabeledLoopByLabelRefInBreak() throws Exception {
|
||||
runTest("idea/testData/refactoring/rename/labeledLoopByLabelRefInBreak/labeledLoopByLabelRefInBreak.test");
|
||||
}
|
||||
|
||||
@TestMetadata("labeledLoopByLabelRefInContinue2/labeledLoopByLabelRefInContinue.test")
|
||||
public void testLabeledLoopByLabelRefInContinue2_LabeledLoopByLabelRefInContinue() throws Exception {
|
||||
runTest("idea/testData/refactoring/rename/labeledLoopByLabelRefInContinue2/labeledLoopByLabelRefInContinue.test");
|
||||
}
|
||||
|
||||
@TestMetadata("labeledLoopByLabelRefInContinue/labeledLoopByLabelRefInContinue.test")
|
||||
public void testLabeledLoopByLabelRefInContinue_LabeledLoopByLabelRefInContinue() throws Exception {
|
||||
runTest("idea/testData/refactoring/rename/labeledLoopByLabelRefInContinue/labeledLoopByLabelRefInContinue.test");
|
||||
|
||||
Reference in New Issue
Block a user