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 720bf2c3d2c..0a52fde9da6 100644 --- a/idea/src/org/jetbrains/kotlin/idea/refactoring/copy/CopyKotlinDeclarationsHandler.kt +++ b/idea/src/org/jetbrains/kotlin/idea/refactoring/copy/CopyKotlinDeclarationsHandler.kt @@ -257,18 +257,20 @@ class CopyKotlinDeclarationsHandler : CopyHandlerDelegateBase() { else -> null } - val targetFile: KtFile + val targetFile: PsiFile val copiedDeclaration: KtNamedDeclaration? if (fileToCopy != null) { targetFile = runWriteAction { - val copiedFile = targetDirectory.copyFileFrom(targetFileName, fileToCopy) as KtFile - if (fileToCopy.packageMatchesDirectory()) { + val copiedFile = targetDirectory.copyFileFrom(targetFileName, fileToCopy) + if (copiedFile is KtFile && fileToCopy.packageMatchesDirectory()) { targetDirectory.getPackage()?.qualifiedName?.let { copiedFile.packageFqName = FqName(it) } } performDelayedRefactoringRequests(project) copiedFile } - copiedDeclaration = if (isSingleDeclarationInFile) targetFile.declarations.singleOrNull() as? KtNamedDeclaration else null + copiedDeclaration = if (isSingleDeclarationInFile && targetFile is KtFile) { + targetFile.declarations.singleOrNull() as? KtNamedDeclaration + } else null } else { targetFile = getOrCreateTargetFile(originalFile, targetDirectory, targetFileName, commandName) ?: return@executeCommand diff --git a/idea/testData/refactoring/copy/copyKtFileToTextFile/after/foo/test.kt b/idea/testData/refactoring/copy/copyKtFileToTextFile/after/foo/test.kt new file mode 100644 index 00000000000..6cd7df7764e --- /dev/null +++ b/idea/testData/refactoring/copy/copyKtFileToTextFile/after/foo/test.kt @@ -0,0 +1,8 @@ +package foo + +val a = 42 + +// comment + +// some other comment +val s = "" \ No newline at end of file diff --git a/idea/testData/refactoring/copy/copyKtFileToTextFile/after/foo/test.kt.txt b/idea/testData/refactoring/copy/copyKtFileToTextFile/after/foo/test.kt.txt new file mode 100644 index 00000000000..6cd7df7764e --- /dev/null +++ b/idea/testData/refactoring/copy/copyKtFileToTextFile/after/foo/test.kt.txt @@ -0,0 +1,8 @@ +package foo + +val a = 42 + +// comment + +// some other comment +val s = "" \ No newline at end of file diff --git a/idea/testData/refactoring/copy/copyKtFileToTextFile/before/foo/test.kt b/idea/testData/refactoring/copy/copyKtFileToTextFile/before/foo/test.kt new file mode 100644 index 00000000000..6cd7df7764e --- /dev/null +++ b/idea/testData/refactoring/copy/copyKtFileToTextFile/before/foo/test.kt @@ -0,0 +1,8 @@ +package foo + +val a = 42 + +// comment + +// some other comment +val s = "" \ No newline at end of file diff --git a/idea/testData/refactoring/copy/copyKtFileToTextFile/copyKtFileToTextFile.test b/idea/testData/refactoring/copy/copyKtFileToTextFile/copyKtFileToTextFile.test new file mode 100644 index 00000000000..a23f3da9142 --- /dev/null +++ b/idea/testData/refactoring/copy/copyKtFileToTextFile/copyKtFileToTextFile.test @@ -0,0 +1,5 @@ +{ + "mainFile": "foo/test.kt", + "targetPackage": "foo", + "newName": "test.kt.txt" +} 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 227e26002d8..106ce5603a9 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/refactoring/copy/CopyTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/refactoring/copy/CopyTestGenerated.java @@ -91,6 +91,12 @@ public class CopyTestGenerated extends AbstractCopyTest { doTest(fileName); } + @TestMetadata("copyKtFileToTextFile/copyKtFileToTextFile.test") + public void testCopyKtFileToTextFile_CopyKtFileToTextFile() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/copy/copyKtFileToTextFile/copyKtFileToTextFile.test"); + doTest(fileName); + } + @TestMetadata("copyLocalClass/copyLocalClass.test") public void testCopyLocalClass_CopyLocalClass() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/copy/copyLocalClass/copyLocalClass.test");