diff --git a/plugins/kotlin/refactorings/kotlin.refactorings.k2/src/org/jetbrains/kotlin/idea/k2/refactoring/copy/CopyKotlinDeclarationsHandler.kt b/plugins/kotlin/refactorings/kotlin.refactorings.k2/src/org/jetbrains/kotlin/idea/k2/refactoring/copy/CopyKotlinDeclarationsHandler.kt index a95a98fbd690..f43ab4abdc6c 100644 --- a/plugins/kotlin/refactorings/kotlin.refactorings.k2/src/org/jetbrains/kotlin/idea/k2/refactoring/copy/CopyKotlinDeclarationsHandler.kt +++ b/plugins/kotlin/refactorings/kotlin.refactorings.k2/src/org/jetbrains/kotlin/idea/k2/refactoring/copy/CopyKotlinDeclarationsHandler.kt @@ -193,7 +193,7 @@ class CopyKotlinDeclarationsHandler : AbstractCopyKotlinDeclarationsHandler() { try { val targetDirectory = runWriteAction { - targetData.targetDirWrapper.getOrCreateTarget() as PsiDirectory + targetData.targetDirWrapper.getOrCreateTarget(dirStructureMatchesPkg = true) as PsiDirectory } val targetFileName = diff --git a/plugins/kotlin/refactorings/kotlin.refactorings.move.k2/src/org/jetbrains/kotlin/idea/k2/refactoring/move/descriptor/K2MoveOperationDescriptor.kt b/plugins/kotlin/refactorings/kotlin.refactorings.move.k2/src/org/jetbrains/kotlin/idea/k2/refactoring/move/descriptor/K2MoveOperationDescriptor.kt index 0858832894f4..ba005cdcefb7 100644 --- a/plugins/kotlin/refactorings/kotlin.refactorings.move.k2/src/org/jetbrains/kotlin/idea/k2/refactoring/move/descriptor/K2MoveOperationDescriptor.kt +++ b/plugins/kotlin/refactorings/kotlin.refactorings.move.k2/src/org/jetbrains/kotlin/idea/k2/refactoring/move/descriptor/K2MoveOperationDescriptor.kt @@ -16,6 +16,7 @@ sealed class K2MoveOperationDescriptor( val searchForText: Boolean, val searchInComments: Boolean, val searchReferences: Boolean, + val dirStructureMatchesPkg: Boolean, val moveCallBack: MoveCallback? = null ) { init { @@ -32,6 +33,7 @@ sealed class K2MoveOperationDescriptor( searchForText: Boolean, searchInComments: Boolean, searchReferences: Boolean, + dirStructureMatchesPkg: Boolean, moveCallBack: MoveCallback? = null ) : K2MoveOperationDescriptor( project, @@ -39,6 +41,7 @@ sealed class K2MoveOperationDescriptor( searchForText, searchInComments, searchReferences, + dirStructureMatchesPkg, moveCallBack ) { override val sourceElements: List get() = moveDescriptors.flatMap { it.source.elements } @@ -54,6 +57,7 @@ sealed class K2MoveOperationDescriptor( searchForText: Boolean, searchInComments: Boolean, searchReferences: Boolean, + dirStructureMatchesPkg: Boolean, moveCallBack: MoveCallback? = null ) : K2MoveOperationDescriptor( project, @@ -61,6 +65,7 @@ sealed class K2MoveOperationDescriptor( searchForText, searchInComments, searchReferences, + dirStructureMatchesPkg, moveCallBack ) { override val sourceElements: List get() = moveDescriptors.flatMap { it.source.elements } diff --git a/plugins/kotlin/refactorings/kotlin.refactorings.move.k2/src/org/jetbrains/kotlin/idea/k2/refactoring/move/descriptor/K2MoveTargetDescriptor.kt b/plugins/kotlin/refactorings/kotlin.refactorings.move.k2/src/org/jetbrains/kotlin/idea/k2/refactoring/move/descriptor/K2MoveTargetDescriptor.kt index a927036825e4..7f75a90fbbcd 100644 --- a/plugins/kotlin/refactorings/kotlin.refactorings.move.k2/src/org/jetbrains/kotlin/idea/k2/refactoring/move/descriptor/K2MoveTargetDescriptor.kt +++ b/plugins/kotlin/refactorings/kotlin.refactorings.move.k2/src/org/jetbrains/kotlin/idea/k2/refactoring/move/descriptor/K2MoveTargetDescriptor.kt @@ -21,35 +21,19 @@ sealed interface K2MoveTargetDescriptor { val pkgName: FqName + /** - * Gets or creates the target location, like a file or directory. It might be the case that the target directory or target file doesn't - * exist yet. In this case this method will create the file or directory based on the [baseDirectory] and [pkgName]. The creation of - * the target is thus package aware, example: - * ``` - * src/ - * Foo.kt <--- root pkg - * a/Bar.kt - * ``` - * If we move `Bar.kt` to `src/b` and change the pkg to `b``the [baseDirectory] will be `src` but because the package is `b` we will - * and `Foo.kt` is in the root package we will create `src/b`. - * - * This also works when the project structure doesn't match the directory structure: - * ``` - * src/ - * Foo.kt <--- package c.d - * a/Bar.kt - * ``` - * If we move `Bar.kt` to `src/b` and change the pkg to `c.d.b``the [baseDirectory] will be `src` but because the package is `c.d.b` - * and `Foo.kt` has package c.d we won't create directory `src/c/d/b` but instead create src/d. + * Gets or creates the target */ @RequiresWriteLock - fun getOrCreateTarget(): PsiFileSystemItem + fun getOrCreateTarget(dirStructureMatchesPkg: Boolean): PsiFileSystemItem open class Directory( override val pkgName: FqName, override val baseDirectory: PsiDirectory ) : K2MoveTargetDescriptor { - override fun getOrCreateTarget(): PsiFileSystemItem { + override fun getOrCreateTarget(dirStructureMatchesPkg: Boolean): PsiFileSystemItem { + if (!dirStructureMatchesPkg) return baseDirectory val implicitPkgPrefix = baseDirectory.getFqNameWithImplicitPrefixOrRoot() val pkgSuffix = pkgName.asString().removePrefix(implicitPkgPrefix.asString()).removePrefix(".") val file = VfsUtilCore.findRelativeFile(pkgSuffix.replace('.', java.io.File.separatorChar), baseDirectory.virtualFile) @@ -63,8 +47,8 @@ sealed interface K2MoveTargetDescriptor { pkgName: FqName, baseDirectory: PsiDirectory ) : Directory(pkgName, baseDirectory) { - override fun getOrCreateTarget(): KtFile { - val directory = super.getOrCreateTarget() as PsiDirectory + override fun getOrCreateTarget(dirStructureMatchesPkg: Boolean): KtFile { + val directory = super.getOrCreateTarget(dirStructureMatchesPkg) as PsiDirectory return getOrCreateKotlinFile(fileName, directory, pkgName.asString()) } } diff --git a/plugins/kotlin/refactorings/kotlin.refactorings.move.k2/src/org/jetbrains/kotlin/idea/k2/refactoring/move/processor/K2MoveDeclarationsRefactoringProcessor.kt b/plugins/kotlin/refactorings/kotlin.refactorings.move.k2/src/org/jetbrains/kotlin/idea/k2/refactoring/move/processor/K2MoveDeclarationsRefactoringProcessor.kt index 808b3015536c..b2a69497747c 100644 --- a/plugins/kotlin/refactorings/kotlin.refactorings.move.k2/src/org/jetbrains/kotlin/idea/k2/refactoring/move/processor/K2MoveDeclarationsRefactoringProcessor.kt +++ b/plugins/kotlin/refactorings/kotlin.refactorings.move.k2/src/org/jetbrains/kotlin/idea/k2/refactoring/move/processor/K2MoveDeclarationsRefactoringProcessor.kt @@ -70,7 +70,7 @@ class K2MoveDeclarationsRefactoringProcessor( allowAnalysisOnEdt { operationDescriptor.moveDescriptors.forEach { moveDescriptor -> val elementsToMove = moveDescriptor.source.elements - val targetFile = moveDescriptor.target.getOrCreateTarget() + val targetFile = moveDescriptor.target.getOrCreateTarget(operationDescriptor.dirStructureMatchesPkg) val sourceFiles = elementsToMove.map { it.containingKtFile }.distinct() val oldToNewMap = elementsToMove.moveInto(targetFile) moveDescriptor.source.elements.forEach(PsiElement::deleteSingle) diff --git a/plugins/kotlin/refactorings/kotlin.refactorings.move.k2/src/org/jetbrains/kotlin/idea/k2/refactoring/move/processor/K2MoveFilesOrDirectoriesRefactoringProcessor.kt b/plugins/kotlin/refactorings/kotlin.refactorings.move.k2/src/org/jetbrains/kotlin/idea/k2/refactoring/move/processor/K2MoveFilesOrDirectoriesRefactoringProcessor.kt index 2f1b4196446b..efd4efa7728e 100644 --- a/plugins/kotlin/refactorings/kotlin.refactorings.move.k2/src/org/jetbrains/kotlin/idea/k2/refactoring/move/processor/K2MoveFilesOrDirectoriesRefactoringProcessor.kt +++ b/plugins/kotlin/refactorings/kotlin.refactorings.move.k2/src/org/jetbrains/kotlin/idea/k2/refactoring/move/processor/K2MoveFilesOrDirectoriesRefactoringProcessor.kt @@ -26,7 +26,7 @@ import org.jetbrains.kotlin.psi.KtNamedDeclaration class K2MoveFilesOrDirectoriesRefactoringProcessor(descriptor: K2MoveOperationDescriptor.Files) : MoveFilesOrDirectoriesProcessor( descriptor.project, descriptor.sourceElements.toTypedArray(), - runWriteAction { descriptor.moveDescriptors.first().target.getOrCreateTarget() as PsiDirectory }, // TODO how to do multi target move? + runWriteAction { descriptor.moveDescriptors.first().target.getOrCreateTarget(descriptor.dirStructureMatchesPkg) as PsiDirectory }, // TODO how to do multi target move? descriptor.searchReferences, descriptor.searchInComments, descriptor.searchForText, diff --git a/plugins/kotlin/refactorings/kotlin.refactorings.move.k2/src/org/jetbrains/kotlin/idea/k2/refactoring/move/ui/K2MoveModel.kt b/plugins/kotlin/refactorings/kotlin.refactorings.move.k2/src/org/jetbrains/kotlin/idea/k2/refactoring/move/ui/K2MoveModel.kt index d8a39c358fbf..cd0919fd3a2d 100644 --- a/plugins/kotlin/refactorings/kotlin.refactorings.move.k2/src/org/jetbrains/kotlin/idea/k2/refactoring/move/ui/K2MoveModel.kt +++ b/plugins/kotlin/refactorings/kotlin.refactorings.move.k2/src/org/jetbrains/kotlin/idea/k2/refactoring/move/ui/K2MoveModel.kt @@ -143,6 +143,7 @@ sealed class K2MoveModel { searchForText.state, searchReferences, searchInComments.state, + dirStructureMatchesPkg = true, moveCallBack ) return operationDescriptor @@ -174,6 +175,7 @@ sealed class K2MoveModel { searchForText.state, searchReferences, searchInComments.state, + dirStructureMatchesPkg = true, moveCallBack ) } diff --git a/plugins/kotlin/refactorings/kotlin.refactorings.move.k2/test/org/jetbrains/kotlin/idea/k2/refactoring/move/AbstractK2MoveFileOrDirectoriesTest.kt b/plugins/kotlin/refactorings/kotlin.refactorings.move.k2/test/org/jetbrains/kotlin/idea/k2/refactoring/move/AbstractK2MoveFileOrDirectoriesTest.kt index 5fb46be828ea..356aba9b4db8 100644 --- a/plugins/kotlin/refactorings/kotlin.refactorings.move.k2/test/org/jetbrains/kotlin/idea/k2/refactoring/move/AbstractK2MoveFileOrDirectoriesTest.kt +++ b/plugins/kotlin/refactorings/kotlin.refactorings.move.k2/test/org/jetbrains/kotlin/idea/k2/refactoring/move/AbstractK2MoveFileOrDirectoriesTest.kt @@ -79,7 +79,8 @@ internal object K2MoveFileOrDirectoriesRefactoringAction : KotlinMoveRefactoring listOf(moveDescriptor), shouldUpdateReferences(config, sourceDescriptor.elements.first(), targetDescriptor.baseDirectory), config.searchInComments(), - config.searchReferences() + config.searchReferences(), + dirStructureMatchesPkg = true ) K2MoveFilesOrDirectoriesRefactoringProcessor(moveOperationDescriptor).run() } diff --git a/plugins/kotlin/refactorings/kotlin.refactorings.move.k2/test/org/jetbrains/kotlin/idea/k2/refactoring/move/AbstractK2MoveTopLevelTest.kt b/plugins/kotlin/refactorings/kotlin.refactorings.move.k2/test/org/jetbrains/kotlin/idea/k2/refactoring/move/AbstractK2MoveTopLevelTest.kt index 699003ccff89..9a39552b9f5b 100644 --- a/plugins/kotlin/refactorings/kotlin.refactorings.move.k2/test/org/jetbrains/kotlin/idea/k2/refactoring/move/AbstractK2MoveTopLevelTest.kt +++ b/plugins/kotlin/refactorings/kotlin.refactorings.move.k2/test/org/jetbrains/kotlin/idea/k2/refactoring/move/AbstractK2MoveTopLevelTest.kt @@ -73,6 +73,7 @@ internal object K2MoveTopLevelRefactoringAction : KotlinMoveRefactoringAction { config.searchForText(), config.searchInComments(), config.searchReferences(), + dirStructureMatchesPkg = true ) K2MoveDeclarationsRefactoringProcessor(moveOperationDescriptor).run() }