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 c249282514f..40905768eef 100644 --- a/idea/src/org/jetbrains/kotlin/idea/refactoring/copy/CopyKotlinDeclarationsHandler.kt +++ b/idea/src/org/jetbrains/kotlin/idea/refactoring/copy/CopyKotlinDeclarationsHandler.kt @@ -187,6 +187,8 @@ class CopyKotlinDeclarationsHandler : CopyHandlerDelegateBase() { val originalFile = elementsToCopy.first().containingFile as KtFile val initialTargetDirectory = defaultTargetDirectory ?: originalFile.containingDirectory ?: return + val isSingleDeclarationInFile = singleElementToCopy is KtNamedDeclaration && originalFile.declarations.singleOrNull() == singleElementToCopy + val project = initialTargetDirectory.project val commandName = "Copy Declarations" @@ -249,16 +251,24 @@ class CopyKotlinDeclarationsHandler : CopyHandlerDelegateBase() { val oldToNewElementsMapping = HashMap() + val fileToCopy = when { + singleElementToCopy is KtFile -> singleElementToCopy + isSingleDeclarationInFile -> originalFile + else -> null + } + val targetFile: KtFile - if (singleElementToCopy is KtFile) { + val copiedDeclaration: KtNamedDeclaration? + if (fileToCopy != null) { targetFile = runWriteAction { - val copiedFile = targetDirectory.copyFileFrom(targetFileName, singleElementToCopy) as KtFile - if (singleElementToCopy.packageMatchesDirectory()) { + val copiedFile = targetDirectory.copyFileFrom(targetFileName, fileToCopy) as KtFile + if (fileToCopy.packageMatchesDirectory()) { targetDirectory.getPackage()?.qualifiedName?.let { copiedFile.packageFqName = FqName(it) } } performDelayedRefactoringRequests(project) copiedFile } + copiedDeclaration = if (isSingleDeclarationInFile) targetFile.declarations.singleOrNull() as? KtNamedDeclaration else null } else { targetFile = getOrCreateTargetFile(originalFile, targetDirectory, targetFileName, commandName) ?: return@executeCommand @@ -274,9 +284,10 @@ class CopyKotlinDeclarationsHandler : CopyHandlerDelegateBase() { performDelayedRefactoringRequests(project) } + copiedDeclaration = oldToNewElementsMapping.values.filterIsInstance().singleOrNull() } - (oldToNewElementsMapping.values.filterIsInstance().singleOrNull())?.let { newDeclaration -> + copiedDeclaration?.let { newDeclaration -> if (newName == newDeclaration.name) return@let val selfReferences = ReferencesSearch.search(newDeclaration, LocalSearchScope(newDeclaration)).findAll() runWriteAction { diff --git a/idea/testData/refactoring/copy/copySingleClass/after/bar/A.kt b/idea/testData/refactoring/copy/copySingleClass/after/bar/A.kt new file mode 100644 index 00000000000..39048f89611 --- /dev/null +++ b/idea/testData/refactoring/copy/copySingleClass/after/bar/A.kt @@ -0,0 +1,7 @@ +package bar + +// test + +class A + +// test2 \ No newline at end of file diff --git a/idea/testData/refactoring/copy/copySingleClass/after/foo/A.kt b/idea/testData/refactoring/copy/copySingleClass/after/foo/A.kt new file mode 100644 index 00000000000..94311a5557d --- /dev/null +++ b/idea/testData/refactoring/copy/copySingleClass/after/foo/A.kt @@ -0,0 +1,7 @@ +package foo + +// test + +class A + +// test2 \ No newline at end of file diff --git a/idea/testData/refactoring/copy/copySingleClass/before/foo/A.kt b/idea/testData/refactoring/copy/copySingleClass/before/foo/A.kt new file mode 100644 index 00000000000..c7a0543a544 --- /dev/null +++ b/idea/testData/refactoring/copy/copySingleClass/before/foo/A.kt @@ -0,0 +1,7 @@ +package foo + +// test + +class A + +// test2 \ No newline at end of file diff --git a/idea/testData/refactoring/copy/copySingleClass/copySingleClass.test b/idea/testData/refactoring/copy/copySingleClass/copySingleClass.test new file mode 100644 index 00000000000..7b0c3015980 --- /dev/null +++ b/idea/testData/refactoring/copy/copySingleClass/copySingleClass.test @@ -0,0 +1,4 @@ +{ + "mainFile": "foo/A.kt", + "targetPackage": "bar" +} diff --git a/idea/testData/refactoring/copy/copySingleClassWithRename/after/bar/B.kt b/idea/testData/refactoring/copy/copySingleClassWithRename/after/bar/B.kt new file mode 100644 index 00000000000..ad03099f823 --- /dev/null +++ b/idea/testData/refactoring/copy/copySingleClassWithRename/after/bar/B.kt @@ -0,0 +1,7 @@ +package bar + +// test + +class B + +// test2 \ No newline at end of file diff --git a/idea/testData/refactoring/copy/copySingleClassWithRename/after/foo/A.kt b/idea/testData/refactoring/copy/copySingleClassWithRename/after/foo/A.kt new file mode 100644 index 00000000000..94311a5557d --- /dev/null +++ b/idea/testData/refactoring/copy/copySingleClassWithRename/after/foo/A.kt @@ -0,0 +1,7 @@ +package foo + +// test + +class A + +// test2 \ No newline at end of file diff --git a/idea/testData/refactoring/copy/copySingleClassWithRename/before/foo/A.kt b/idea/testData/refactoring/copy/copySingleClassWithRename/before/foo/A.kt new file mode 100644 index 00000000000..c7a0543a544 --- /dev/null +++ b/idea/testData/refactoring/copy/copySingleClassWithRename/before/foo/A.kt @@ -0,0 +1,7 @@ +package foo + +// test + +class A + +// test2 \ No newline at end of file diff --git a/idea/testData/refactoring/copy/copySingleClassWithRename/copySingleClassWithRename.test b/idea/testData/refactoring/copy/copySingleClassWithRename/copySingleClassWithRename.test new file mode 100644 index 00000000000..2c400988286 --- /dev/null +++ b/idea/testData/refactoring/copy/copySingleClassWithRename/copySingleClassWithRename.test @@ -0,0 +1,5 @@ +{ + "mainFile": "foo/A.kt", + "targetPackage": "bar", + "newName": "B" +} diff --git a/idea/testData/refactoring/copy/refToImportJavaStaticField/after/foo/test.kt b/idea/testData/refactoring/copy/refToImportJavaStaticField/after/foo/test.kt index 4e4fb32b49d..3b791a97bfa 100644 --- a/idea/testData/refactoring/copy/refToImportJavaStaticField/after/foo/test.kt +++ b/idea/testData/refactoring/copy/refToImportJavaStaticField/after/foo/test.kt @@ -5,3 +5,7 @@ import foo.J.JJJ fun test() { val x = JJJ } + +fun dummy() { + +} \ No newline at end of file diff --git a/idea/testData/refactoring/copy/refToImportJavaStaticField/before/foo/test.kt b/idea/testData/refactoring/copy/refToImportJavaStaticField/before/foo/test.kt index cbff5af4098..0759147e69e 100644 --- a/idea/testData/refactoring/copy/refToImportJavaStaticField/before/foo/test.kt +++ b/idea/testData/refactoring/copy/refToImportJavaStaticField/before/foo/test.kt @@ -5,3 +5,7 @@ import foo.J.JJJ fun test() { val x = JJJ } + +fun dummy() { + +} \ No newline at end of file diff --git a/idea/testData/refactoring/copy/refToImportJavaStaticMethod/after/foo/test.kt b/idea/testData/refactoring/copy/refToImportJavaStaticMethod/after/foo/test.kt index 708e4f52db0..8b92b427f04 100644 --- a/idea/testData/refactoring/copy/refToImportJavaStaticMethod/after/foo/test.kt +++ b/idea/testData/refactoring/copy/refToImportJavaStaticMethod/after/foo/test.kt @@ -5,3 +5,7 @@ import foo.J.jjj fun test() { jjj() } + +fun dummy() { + +} \ No newline at end of file diff --git a/idea/testData/refactoring/copy/refToImportJavaStaticMethod/before/foo/test.kt b/idea/testData/refactoring/copy/refToImportJavaStaticMethod/before/foo/test.kt index 5f4bcff8b61..d4562db4a78 100644 --- a/idea/testData/refactoring/copy/refToImportJavaStaticMethod/before/foo/test.kt +++ b/idea/testData/refactoring/copy/refToImportJavaStaticMethod/before/foo/test.kt @@ -5,3 +5,7 @@ import foo.J.jjj fun test() { jjj() } + +fun dummy() { + +} \ No newline at end of file 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 778095c8a00..dd94179ebe9 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/refactoring/copy/CopyTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/refactoring/copy/CopyTestGenerated.java @@ -168,12 +168,24 @@ public class CopyTestGenerated extends AbstractCopyTest { doTest(fileName); } + @TestMetadata("copySingleClass/copySingleClass.test") + public void testCopySingleClass_CopySingleClass() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/copy/copySingleClass/copySingleClass.test"); + doTest(fileName); + } + @TestMetadata("copySingleClassFile/copySingleClassFile.test") public void testCopySingleClassFile_CopySingleClassFile() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/copy/copySingleClassFile/copySingleClassFile.test"); doTest(fileName); } + @TestMetadata("copySingleClassWithRename/copySingleClassWithRename.test") + public void testCopySingleClassWithRename_CopySingleClassWithRename() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/copy/copySingleClassWithRename/copySingleClassWithRename.test"); + doTest(fileName); + } + @TestMetadata("copyTopLevelFunction/copyTopLevelFunction.test") public void testCopyTopLevelFunction_CopyTopLevelFunction() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/copy/copyTopLevelFunction/copyTopLevelFunction.test");