Move Nested Class to Upper Level: Do not show directory selection dialog twice
#KT-13906 Fixed
This commit is contained in:
@@ -131,6 +131,7 @@ These artifacts include extensions for the types available in the latter JDKs, s
|
||||
- [`KT-13759`](https://youtrack.jetbrains.com/issue/KT-13759) Rename: Process object-wrapping alias references
|
||||
- [`KT-13903`](https://youtrack.jetbrains.com/issue/KT-13903) Move: Remove companion object which becomes empty after the move
|
||||
- [`KT-13916`](https://youtrack.jetbrains.com/issue/KT-13916) Move: Report visibility conflicts in import directives
|
||||
- [`KT-13906`](https://youtrack.jetbrains.com/issue/KT-13906) Move Nested Class to Upper Level: Do not show directory selection dialog twice
|
||||
|
||||
##### New features
|
||||
|
||||
|
||||
+19
-16
@@ -17,6 +17,7 @@
|
||||
package org.jetbrains.kotlin.idea.refactoring.move.moveDeclarations.ui;
|
||||
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.options.ConfigurationException;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.roots.JavaProjectRootsUtil;
|
||||
import com.intellij.openapi.roots.ProjectRootManager;
|
||||
@@ -299,16 +300,16 @@ public class MoveKotlinNestedClassesToUpperLevelDialog extends MoveDialogBase {
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private String getErrorMessageIfAny() {
|
||||
private PsiElement getTargetContainerWithValidation() throws ConfigurationException {
|
||||
String className = getClassName();
|
||||
String parameterName = getParameterName();
|
||||
|
||||
if (className != null && className.isEmpty()) return RefactoringBundle.message("no.class.name.specified");
|
||||
if (!KotlinNameSuggester.INSTANCE.isIdentifier(className)) return RefactoringMessageUtil.getIncorrectIdentifierMessage(className);
|
||||
if (className != null && className.isEmpty()) throw new ConfigurationException(RefactoringBundle.message("no.class.name.specified"));
|
||||
if (!KotlinNameSuggester.INSTANCE.isIdentifier(className)) throw new ConfigurationException(RefactoringMessageUtil.getIncorrectIdentifierMessage(className));
|
||||
|
||||
if (passOuterClassCheckBox.isSelected()) {
|
||||
if (parameterName != null && parameterName.isEmpty()) return RefactoringBundle.message("no.parameter.name.specified");
|
||||
if (!KotlinNameSuggester.INSTANCE.isIdentifier(parameterName)) return RefactoringMessageUtil.getIncorrectIdentifierMessage(parameterName);
|
||||
if (parameterName != null && parameterName.isEmpty()) throw new ConfigurationException(RefactoringBundle.message("no.parameter.name.specified"));
|
||||
if (!KotlinNameSuggester.INSTANCE.isIdentifier(parameterName)) throw new ConfigurationException(RefactoringMessageUtil.getIncorrectIdentifierMessage(parameterName));
|
||||
}
|
||||
|
||||
PsiElement targetContainer = getTargetContainer();
|
||||
@@ -317,14 +318,14 @@ public class MoveKotlinNestedClassesToUpperLevelDialog extends MoveDialogBase {
|
||||
KtClassOrObject targetClass = (KtClassOrObject) targetContainer;
|
||||
for (KtDeclaration member : targetClass.getDeclarations()) {
|
||||
if (member instanceof KtClassOrObject && className != null && className.equals(member.getName())) {
|
||||
return RefactoringBundle.message("inner.class.exists", className, targetClass.getName());
|
||||
throw new ConfigurationException(RefactoringBundle.message("inner.class.exists", className, targetClass.getName()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (targetContainer instanceof PsiDirectory || targetContainer instanceof KtFile) {
|
||||
FqName targetPackageFqName = getTargetPackageFqName();
|
||||
if (targetPackageFqName == null) return "No package corresponds to this directory";
|
||||
if (targetPackageFqName == null) throw new ConfigurationException("No package corresponds to this directory");
|
||||
|
||||
//noinspection ConstantConditions
|
||||
ClassifierDescriptor existingClass = DescriptorUtils
|
||||
@@ -332,28 +333,30 @@ public class MoveKotlinNestedClassesToUpperLevelDialog extends MoveDialogBase {
|
||||
.getPackage(targetPackageFqName)
|
||||
.getMemberScope()
|
||||
.getContributedClassifier(Name.identifier(className), NoLookupLocation.FROM_IDE);
|
||||
if (existingClass != null) return "Class " + className + " already exists in package " + targetPackageFqName;
|
||||
if (existingClass != null) throw new ConfigurationException("Class " + className + " already exists in package " + targetPackageFqName);
|
||||
|
||||
PsiDirectory targetDir = targetContainer instanceof PsiDirectory
|
||||
? (PsiDirectory) targetContainer
|
||||
: targetContainer.getContainingFile().getContainingDirectory();
|
||||
return RefactoringMessageUtil.checkCanCreateFile(targetDir, className + ".kt");
|
||||
String message = RefactoringMessageUtil.checkCanCreateFile(targetDir, className + ".kt");
|
||||
if (message != null) throw new ConfigurationException(message);
|
||||
}
|
||||
|
||||
return null;
|
||||
return targetContainer;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doAction() {
|
||||
String errorMessage = getErrorMessageIfAny();
|
||||
if (errorMessage != null) {
|
||||
CommonRefactoringUtil.showErrorMessage(MoveInnerImpl.REFACTORING_NAME, errorMessage, HelpID.MOVE_INNER_UPPER, project);
|
||||
PsiElement target;
|
||||
try {
|
||||
target = getTargetContainerWithValidation();
|
||||
if (target == null) return;
|
||||
}
|
||||
catch (ConfigurationException e) {
|
||||
CommonRefactoringUtil.showErrorMessage(MoveInnerImpl.REFACTORING_NAME, e.getMessage(), HelpID.MOVE_INNER_UPPER, project);
|
||||
return;
|
||||
}
|
||||
|
||||
PsiElement target = getTargetContainer();
|
||||
if (target == null) return;
|
||||
|
||||
KotlinMoveTarget moveTarget;
|
||||
if (target instanceof PsiDirectory) {
|
||||
final PsiDirectory targetDir = (PsiDirectory) target;
|
||||
|
||||
Reference in New Issue
Block a user