Copy: Fix exception on copying KtFile to non-KtFile
#KT-22669 Fixed
This commit is contained in:
+6
-4
@@ -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
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
package foo
|
||||
|
||||
val a = 42
|
||||
|
||||
// comment
|
||||
|
||||
// some other comment
|
||||
val s = ""
|
||||
@@ -0,0 +1,8 @@
|
||||
package foo
|
||||
|
||||
val a = 42
|
||||
|
||||
// comment
|
||||
|
||||
// some other comment
|
||||
val s = ""
|
||||
@@ -0,0 +1,8 @@
|
||||
package foo
|
||||
|
||||
val a = 42
|
||||
|
||||
// comment
|
||||
|
||||
// some other comment
|
||||
val s = ""
|
||||
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"mainFile": "foo/test.kt",
|
||||
"targetPackage": "foo",
|
||||
"newName": "test.kt.txt"
|
||||
}
|
||||
@@ -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");
|
||||
|
||||
Reference in New Issue
Block a user