Copy: Rename only self-usages of the copied class
#KT-18034 Fixed
This commit is contained in:
+9
-4
@@ -25,18 +25,18 @@ import com.intellij.openapi.vfs.VirtualFile
|
||||
import com.intellij.psi.PsiDirectory
|
||||
import com.intellij.psi.PsiElement
|
||||
import com.intellij.psi.PsiFile
|
||||
import com.intellij.psi.search.LocalSearchScope
|
||||
import com.intellij.psi.search.searches.ReferencesSearch
|
||||
import com.intellij.refactoring.BaseRefactoringProcessor
|
||||
import com.intellij.refactoring.RefactoringBundle
|
||||
import com.intellij.refactoring.copy.CopyFilesOrDirectoriesDialog
|
||||
import com.intellij.refactoring.copy.CopyHandlerDelegateBase
|
||||
import com.intellij.refactoring.rename.RenameProcessor
|
||||
import com.intellij.refactoring.util.MoveRenameUsageInfo
|
||||
import com.intellij.usageView.UsageInfo
|
||||
import com.intellij.util.IncorrectOperationException
|
||||
import com.intellij.util.containers.MultiMap
|
||||
import org.jetbrains.annotations.TestOnly
|
||||
import org.jetbrains.kotlin.idea.codeInsight.shorten.performDelayedRefactoringRequests
|
||||
import org.jetbrains.kotlin.idea.core.quoteIfNeeded
|
||||
import org.jetbrains.kotlin.idea.refactoring.checkConflictsInteractively
|
||||
import org.jetbrains.kotlin.idea.refactoring.createKotlinFile
|
||||
import org.jetbrains.kotlin.idea.refactoring.move.*
|
||||
@@ -240,8 +240,13 @@ class CopyKotlinDeclarationsHandler : CopyHandlerDelegateBase() {
|
||||
performDelayedRefactoringRequests(project)
|
||||
}
|
||||
|
||||
oldToNewElementsMapping.values.singleOrNull()?.let {
|
||||
RenameProcessor(project, it, newName!!.quoteIfNeeded(), false, false).run()
|
||||
(oldToNewElementsMapping.values.singleOrNull() as? KtNamedDeclaration)?.let { newDeclaration ->
|
||||
if (newName == newDeclaration.name) return@let
|
||||
val selfReferences = ReferencesSearch.search(newDeclaration, LocalSearchScope(newDeclaration)).findAll()
|
||||
runWriteAction {
|
||||
selfReferences.forEach { it.handleElementRename(newName!!) }
|
||||
newDeclaration.setName(newName!!)
|
||||
}
|
||||
}
|
||||
|
||||
if (openInEditor) {
|
||||
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
package foo
|
||||
|
||||
class X {
|
||||
val a: X = X()
|
||||
val b: B = B()
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package foo
|
||||
|
||||
class A {
|
||||
val a: A = A()
|
||||
val b: B = B()
|
||||
}
|
||||
|
||||
class B {
|
||||
val a: A = A()
|
||||
val b: B = B()
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package foo
|
||||
|
||||
class <caret>A {
|
||||
val a: A = A()
|
||||
val b: B = B()
|
||||
}
|
||||
|
||||
class B {
|
||||
val a: A = A()
|
||||
val b: B = B()
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"mainFile": "foo/test.kt",
|
||||
"targetPackage": "foo",
|
||||
"newName": "X"
|
||||
}
|
||||
@@ -54,6 +54,12 @@ public class CopyTestGenerated extends AbstractCopyTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("copyClassToSamePackageWithRename/copyClassToSamePackageWithRename.test")
|
||||
public void testCopyClassToSamePackageWithRename_CopyClassToSamePackageWithRename() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/copy/copyClassToSamePackageWithRename/copyClassToSamePackageWithRename.test");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("copyClassWithCompanionRefs/copyClassWithCompanionRefs.test")
|
||||
public void testCopyClassWithCompanionRefs_CopyClassWithCompanionRefs() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/copy/copyClassWithCompanionRefs/copyClassWithCompanionRefs.test");
|
||||
|
||||
Reference in New Issue
Block a user