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(
|
class MoveFilesWithDeclarationsProcessor(
|
||||||
project: Project,
|
project: Project,
|
||||||
private val sourceFiles: List<KtFile>,
|
private val sourceFiles: List<KtFile>,
|
||||||
targetDirectory: PsiDirectory,
|
private val targetDirectory: PsiDirectory,
|
||||||
private val targetFileName: String?,
|
private val targetFileName: String?,
|
||||||
searchInComments: Boolean,
|
searchInComments: Boolean,
|
||||||
searchInNonJavaFiles: Boolean,
|
searchInNonJavaFiles: Boolean,
|
||||||
@@ -43,25 +43,8 @@ class MoveFilesWithDeclarationsProcessor(
|
|||||||
true,
|
true,
|
||||||
searchInComments,
|
searchInComments,
|
||||||
searchInNonJavaFiles,
|
searchInNonJavaFiles,
|
||||||
MoveCallbackImpl(sourceFiles, targetFileName, moveCallback),
|
moveCallback,
|
||||||
EmptyRunnable.INSTANCE) {
|
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 {
|
override fun getCommandName(): String {
|
||||||
return if (targetFileName != null) "Move " + sourceFiles.single().name else "Move"
|
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
|
// Assign a temporary name to file-under-move to avoid naming conflict during the refactoring
|
||||||
private fun renameFileTemporarily() {
|
private fun renameFileTemporarily() {
|
||||||
if (targetFileName != null) {
|
if (targetFileName == null || targetDirectory.findFile(targetFileName) == null) return
|
||||||
val sourceFile = sourceFiles.single()
|
|
||||||
//noinspection ConstantConditions
|
val sourceFile = sourceFiles.single()
|
||||||
val temporaryName = UniqueNameGenerator.generateUniqueName(
|
val temporaryName = UniqueNameGenerator.generateUniqueName("temp", "", ".kt") {
|
||||||
"temp",
|
sourceFile.containingDirectory!!.findFile(it) == null
|
||||||
"",
|
|
||||||
".kt",
|
|
||||||
sourceFile.containingDirectory!!.files.map { file -> file.name })
|
|
||||||
sourceFile.name = temporaryName
|
|
||||||
}
|
}
|
||||||
|
sourceFile.name = temporaryName
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun performRefactoring(usages: Array<UsageInfo>) {
|
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