From 9219f2214e852c08a047b524a3a48867acd15ece Mon Sep 17 00:00:00 2001 From: Alexey Sedunov Date: Fri, 24 Jul 2015 13:49:10 +0300 Subject: [PATCH] Move: Fix ClassCastException on attempt to move several classes (located in different files) to the same file #KT-8437 Fixed --- .../MoveKotlinTopLevelDeclarationsHandler.kt | 20 +++++++++---------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/idea/src/org/jetbrains/kotlin/idea/refactoring/move/moveTopLevelDeclarations/MoveKotlinTopLevelDeclarationsHandler.kt b/idea/src/org/jetbrains/kotlin/idea/refactoring/move/moveTopLevelDeclarations/MoveKotlinTopLevelDeclarationsHandler.kt index 2085536ecfc..6c1b76e3c23 100644 --- a/idea/src/org/jetbrains/kotlin/idea/refactoring/move/moveTopLevelDeclarations/MoveKotlinTopLevelDeclarationsHandler.kt +++ b/idea/src/org/jetbrains/kotlin/idea/refactoring/move/moveTopLevelDeclarations/MoveKotlinTopLevelDeclarationsHandler.kt @@ -20,10 +20,7 @@ import com.intellij.openapi.actionSystem.DataContext import com.intellij.openapi.actionSystem.LangDataKeys import com.intellij.openapi.editor.Editor import com.intellij.openapi.project.Project -import com.intellij.psi.PsiDirectory -import com.intellij.psi.PsiElement -import com.intellij.psi.PsiPackage -import com.intellij.psi.PsiReference +import com.intellij.psi.* import com.intellij.refactoring.JavaRefactoringSettings import com.intellij.refactoring.RefactoringBundle import com.intellij.refactoring.move.MoveCallback @@ -52,12 +49,10 @@ public class MoveKotlinTopLevelDeclarationsHandler : MoveHandlerDelegate() { val sourceFiles = elementsToSearch.mapTo(LinkedHashSet()) { it.getContainingJetFile() } val sourceFile = sourceFiles.singleOrNull() if (sourceFile == null) { - if (sourceFiles.all { it.getDeclarations().size() == 1 }) { - moveFilesOrDirectories(project, sourceFiles.toTypedArray(), targetContainer) - return true - } - - CommonRefactoringUtil.showErrorMessage(RefactoringBundle.message("move.title"), "All declarations must belong to the same file", null, project) + CommonRefactoringUtil.showErrorMessage(RefactoringBundle.message("move.title"), + "All declarations must belong to the same file", + null, + project) return false } @@ -76,7 +71,10 @@ public class MoveKotlinTopLevelDeclarationsHandler : MoveHandlerDelegate() { } override fun canMove(elements: Array, targetContainer: PsiElement?): Boolean { - return super.canMove(elements, targetContainer) && elements.all { e -> + if (!super.canMove(elements, targetContainer)) return false + if (elements.map { it.getContainingFile() }.distinct().size() != 1) return false + + return elements.all { e -> if (e is JetClass || (e is JetObjectDeclaration && !e.isObjectLiteral()) || e is JetNamedFunction || e is JetProperty) { val parent = e.getParent() parent is JetFile && parent.isInJavaSourceRoot()