use of com.intellij.psi.PsiFileSystemItem in project intellij-community by JetBrains.
the class PsiFileSystemItemUtil method getCommonAncestor.
@Nullable
static PsiFileSystemItem getCommonAncestor(PsiFileSystemItem file1, PsiFileSystemItem file2) {
if (file1 == file2)
return file1;
int depth1 = getDepth(file1);
int depth2 = getDepth(file2);
PsiFileSystemItem parent1 = file1;
PsiFileSystemItem parent2 = file2;
while (depth1 > depth2 && parent1 != null) {
parent1 = parent1.getParent();
depth1--;
}
while (depth2 > depth1 && parent2 != null) {
parent2 = parent2.getParent();
depth2--;
}
while (parent1 != null && parent2 != null && !parent1.equals(parent2)) {
parent1 = parent1.getParent();
parent2 = parent2.getParent();
}
return parent1;
}
use of com.intellij.psi.PsiFileSystemItem in project intellij-plugins by JetBrains.
the class DartRunConfigurationBase method getRefactoringElementListener.
@Nullable
@Override
public RefactoringElementListener getRefactoringElementListener(final PsiElement element) {
if (!(element instanceof PsiFileSystemItem))
return null;
final String filePath = getRunnerParameters().getFilePath();
final VirtualFile file = filePath == null ? null : ((PsiFileSystemItem) element).getVirtualFile();
if (file == null)
return null;
final String affectedPath = file.getPath();
if (element instanceof PsiFile) {
if (filePath.equals(affectedPath)) {
return new RenameRefactoringListener(affectedPath);
}
}
if (element instanceof PsiDirectory) {
if (filePath.startsWith(affectedPath + "/")) {
return new RenameRefactoringListener(affectedPath);
}
}
return null;
}
use of com.intellij.psi.PsiFileSystemItem in project intellij-community by JetBrains.
the class FileListPasteProvider method performPaste.
@Override
public void performPaste(@NotNull DataContext dataContext) {
final Project project = CommonDataKeys.PROJECT.getData(dataContext);
final IdeView ideView = LangDataKeys.IDE_VIEW.getData(dataContext);
if (project == null || ideView == null)
return;
if (!FileCopyPasteUtil.isFileListFlavorAvailable())
return;
final Transferable contents = CopyPasteManager.getInstance().getContents();
if (contents == null)
return;
final List<File> fileList = FileCopyPasteUtil.getFileList(contents);
if (fileList == null)
return;
if (DumbService.isDumb(project)) {
DumbService.getInstance(project).showDumbModeNotification("Sorry, file copy/paste is not available during indexing");
return;
}
final List<PsiElement> elements = new ArrayList<>();
for (File file : fileList) {
final VirtualFile vFile = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(file);
if (vFile != null) {
final PsiManager instance = PsiManager.getInstance(project);
PsiFileSystemItem item = vFile.isDirectory() ? instance.findDirectory(vFile) : instance.findFile(vFile);
if (item != null) {
elements.add(item);
}
}
}
if (elements.size() > 0) {
final PsiDirectory dir = ideView.getOrChooseDirectory();
if (dir != null) {
final boolean move = LinuxDragAndDropSupport.isMoveOperation(contents);
if (move) {
new MoveFilesOrDirectoriesHandler().doMove(PsiUtilCore.toPsiElementArray(elements), dir);
} else {
new CopyFilesOrDirectoriesHandler().doCopy(PsiUtilCore.toPsiElementArray(elements), dir);
}
}
}
}
use of com.intellij.psi.PsiFileSystemItem 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);
}
use of com.intellij.psi.PsiFileSystemItem in project intellij-community by JetBrains.
the class ScopeViewPane method select.
@Override
public void select(Object element, VirtualFile file, boolean requestFocus) {
if (file == null)
return;
PsiFileSystemItem psiFile = file.isDirectory() ? PsiManager.getInstance(myProject).findDirectory(file) : PsiManager.getInstance(myProject).findFile(file);
if (psiFile == null)
return;
if (!(element instanceof PsiElement))
return;
List<NamedScope> allScopes = ContainerUtil.newArrayList(getShownScopes());
for (NamedScope scope : allScopes) {
String name = scope.getName();
if (name.equals(getSubId())) {
allScopes.remove(scope);
allScopes.add(0, scope);
break;
}
}
for (NamedScope scope : allScopes) {
String name = scope.getName();
PackageSet packageSet = scope.getValue();
if (packageSet == null)
continue;
if (changeView(packageSet, ((PsiElement) element), psiFile, name, myNamedScopeManager, requestFocus))
break;
if (changeView(packageSet, ((PsiElement) element), psiFile, name, myDependencyValidationManager, requestFocus))
break;
}
}
Aggregations