use of com.intellij.refactoring.safeDelete.SafeDeleteProcessor in project intellij-community by JetBrains.
the class DeleteHandler method deletePsiElement.
public static void deletePsiElement(final PsiElement[] elementsToDelete, final Project project, boolean needConfirmation) {
if (elementsToDelete == null || elementsToDelete.length == 0)
return;
final PsiElement[] elements = PsiTreeUtil.filterAncestors(elementsToDelete);
boolean safeDeleteApplicable = true;
for (int i = 0; i < elements.length && safeDeleteApplicable; i++) {
PsiElement element = elements[i];
safeDeleteApplicable = SafeDeleteProcessor.validElement(element);
}
final boolean dumb = DumbService.getInstance(project).isDumb();
if (safeDeleteApplicable && !dumb) {
final Ref<Boolean> exit = Ref.create(false);
final SafeDeleteDialog dialog = new SafeDeleteDialog(project, elements, new SafeDeleteDialog.Callback() {
@Override
public void run(final SafeDeleteDialog dialog) {
if (!CommonRefactoringUtil.checkReadOnlyStatusRecursively(project, Arrays.asList(elements), true))
return;
SafeDeleteProcessor processor = SafeDeleteProcessor.createInstance(project, () -> {
exit.set(true);
dialog.close(DialogWrapper.OK_EXIT_CODE);
}, elements, dialog.isSearchInComments(), dialog.isSearchForTextOccurences(), true);
processor.run();
}
}) {
@Override
protected boolean isDelete() {
return true;
}
};
if (needConfirmation) {
dialog.setTitle(RefactoringBundle.message("delete.title"));
if (!dialog.showAndGet() || exit.get()) {
return;
}
}
} else {
@SuppressWarnings({ "UnresolvedPropertyKey" }) String warningMessage = DeleteUtil.generateWarningMessage(IdeBundle.message("prompt.delete.elements"), elements);
boolean anyDirectories = false;
String directoryName = null;
for (PsiElement psiElement : elementsToDelete) {
if (psiElement instanceof PsiDirectory && !PsiUtilBase.isSymLink((PsiDirectory) psiElement)) {
anyDirectories = true;
directoryName = ((PsiDirectory) psiElement).getName();
break;
}
}
if (anyDirectories) {
if (elements.length == 1) {
warningMessage += IdeBundle.message("warning.delete.all.files.and.subdirectories", directoryName);
} else {
warningMessage += IdeBundle.message("warning.delete.all.files.and.subdirectories.in.the.selected.directory");
}
}
if (safeDeleteApplicable && dumb) {
warningMessage += "\n\nWarning:\n Safe delete is not available while " + ApplicationNamesInfo.getInstance().getFullProductName() + " updates indices,\n no usages will be checked.";
}
if (needConfirmation) {
int result = Messages.showOkCancelDialog(project, warningMessage, IdeBundle.message("title.delete"), ApplicationBundle.message("button.delete"), CommonBundle.getCancelButtonText(), Messages.getQuestionIcon());
if (result != Messages.OK)
return;
}
}
CommandProcessor.getInstance().executeCommand(project, () -> NonProjectFileWritingAccessProvider.disableChecksDuring(() -> {
Collection<PsiElement> directories = ContainerUtil.newSmartList();
for (PsiElement e : elements) {
if (e instanceof PsiFileSystemItem && e.getParent() != null) {
directories.add(e.getParent());
}
}
if (!CommonRefactoringUtil.checkReadOnlyStatus(project, Arrays.asList(elements), directories, false)) {
return;
}
// deleted from project view or something like that.
if (CommonDataKeys.EDITOR.getData(DataManager.getInstance().getDataContext()) == null) {
CommandProcessor.getInstance().markCurrentCommandAsGlobal(project);
}
for (final PsiElement elementToDelete : elements) {
//was already deleted
if (!elementToDelete.isValid())
continue;
if (elementToDelete instanceof PsiDirectory) {
VirtualFile virtualFile = ((PsiDirectory) elementToDelete).getVirtualFile();
if (virtualFile.isInLocalFileSystem() && !virtualFile.is(VFileProperty.SYMLINK)) {
ArrayList<VirtualFile> readOnlyFiles = new ArrayList<>();
CommonRefactoringUtil.collectReadOnlyFiles(virtualFile, readOnlyFiles);
if (!readOnlyFiles.isEmpty()) {
String message = IdeBundle.message("prompt.directory.contains.read.only.files", virtualFile.getPresentableUrl());
int _result = Messages.showYesNoDialog(project, message, IdeBundle.message("title.delete"), Messages.getQuestionIcon());
if (_result != Messages.YES)
continue;
boolean success = true;
for (VirtualFile file : readOnlyFiles) {
success = clearReadOnlyFlag(file, project);
if (!success)
break;
}
if (!success)
continue;
}
}
} else if (!elementToDelete.isWritable() && !(elementToDelete instanceof PsiFileSystemItem && PsiUtilBase.isSymLink((PsiFileSystemItem) elementToDelete))) {
final PsiFile file = elementToDelete.getContainingFile();
if (file != null) {
final VirtualFile virtualFile = file.getVirtualFile();
if (virtualFile.isInLocalFileSystem()) {
int _result = MessagesEx.fileIsReadOnly(project, virtualFile).setTitle(IdeBundle.message("title.delete")).appendMessage(" " + IdeBundle.message("prompt.delete.it.anyway")).askYesNo();
if (_result != Messages.YES)
continue;
boolean success = clearReadOnlyFlag(virtualFile, project);
if (!success)
continue;
}
}
}
try {
elementToDelete.checkDelete();
} catch (IncorrectOperationException ex) {
Messages.showMessageDialog(project, ex.getMessage(), CommonBundle.getErrorTitle(), Messages.getErrorIcon());
continue;
}
ApplicationManager.getApplication().runWriteAction(() -> {
try {
elementToDelete.delete();
} catch (final IncorrectOperationException ex) {
ApplicationManager.getApplication().invokeLater(() -> Messages.showMessageDialog(project, ex.getMessage(), CommonBundle.getErrorTitle(), Messages.getErrorIcon()));
}
});
}
}), RefactoringBundle.message("safe.delete.command", RefactoringUIUtil.calculatePsiElementDescriptionList(elements)), null);
}
Aggregations