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
This commit is contained in:
+20
-30
@@ -32,7 +32,7 @@ import org.jetbrains.kotlin.psi.KtFile
|
||||
class MoveFilesWithDeclarationsProcessor(
|
||||
project: Project,
|
||||
private val sourceFiles: List<KtFile>,
|
||||
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<KtFile>,
|
||||
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<UsageInfo>) {
|
||||
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!!
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user