diff --git a/idea/src/org/jetbrains/kotlin/idea/refactoring/move/moveDeclarations/ui/KotlinSelectNestedClassRefactoringDialog.kt b/idea/src/org/jetbrains/kotlin/idea/refactoring/move/moveDeclarations/ui/KotlinSelectNestedClassRefactoringDialog.kt index 33715a17854..682b26dbb90 100644 --- a/idea/src/org/jetbrains/kotlin/idea/refactoring/move/moveDeclarations/ui/KotlinSelectNestedClassRefactoringDialog.kt +++ b/idea/src/org/jetbrains/kotlin/idea/refactoring/move/moveDeclarations/ui/KotlinSelectNestedClassRefactoringDialog.kt @@ -5,9 +5,9 @@ package org.jetbrains.kotlin.idea.refactoring.move.moveDeclarations.ui -import com.intellij.BundleBase import com.intellij.openapi.project.Project import com.intellij.openapi.ui.DialogWrapper +import com.intellij.psi.PsiDirectory import com.intellij.psi.PsiElement import com.intellij.refactoring.RefactoringBundle import com.intellij.refactoring.util.RadioUpDownListener @@ -99,10 +99,7 @@ internal class KotlinSelectNestedClassRefactoringDialog private constructor( fun chooseNestedClassRefactoring(nestedClass: KtClassOrObject, targetContainer: PsiElement?) { val project = nestedClass.project val dialog = when { - targetContainer != null && targetContainer !is KtClassOrObject || - nestedClass is KtClass && nestedClass.isInner() -> { - MoveKotlinNestedClassesToUpperLevelDialog(nestedClass, targetContainer) - } + targetContainer.isUpperLevelFor(nestedClass) -> MoveKotlinNestedClassesToUpperLevelDialog(nestedClass, targetContainer) nestedClass is KtEnumEntry -> return else -> { val selectionDialog = @@ -118,5 +115,9 @@ internal class KotlinSelectNestedClassRefactoringDialog private constructor( } dialog?.show() } + + private fun PsiElement?.isUpperLevelFor(nestedClass: KtClassOrObject) = + this != null && this !is KtClassOrObject && this !is PsiDirectory || + nestedClass is KtClass && nestedClass.isInner() } } \ No newline at end of file diff --git a/idea/src/org/jetbrains/kotlin/idea/refactoring/move/moveDeclarations/ui/MoveKotlinNestedClassesToUpperLevelModel.kt b/idea/src/org/jetbrains/kotlin/idea/refactoring/move/moveDeclarations/ui/MoveKotlinNestedClassesToUpperLevelModel.kt index ab07319253d..7562e42ba37 100644 --- a/idea/src/org/jetbrains/kotlin/idea/refactoring/move/moveDeclarations/ui/MoveKotlinNestedClassesToUpperLevelModel.kt +++ b/idea/src/org/jetbrains/kotlin/idea/refactoring/move/moveDeclarations/ui/MoveKotlinNestedClassesToUpperLevelModel.kt @@ -30,6 +30,7 @@ import org.jetbrains.kotlin.idea.refactoring.move.moveDeclarations.* import org.jetbrains.kotlin.idea.roots.getSuitableDestinationSourceRoots import org.jetbrains.kotlin.idea.statistics.MoveRefactoringFUSCollector.MovedEntity import org.jetbrains.kotlin.idea.statistics.MoveRefactoringFUSCollector.MoveRefactoringDestination +import org.jetbrains.kotlin.idea.util.projectStructure.module import org.jetbrains.kotlin.incremental.components.NoLookupLocation import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.psi.KtClassOrObject @@ -66,8 +67,8 @@ internal abstract class MoveKotlinNestedClassesToUpperLevelModel( val targetName = packageName if (oldPackageFqName?.asString() != targetName) { val projectRootManager = ProjectRootManager.getInstance(project) - - val contentSourceRoots = getSuitableDestinationSourceRoots(project) + val contentSourceRoots = target.module?.let { getSuitableDestinationSourceRoots(it) } + ?: getSuitableDestinationSourceRoots(project) val newPackage = PackageWrapper(PsiManager.getInstance(project), targetName) val targetSourceRoot: VirtualFile diff --git a/idea/src/org/jetbrains/kotlin/idea/roots/projectRootUtils.kt b/idea/src/org/jetbrains/kotlin/idea/roots/projectRootUtils.kt index 5eb63a27dd1..09b0c07790f 100644 --- a/idea/src/org/jetbrains/kotlin/idea/roots/projectRootUtils.kt +++ b/idea/src/org/jetbrains/kotlin/idea/roots/projectRootUtils.kt @@ -130,6 +130,12 @@ fun getSuitableDestinationSourceRoots(project: Project): List { return roots } +fun getSuitableDestinationSourceRoots(module: Module): MutableList { + val roots = ArrayList() + collectSuitableDestinationSourceRoots(module, roots) + return roots +} + fun collectSuitableDestinationSourceRoots(module: Module, result: MutableList) { for (entry in ModuleRootManager.getInstance(module).contentEntries) { for (sourceFolder in entry.getSourceFolders(KOTLIN_AWARE_SOURCE_ROOT_TYPES)) {