From 13319229635d2c9002ec45c3e217cc27e3c4e7d5 Mon Sep 17 00:00:00 2001 From: Alexey Sedunov Date: Wed, 8 Mar 2017 01:35:52 +0300 Subject: [PATCH] Move: Fix file rename of move Avoid temporary file rename if the current name doesn't conflict with other files in the target directory. Improve protection against exceptions during the refactoring which may prevent final rename --- .../MoveFilesWithDeclarationsProcessor.kt | 50 ++++++++----------- 1 file changed, 20 insertions(+), 30 deletions(-) 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!! + } + } } }