Minor fixes of move refactoring

Fix NPE for case when target path for package is not exists
Clarify case when target path for file move does not exists
Fix NPE when file selector shows with empty edit box of file name in Move dialog
This commit is contained in:
Igor Yakovlev
2019-12-24 21:25:38 +03:00
parent a2db1f8314
commit 4afbbda858
2 changed files with 23 additions and 13 deletions
@@ -272,15 +272,21 @@ public class MoveKotlinTopLevelDeclarationsDialog extends RefactoringDialog {
File targetFile1 = new File(fileChooser.getText());
PsiFile targetPsiFile = PhysicalFileSystemUtilsKt.toPsiFile(targetFile1, myProject);
if (targetPsiFile instanceof KtFile) {
dialog.select((KtFile) targetPsiFile);
}
else {
PsiDirectory targetDir = PhysicalFileSystemUtilsKt.toPsiDirectory(targetFile1.getParentFile(), myProject);
if (targetDir == null) {
targetDir = sourceDir;
if (targetPsiFile != null) {
if (targetPsiFile instanceof KtFile) {
dialog.select((KtFile) targetPsiFile);
}
dialog.selectDirectory(targetDir);
else {
PsiDirectory targetDir = PhysicalFileSystemUtilsKt.toPsiDirectory(targetFile1.getParentFile(), myProject);
if (targetDir != null) {
dialog.selectDirectory(targetDir);
} else {
dialog.selectDirectory(sourceDir);
}
}
} else {
dialog.selectDirectory(sourceDir);
}
dialog.showDialog();
@@ -18,7 +18,6 @@ import com.intellij.refactoring.move.MoveCallback
import com.intellij.refactoring.move.moveClassesOrPackages.AutocreatingSingleSourceRootMoveDestination
import com.intellij.refactoring.move.moveClassesOrPackages.MultipleRootsMoveDestination
import com.intellij.util.IncorrectOperationException
import org.jetbrains.kotlin.backend.common.onlyIf
import org.jetbrains.kotlin.idea.KotlinFileType
import org.jetbrains.kotlin.idea.core.util.toPsiFile
import org.jetbrains.kotlin.idea.refactoring.KotlinRefactoringBundle
@@ -67,7 +66,7 @@ internal class MoveKotlinTopLevelDeclarationsModel(
val targetPackageWrapper = PackageWrapper(PsiManager.getInstance(project), targetPackage)
return if (selectedPsiDirectory === null)
return if (selectedPsiDirectory == null)
TargetDirAndDestination(null, MultipleRootsMoveDestination(targetPackageWrapper))
else {
TargetDirAndDestination(
@@ -99,6 +98,7 @@ internal class MoveKotlinTopLevelDeclarationsModel(
val (targetDir, moveDestination) = selectPackageBasedTargetDirAndDestination()
val targetDirectory = moveDestination.getTargetIfExists(sourceDirectory)
?: throw ConfigurationException("Can't get target directory for selected package")
val destination = sourceFiles
.mapToSingleOrNull { moveDestination.getTargetIfExists(it) }
@@ -155,8 +155,12 @@ internal class MoveKotlinTopLevelDeclarationsModel(
}
val targetDirPath = targetFile.toPath().parent
val projectBasePath = project.basePath ?: throw ConfigurationException("Can't move for current project")
if (targetDirPath === null || !targetDirPath.startsWith(projectBasePath)) {
?: throw ConfigurationException("Incorrect target path. Directory is not specified.")
val projectBasePath = project.basePath
?: throw ConfigurationException("Can't move for current project")
if (!targetDirPath.startsWith(projectBasePath)) {
throw ConfigurationException("Incorrect target path. Directory $targetDirPath does not belong to current project.")
}
@@ -222,7 +226,7 @@ internal class MoveKotlinTopLevelDeclarationsModel(
if (targetFileName != null) checkTargetFileName(targetFileName)
val moveDestination = selectPackageBasedTargetDirAndDestination().destination
val targetDir = moveDestination.getTargetIfExists(sourceDirectory)
val targetDir = moveDestination.getTargetIfExists(sourceDirectory) ?: return null
val filesExistingInTargetDir = getFilesExistingInTargetDir(targetFileName, targetDir)