Search in sources :

Example 11 with PsiFileSystemItem

use of com.intellij.psi.PsiFileSystemItem in project intellij-community by JetBrains.

the class PyImportedModuleType method resolveMember.

@Nullable
@Override
public List<? extends RatedResolveResult> resolveMember(@NotNull String name, @Nullable PyExpression location, @NotNull AccessDirection direction, @NotNull PyResolveContext resolveContext) {
    final PsiElement resolved = myImportedModule.resolve();
    if (resolved != null) {
        final PsiFile containingFile = location != null ? location.getContainingFile() : null;
        List<PsiElement> elements = Collections.singletonList(ResolveImportUtil.resolveChild(resolved, name, containingFile, false, true, false));
        final PyImportElement importElement = myImportedModule.getImportElement();
        final PyFile resolvedFile = PyUtil.as(resolved, PyFile.class);
        if (location != null && importElement != null && PyUtil.inSameFile(location, importElement) && ResolveImportUtil.getPointInImport(location) == PointInImport.NONE && resolved instanceof PsiFileSystemItem && (resolvedFile == null || !PyUtil.isPackage(resolvedFile) || resolvedFile.getElementNamed(name) == null)) {
            final List<PsiElement> importedSubmodules = PyModuleType.collectImportedSubmodules((PsiFileSystemItem) resolved, location);
            if (importedSubmodules != null) {
                final Set<PsiElement> imported = Sets.newHashSet(importedSubmodules);
                elements = ContainerUtil.filter(elements, element -> imported.contains(element));
            }
        }
        return ResolveImportUtil.rateResults(elements);
    }
    return null;
}
Also used : ProcessingContext(com.intellij.util.ProcessingContext) ArrayUtil(com.intellij.util.ArrayUtil) LookupElement(com.intellij.codeInsight.lookup.LookupElement) ResolveImportUtil(com.jetbrains.python.psi.resolve.ResolveImportUtil) Set(java.util.Set) ContainerUtil(com.intellij.util.containers.ContainerUtil) PyResolveContext(com.jetbrains.python.psi.resolve.PyResolveContext) Sets(com.google.common.collect.Sets) ArrayList(java.util.ArrayList) RatedResolveResult(com.jetbrains.python.psi.resolve.RatedResolveResult) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) PsiElement(com.intellij.psi.PsiElement) PsiFile(com.intellij.psi.PsiFile) com.jetbrains.python.psi(com.jetbrains.python.psi) PsiDirectory(com.intellij.psi.PsiDirectory) PyImportedModule(com.jetbrains.python.psi.impl.PyImportedModule) PointInImport(com.jetbrains.python.psi.resolve.PointInImport) NotNull(org.jetbrains.annotations.NotNull) PsiFileSystemItem(com.intellij.psi.PsiFileSystemItem) Collections(java.util.Collections) PsiFile(com.intellij.psi.PsiFile) PsiFileSystemItem(com.intellij.psi.PsiFileSystemItem) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 12 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 13 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 14 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)

Example 15 with PsiFileSystemItem

use of com.intellij.psi.PsiFileSystemItem in project intellij-community by JetBrains.

the class FileIncludeManagerImpl method doResolve.

@Nullable
private PsiFileSystemItem doResolve(@NotNull final FileIncludeInfo info, @NotNull final PsiFile context) {
    if (info instanceof FileIncludeInfoImpl) {
        String id = ((FileIncludeInfoImpl) info).providerId;
        FileIncludeProvider provider = id == null ? null : myProviderMap.get(id);
        final PsiFileSystemItem resolvedByProvider = provider == null ? null : provider.resolveIncludedFile(info, context);
        if (resolvedByProvider != null) {
            return resolvedByProvider;
        }
    }
    PsiFileImpl psiFile = (PsiFileImpl) myPsiFileFactory.createFileFromText("dummy.txt", FileTypes.PLAIN_TEXT, info.path);
    psiFile.setOriginalFile(context);
    return new FileReferenceSet(psiFile) {

        @Override
        protected boolean useIncludingFileAsContext() {
            return false;
        }
    }.resolve();
}
Also used : PsiFileImpl(com.intellij.psi.impl.source.PsiFileImpl) FileReferenceSet(com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReferenceSet) PsiFileSystemItem(com.intellij.psi.PsiFileSystemItem) Nullable(org.jetbrains.annotations.Nullable)

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