Search in sources :

Example 21 with PsiFileSystemItem

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;
}
Also used : PsiFileSystemItem(com.intellij.psi.PsiFileSystemItem) Nullable(org.jetbrains.annotations.Nullable)

Example 22 with PsiFileSystemItem

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;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiDirectory(com.intellij.psi.PsiDirectory) PsiFile(com.intellij.psi.PsiFile) PsiFileSystemItem(com.intellij.psi.PsiFileSystemItem) Nullable(org.jetbrains.annotations.Nullable)

Example 23 with PsiFileSystemItem

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);
            }
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) CopyFilesOrDirectoriesHandler(com.intellij.refactoring.copy.CopyFilesOrDirectoriesHandler) Transferable(java.awt.datatransfer.Transferable) ArrayList(java.util.ArrayList) PsiManager(com.intellij.psi.PsiManager) PsiFileSystemItem(com.intellij.psi.PsiFileSystemItem) Project(com.intellij.openapi.project.Project) PsiDirectory(com.intellij.psi.PsiDirectory) MoveFilesOrDirectoriesHandler(com.intellij.refactoring.move.moveFilesOrDirectories.MoveFilesOrDirectoriesHandler) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) PsiElement(com.intellij.psi.PsiElement)

Example 24 with PsiFileSystemItem

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);
}
Also used : SafeDeleteDialog(com.intellij.refactoring.safeDelete.SafeDeleteDialog) VirtualFile(com.intellij.openapi.vfs.VirtualFile) LightVirtualFile(com.intellij.testFramework.LightVirtualFile) ArrayList(java.util.ArrayList) SafeDeleteProcessor(com.intellij.refactoring.safeDelete.SafeDeleteProcessor) PsiFileSystemItem(com.intellij.psi.PsiFileSystemItem) PsiDirectory(com.intellij.psi.PsiDirectory) Collection(java.util.Collection) PsiFile(com.intellij.psi.PsiFile) IncorrectOperationException(com.intellij.util.IncorrectOperationException) PsiElement(com.intellij.psi.PsiElement)

Example 25 with PsiFileSystemItem

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;
    }
}
Also used : PsiFileSystemItem(com.intellij.psi.PsiFileSystemItem) PsiElement(com.intellij.psi.PsiElement)

Aggregations

PsiFileSystemItem (com.intellij.psi.PsiFileSystemItem)60 VirtualFile (com.intellij.openapi.vfs.VirtualFile)31 Nullable (org.jetbrains.annotations.Nullable)20 PsiElement (com.intellij.psi.PsiElement)18 PsiFile (com.intellij.psi.PsiFile)18 Project (com.intellij.openapi.project.Project)14 PsiDirectory (com.intellij.psi.PsiDirectory)14 NotNull (org.jetbrains.annotations.NotNull)8 Module (com.intellij.openapi.module.Module)7 File (java.io.File)6 PropertiesFile (com.intellij.lang.properties.psi.PropertiesFile)5 ArrayList (java.util.ArrayList)5 PsiManager (com.intellij.psi.PsiManager)4 IProperty (com.intellij.lang.properties.IProperty)3 LookupElement (com.intellij.codeInsight.lookup.LookupElement)2 Sdk (com.intellij.openapi.projectRoots.Sdk)2 ProjectFileIndex (com.intellij.openapi.roots.ProjectFileIndex)2 SearchScope (com.intellij.psi.search.SearchScope)2 QualifiedName (com.intellij.psi.util.QualifiedName)2 XmlFile (com.intellij.psi.xml.XmlFile)2