Copy: Rename only self-usages of the copied class

#KT-18034 Fixed
This commit is contained in:
Alexey Sedunov
2017-05-23 14:32:32 +03:00
parent 4252d9786b
commit 2f88b6ef9b
6 changed files with 48 additions and 4 deletions
@@ -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) {
@@ -0,0 +1,6 @@
package foo
class X {
val a: X = X()
val b: B = B()
}
@@ -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()
}
@@ -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()
}
@@ -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");