diff --git a/idea/src/org/jetbrains/kotlin/idea/refactoring/move/moveDeclarations/MoveFilesWithDeclarationsProcessor.kt b/idea/src/org/jetbrains/kotlin/idea/refactoring/move/moveDeclarations/MoveFilesWithDeclarationsProcessor.kt index 6e3873ae206..423b9c38a6a 100644 --- a/idea/src/org/jetbrains/kotlin/idea/refactoring/move/moveDeclarations/MoveFilesWithDeclarationsProcessor.kt +++ b/idea/src/org/jetbrains/kotlin/idea/refactoring/move/moveDeclarations/MoveFilesWithDeclarationsProcessor.kt @@ -32,7 +32,7 @@ import org.jetbrains.kotlin.psi.KtFile class MoveFilesWithDeclarationsProcessor( project: Project, private val sourceFiles: List, - targetDirectory: PsiDirectory, + private val targetDirectory: PsiDirectory, private val targetFileName: String?, searchInComments: Boolean, searchInNonJavaFiles: Boolean, @@ -43,25 +43,8 @@ class MoveFilesWithDeclarationsProcessor( true, searchInComments, searchInNonJavaFiles, - MoveCallbackImpl(sourceFiles, targetFileName, moveCallback), + moveCallback, EmptyRunnable.INSTANCE) { - class MoveCallbackImpl( - private val sourceFiles: List, - private val targetFileName: String?, - private val nextCallback: MoveCallback? - ) : MoveCallback { - override fun refactoringCompleted() { - try { - if (targetFileName != null) { - sourceFiles.single().name = targetFileName - } - } - finally { - nextCallback?.refactoringCompleted() - } - } - } - override fun getCommandName(): String { return if (targetFileName != null) "Move " + sourceFiles.single().name else "Move" } @@ -80,21 +63,28 @@ class MoveFilesWithDeclarationsProcessor( // Assign a temporary name to file-under-move to avoid naming conflict during the refactoring private fun renameFileTemporarily() { - if (targetFileName != null) { - val sourceFile = sourceFiles.single() - //noinspection ConstantConditions - val temporaryName = UniqueNameGenerator.generateUniqueName( - "temp", - "", - ".kt", - sourceFile.containingDirectory!!.files.map { file -> file.name }) - sourceFile.name = temporaryName + if (targetFileName == null || targetDirectory.findFile(targetFileName) == null) return + + val sourceFile = sourceFiles.single() + val temporaryName = UniqueNameGenerator.generateUniqueName("temp", "", ".kt") { + sourceFile.containingDirectory!!.findFile(it) == null } + sourceFile.name = temporaryName } override fun performRefactoring(usages: Array) { - renameFileTemporarily() + val needTemporaryRename = targetFileName != null && targetDirectory.findFile(targetFileName) != null + if (needTemporaryRename) { + renameFileTemporarily() + } - super.performRefactoring(usages) + try { + super.performRefactoring(usages) + } + finally { + if (needTemporaryRename) { + sourceFiles.single().name = targetFileName!! + } + } } }