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 b78c078e3ab..caa8d13598f 100644 --- a/idea/src/org/jetbrains/kotlin/idea/refactoring/copy/CopyKotlinDeclarationsHandler.kt +++ b/idea/src/org/jetbrains/kotlin/idea/refactoring/copy/CopyKotlinDeclarationsHandler.kt @@ -24,11 +24,12 @@ 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.KotlinBundle import org.jetbrains.kotlin.idea.codeInsight.shorten.performDelayedRefactoringRequests import org.jetbrains.kotlin.idea.core.getFqNameWithImplicitPrefix import org.jetbrains.kotlin.idea.core.packageMatchesDirectoryOrImplicit +import org.jetbrains.kotlin.idea.core.quoteIfNeeded import org.jetbrains.kotlin.idea.core.util.toPsiDirectory -import org.jetbrains.kotlin.idea.KotlinBundle import org.jetbrains.kotlin.idea.refactoring.checkConflictsInteractively import org.jetbrains.kotlin.idea.refactoring.createKotlinFile import org.jetbrains.kotlin.idea.refactoring.move.* @@ -126,7 +127,8 @@ class CopyKotlinDeclarationsHandler : CopyHandlerDelegateBase() { targetFileName: String, targetDirectory: PsiDirectory ): ExistingFilePolicy { - val message = KotlinBundle.message("text.file.0.already.exists.in.1", + val message = KotlinBundle.message( + "text.file.0.already.exists.in.1", targetFileName, targetDirectory.virtualFile.path ) @@ -291,7 +293,7 @@ class CopyKotlinDeclarationsHandler : CopyHandlerDelegateBase() { if (targetKtFile !== null) { runWriteAction { if (!targetKtFile.packageMatchesDirectoryOrImplicit()) { - targetKtFile.containingDirectory?.getFqNameWithImplicitPrefix()?.let { targetDirectoryFqName -> + targetKtFile.containingDirectory?.getFqNameWithImplicitPrefix()?.quoteIfNeeded()?.let { targetDirectoryFqName -> targetKtFile.packageFqName = targetDirectoryFqName } } @@ -419,8 +421,9 @@ class CopyKotlinDeclarationsHandler : CopyHandlerDelegateBase() { val targetDirectoryFqName = targetDirectory.getFqNameWithImplicitPrefix() val copiedFile = targetDirectory.copyFileFrom(targetFileName, fileToCopy) if (copiedFile is KtFile && fileToCopy.packageMatchesDirectoryOrImplicit()) { - targetDirectoryFqName?.let { copiedFile.packageFqName = it } + targetDirectoryFqName?.quoteIfNeeded()?.let { copiedFile.packageFqName = it } } + performDelayedRefactoringRequests(sourceData.project) copiedFile } diff --git a/idea/testData/refactoring/copy/copyFileToRoot/after/Foo.kt b/idea/testData/refactoring/copy/copyFileToRoot/after/Foo.kt new file mode 100644 index 00000000000..923b9cdb6d0 --- /dev/null +++ b/idea/testData/refactoring/copy/copyFileToRoot/after/Foo.kt @@ -0,0 +1,3 @@ +package root.`20-20` + +class Foo \ No newline at end of file diff --git a/idea/testData/refactoring/copy/copyFileToRoot/after/Root.kt b/idea/testData/refactoring/copy/copyFileToRoot/after/Root.kt new file mode 100644 index 00000000000..90b4f7b8642 --- /dev/null +++ b/idea/testData/refactoring/copy/copyFileToRoot/after/Root.kt @@ -0,0 +1,3 @@ +package root.`20-20` + +class Root \ No newline at end of file diff --git a/idea/testData/refactoring/copy/copyFileToRoot/after/foo/Foo.kt b/idea/testData/refactoring/copy/copyFileToRoot/after/foo/Foo.kt new file mode 100644 index 00000000000..f4c5585faad --- /dev/null +++ b/idea/testData/refactoring/copy/copyFileToRoot/after/foo/Foo.kt @@ -0,0 +1,3 @@ +package foo + +class Foo \ No newline at end of file diff --git a/idea/testData/refactoring/copy/copyFileToRoot/before/Root.kt b/idea/testData/refactoring/copy/copyFileToRoot/before/Root.kt new file mode 100644 index 00000000000..90b4f7b8642 --- /dev/null +++ b/idea/testData/refactoring/copy/copyFileToRoot/before/Root.kt @@ -0,0 +1,3 @@ +package root.`20-20` + +class Root \ No newline at end of file diff --git a/idea/testData/refactoring/copy/copyFileToRoot/before/foo/Foo.kt b/idea/testData/refactoring/copy/copyFileToRoot/before/foo/Foo.kt new file mode 100644 index 00000000000..f4c5585faad --- /dev/null +++ b/idea/testData/refactoring/copy/copyFileToRoot/before/foo/Foo.kt @@ -0,0 +1,3 @@ +package foo + +class Foo \ No newline at end of file diff --git a/idea/testData/refactoring/copy/copyFileToRoot/copyFileToRoot.test b/idea/testData/refactoring/copy/copyFileToRoot/copyFileToRoot.test new file mode 100644 index 00000000000..d8cddb180c5 --- /dev/null +++ b/idea/testData/refactoring/copy/copyFileToRoot/copyFileToRoot.test @@ -0,0 +1,4 @@ +{ + "mainFile": "foo/Foo.kt", + "targetPackage": "" +} 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 46f6531d92a..2f0fa9490e1 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/refactoring/copy/CopyTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/refactoring/copy/CopyTestGenerated.java @@ -83,6 +83,11 @@ public class CopyTestGenerated extends AbstractCopyTest { runTest("idea/testData/refactoring/copy/copyFIleToDefaultPackage/copyFIleToDefaultPackage.test"); } + @TestMetadata("copyFileToRoot/copyFileToRoot.test") + public void testCopyFileToRoot_CopyFileToRoot() throws Exception { + runTest("idea/testData/refactoring/copy/copyFileToRoot/copyFileToRoot.test"); + } + @TestMetadata("copyFIleWithPackageAndDirUnmatched/copyFIleWithPackageAndDirUnmatched.test") public void testCopyFIleWithPackageAndDirUnmatched_CopyFIleWithPackageAndDirUnmatched() throws Exception { runTest("idea/testData/refactoring/copy/copyFIleWithPackageAndDirUnmatched/copyFIleWithPackageAndDirUnmatched.test");