use of com.intellij.refactoring.MoveDestination in project kotlin by JetBrains.
the class MoveKotlinTopLevelDeclarationsDialog method doAction.
@Override
protected void doAction() {
KotlinMoveTarget target = selectMoveTarget();
if (target == null)
return;
saveRefactoringSettings();
List<KtNamedDeclaration> elementsToMove = getSelectedElementsToMove();
List<KtFile> sourceFiles = getSourceFiles(elementsToMove);
final PsiDirectory sourceDirectory = getSourceDirectory(sourceFiles);
for (PsiElement element : elementsToMove) {
String message = target.verify(element.getContainingFile());
if (message != null) {
CommonRefactoringUtil.showErrorMessage(RefactoringBundle.message("error.title"), message, null, myProject);
return;
}
}
try {
boolean deleteSourceFile = false;
if (isFullFileMove()) {
if (isMoveToPackage()) {
Pair<VirtualFile, ? extends MoveDestination> sourceRootWithMoveDestination = selectPackageBasedTargetDirAndDestination(false);
//noinspection ConstantConditions
final MoveDestination moveDestination = sourceRootWithMoveDestination.getSecond();
PsiDirectory targetDir = moveDestination.getTargetIfExists(sourceDirectory);
String targetFileName = sourceFiles.size() > 1 ? null : tfFileNameInPackage.getText();
List<PsiFile> filesExistingInTargetDir = getFilesExistingInTargetDir(sourceFiles, targetFileName, targetDir);
if (filesExistingInTargetDir.isEmpty()) {
PsiDirectory targetDirectory = ApplicationUtilsKt.runWriteAction(new Function0<PsiDirectory>() {
@Override
public PsiDirectory invoke() {
return moveDestination.getTargetDirectory(sourceDirectory);
}
});
for (KtFile sourceFile : sourceFiles) {
MoveUtilsKt.setUpdatePackageDirective(sourceFile, cbUpdatePackageDirective.isSelected());
}
invokeRefactoring(new MoveFilesWithDeclarationsProcessor(myProject, sourceFiles, targetDirectory, targetFileName, isSearchInComments(), isSearchInNonJavaFiles(), moveCallback));
return;
}
}
int ret = Messages.showYesNoCancelDialog(myProject, "You are about to move all declarations out of the source file(s). Do you want to delete empty files?", RefactoringBundle.message("move.title"), Messages.getQuestionIcon());
if (ret == Messages.CANCEL)
return;
deleteSourceFile = ret == Messages.YES;
}
MoveDeclarationsDescriptor options = new MoveDeclarationsDescriptor(elementsToMove, target, MoveDeclarationsDelegate.TopLevel.INSTANCE, isSearchInComments(), isSearchInNonJavaFiles(), true, deleteSourceFile, moveCallback, false);
invokeRefactoring(new MoveKotlinDeclarationsProcessor(myProject, options, Mover.Default.INSTANCE));
} catch (IncorrectOperationException e) {
CommonRefactoringUtil.showErrorMessage(RefactoringBundle.message("error.title"), e.getMessage(), null, myProject);
}
}
use of com.intellij.refactoring.MoveDestination in project intellij-community by JetBrains.
the class JavaIntroduceParameterObjectClassDescriptor method createClass.
@Override
public PsiClass createClass(PsiMethod method, ReadWriteAccessDetector.Access[] accessors) {
if (isUseExistingClass()) {
return getExistingClass();
}
final ParameterObjectBuilder beanClassBuilder = new ParameterObjectBuilder();
beanClassBuilder.setVisibility(isCreateInnerClass() ? PsiModifier.PRIVATE : PsiModifier.PUBLIC);
beanClassBuilder.setProject(method.getProject());
beanClassBuilder.setTypeArguments(getTypeParameters());
beanClassBuilder.setClassName(getClassName());
beanClassBuilder.setPackageName(getPackageName());
PsiParameter[] parameters = method.getParameterList().getParameters();
final ParameterInfoImpl[] parameterInfos = getParamsToMerge();
for (int i = 0; i < parameterInfos.length; i++) {
PsiParameter parameter = parameters[parameterInfos[i].getOldIndex()];
final boolean setterRequired = accessors[i] == ReadWriteAccessDetector.Access.Write;
final String newName = parameterInfos[i].getName();
beanClassBuilder.addField(parameter, newName, parameterInfos[i].getTypeWrapper().getType(method), setterRequired);
}
final String classString = beanClassBuilder.buildBeanClass();
try {
final PsiFileFactory factory = PsiFileFactory.getInstance(method.getProject());
final PsiJavaFile newFile = (PsiJavaFile) factory.createFileFromText(getClassName() + ".java", JavaFileType.INSTANCE, classString);
if (isCreateInnerClass()) {
final PsiClass containingClass = method.getContainingClass();
final PsiClass[] classes = newFile.getClasses();
assert classes.length > 0 : classString;
final PsiClass innerClass = (PsiClass) containingClass.add(classes[0]);
PsiUtil.setModifierProperty(innerClass, PsiModifier.STATIC, true);
return (PsiClass) JavaCodeStyleManager.getInstance(newFile.getProject()).shortenClassReferences(innerClass);
} else {
final PsiFile containingFile = method.getContainingFile();
final PsiDirectory containingDirectory = containingFile.getContainingDirectory();
final PsiDirectory directory;
final MoveDestination moveDestination = getMoveDestination();
if (moveDestination != null) {
directory = moveDestination.getTargetDirectory(containingDirectory);
} else {
final Module module = ModuleUtilCore.findModuleForPsiElement(containingFile);
directory = PackageUtil.findOrCreateDirectoryForPackage(module, getPackageName(), containingDirectory, true, true);
}
if (directory != null) {
PsiFile file = directory.findFile(newFile.getName());
if (file == null) {
final CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(method.getManager().getProject());
final PsiElement shortenedFile = JavaCodeStyleManager.getInstance(newFile.getProject()).shortenClassReferences(newFile);
final PsiElement reformattedFile = codeStyleManager.reformat(shortenedFile);
file = (PsiFile) directory.add(reformattedFile);
}
return ((PsiJavaFile) file).getClasses()[0];
}
}
} catch (IncorrectOperationException e) {
LOG.error(e);
}
return null;
}
use of com.intellij.refactoring.MoveDestination in project intellij-community by JetBrains.
the class CreateClassDialog method doOKAction.
@Override
protected void doOKAction() {
RecentsManager.getInstance(myProject).registerRecentEntry(RECENTS_KEY, myPackageComponent.getText());
final String packageName = getPackageName();
final String[] errorString = new String[1];
CommandProcessor.getInstance().executeCommand(myProject, () -> {
try {
final PackageWrapper targetPackage = new PackageWrapper(PsiManager.getInstance(myProject), packageName);
final MoveDestination destination = myDestinationCB.selectDirectory(targetPackage, false);
if (destination == null)
return;
myTargetDirectory = ApplicationManager.getApplication().runWriteAction(new Computable<PsiDirectory>() {
@Override
public PsiDirectory compute() {
PsiDirectory baseDir = getBaseDir(packageName);
if (baseDir == null && destination instanceof MultipleRootsMoveDestination) {
errorString[0] = "Destination not found for package '" + packageName + "'";
return null;
}
return destination.getTargetDirectory(baseDir);
}
});
if (myTargetDirectory == null) {
return;
}
errorString[0] = RefactoringMessageUtil.checkCanCreateClass(myTargetDirectory, getClassName());
} catch (IncorrectOperationException e) {
errorString[0] = e.getMessage();
}
}, CodeInsightBundle.message("create.directory.command"), null);
if (errorString[0] != null) {
if (errorString[0].length() > 0) {
Messages.showMessageDialog(myProject, errorString[0], CommonBundle.getErrorTitle(), Messages.getErrorIcon());
}
return;
}
super.doOKAction();
}
use of com.intellij.refactoring.MoveDestination in project intellij-community by JetBrains.
the class JavaExtractSuperBaseDialog method preparePackage.
@Override
protected void preparePackage() throws OperationFailedException {
final String targetPackageName = getTargetPackageName();
final PsiFile containingFile = mySourceClass.getContainingFile();
final boolean fromDefaultPackage = containingFile instanceof PsiClassOwner && ((PsiClassOwner) containingFile).getPackageName().isEmpty();
if (!(fromDefaultPackage && StringUtil.isEmpty(targetPackageName)) && !PsiNameHelper.getInstance(myProject).isQualifiedName(targetPackageName)) {
throw new OperationFailedException("Invalid package name: " + targetPackageName);
}
final PsiPackage aPackage = JavaPsiFacade.getInstance(myProject).findPackage(targetPackageName);
if (aPackage != null) {
final PsiDirectory[] directories = aPackage.getDirectories(mySourceClass.getResolveScope());
if (directories.length >= 1) {
myTargetDirectory = getDirUnderSameSourceRoot(directories);
}
}
final MoveDestination moveDestination = myDestinationFolderComboBox.selectDirectory(new PackageWrapper(PsiManager.getInstance(myProject), targetPackageName), false);
if (moveDestination == null)
return;
myTargetDirectory = myTargetDirectory != null ? ApplicationManager.getApplication().runWriteAction(new Computable<PsiDirectory>() {
@Override
public PsiDirectory compute() {
return moveDestination.getTargetDirectory(myTargetDirectory);
}
}) : null;
if (myTargetDirectory == null) {
// message already reported by PackageUtil
throw new OperationFailedException("");
}
String error = RefactoringMessageUtil.checkCanCreateClass(myTargetDirectory, getExtractedSuperName());
if (error != null) {
throw new OperationFailedException(error);
}
}
use of com.intellij.refactoring.MoveDestination in project intellij-community by JetBrains.
the class JavaMoveClassesOrPackagesHandler method moveAsDirectory.
private static void moveAsDirectory(Project project, PsiElement targetContainer, final MoveCallback callback, final PsiDirectory[] directories) {
if (targetContainer instanceof PsiDirectory) {
final JavaRefactoringSettings refactoringSettings = JavaRefactoringSettings.getInstance();
final MoveDirectoryWithClassesProcessor processor = new MoveDirectoryWithClassesProcessor(project, directories, (PsiDirectory) targetContainer, refactoringSettings.RENAME_SEARCH_IN_COMMENTS_FOR_PACKAGE, refactoringSettings.RENAME_SEARCH_IN_COMMENTS_FOR_PACKAGE, true, callback);
processor.setPrepareSuccessfulSwingThreadCallback(() -> {
});
processor.run();
} else {
final boolean containsJava = hasJavaFiles(directories[0]);
if (!containsJava) {
MoveFilesOrDirectoriesUtil.doMove(project, directories, new PsiElement[] { targetContainer }, callback);
return;
}
final MoveClassesOrPackagesToNewDirectoryDialog dlg = new MoveClassesOrPackagesToNewDirectoryDialog(directories[0], PsiElement.EMPTY_ARRAY, false, callback) {
@Override
protected BaseRefactoringProcessor createRefactoringProcessor(Project project, final PsiDirectory targetDirectory, PsiPackage aPackage, boolean searchInComments, boolean searchForTextOccurences) {
final MoveDestination destination = createDestination(aPackage, targetDirectory);
try {
for (PsiDirectory dir : directories) {
MoveFilesOrDirectoriesUtil.checkIfMoveIntoSelf(dir, WriteAction.compute(() -> destination.getTargetDirectory(dir)));
}
} catch (IncorrectOperationException e) {
Messages.showErrorDialog(project, e.getMessage(), RefactoringBundle.message("cannot.move"));
return null;
}
return new MoveDirectoryWithClassesProcessor(project, directories, null, searchInComments, searchForTextOccurences, true, callback) {
@Override
public TargetDirectoryWrapper getTargetDirectory(PsiDirectory dir) {
final PsiDirectory targetDirectory = destination.getTargetDirectory(dir);
return new TargetDirectoryWrapper(targetDirectory);
}
@Override
protected String getTargetName() {
return targetDirectory.getName();
}
};
}
};
dlg.show();
}
}
Aggregations