From 2f88b6ef9bf1b8d71ec297059a43ddc9a4358779 Mon Sep 17 00:00:00 2001 From: Alexey Sedunov Date: Tue, 23 May 2017 14:32:32 +0300 Subject: [PATCH] Copy: Rename only self-usages of the copied class #KT-18034 Fixed --- .../copy/CopyKotlinDeclarationsHandler.kt | 13 +++++++++---- .../copyClassToSamePackageWithRename/after/foo/X.kt | 6 ++++++ .../after/foo/test.kt | 11 +++++++++++ .../before/foo/test.kt | 11 +++++++++++ .../copyClassToSamePackageWithRename.test | 5 +++++ .../idea/refactoring/copy/CopyTestGenerated.java | 6 ++++++ 6 files changed, 48 insertions(+), 4 deletions(-) create mode 100644 idea/testData/refactoring/copy/copyClassToSamePackageWithRename/after/foo/X.kt create mode 100644 idea/testData/refactoring/copy/copyClassToSamePackageWithRename/after/foo/test.kt create mode 100644 idea/testData/refactoring/copy/copyClassToSamePackageWithRename/before/foo/test.kt create mode 100644 idea/testData/refactoring/copy/copyClassToSamePackageWithRename/copyClassToSamePackageWithRename.test diff --git a/idea/src/org/jetbrains/kotlin/idea/refactoring/copy/CopyKotlinDeclarationsHandler.kt b/idea/src/org/jetbrains/kotlin/idea/refactoring/copy/CopyKotlinDeclarationsHandler.kt index 9ea11a45295..3f3860af8f7 100644 --- a/idea/src/org/jetbrains/kotlin/idea/refactoring/copy/CopyKotlinDeclarationsHandler.kt +++ b/idea/src/org/jetbrains/kotlin/idea/refactoring/copy/CopyKotlinDeclarationsHandler.kt @@ -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) { diff --git a/idea/testData/refactoring/copy/copyClassToSamePackageWithRename/after/foo/X.kt b/idea/testData/refactoring/copy/copyClassToSamePackageWithRename/after/foo/X.kt new file mode 100644 index 00000000000..98d88b73b1f --- /dev/null +++ b/idea/testData/refactoring/copy/copyClassToSamePackageWithRename/after/foo/X.kt @@ -0,0 +1,6 @@ +package foo + +class X { + val a: X = X() + val b: B = B() +} \ No newline at end of file diff --git a/idea/testData/refactoring/copy/copyClassToSamePackageWithRename/after/foo/test.kt b/idea/testData/refactoring/copy/copyClassToSamePackageWithRename/after/foo/test.kt new file mode 100644 index 00000000000..c6688a1d8c2 --- /dev/null +++ b/idea/testData/refactoring/copy/copyClassToSamePackageWithRename/after/foo/test.kt @@ -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() +} diff --git a/idea/testData/refactoring/copy/copyClassToSamePackageWithRename/before/foo/test.kt b/idea/testData/refactoring/copy/copyClassToSamePackageWithRename/before/foo/test.kt new file mode 100644 index 00000000000..52166ff2636 --- /dev/null +++ b/idea/testData/refactoring/copy/copyClassToSamePackageWithRename/before/foo/test.kt @@ -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() +} diff --git a/idea/testData/refactoring/copy/copyClassToSamePackageWithRename/copyClassToSamePackageWithRename.test b/idea/testData/refactoring/copy/copyClassToSamePackageWithRename/copyClassToSamePackageWithRename.test new file mode 100644 index 00000000000..925cde84532 --- /dev/null +++ b/idea/testData/refactoring/copy/copyClassToSamePackageWithRename/copyClassToSamePackageWithRename.test @@ -0,0 +1,5 @@ +{ + "mainFile": "foo/test.kt", + "targetPackage": "foo", + "newName": "X" +} diff --git a/idea/tests/org/jetbrains/kotlin/idea/refactoring/copy/CopyTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/refactoring/copy/CopyTestGenerated.java index 5470a54df17..7b56d34859f 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/refactoring/copy/CopyTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/refactoring/copy/CopyTestGenerated.java @@ -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");