Search in sources :

Example 46 with PsiFileSystemItem

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

the class BaseClassesAnalysisAction method getScopeModules.

/**
   * @param scope  the scope of which to return the modules
   * @return the modules contained by the scope and/or the module containing the scope.
   */
@NotNull
public static Set<Module> getScopeModules(@NotNull AnalysisScope scope) {
    final int scopeType = scope.getScopeType();
    final Set<Module> result = new HashSet<>();
    final Project project = scope.getProject();
    if (scopeType == AnalysisScope.MODULE) {
        final Module module = scope.getModule();
        if (module != null) {
            result.add(module);
        }
    } else if (scopeType == AnalysisScope.MODULES) {
        result.addAll(scope.getModules());
    } else if (scopeType == AnalysisScope.FILE) {
        final PsiElement element = scope.getElement();
        if (element != null) {
            final VirtualFile vFile = ((PsiFileSystemItem) element).getVirtualFile();
            getContainingModule(vFile, project, result);
        }
    } else if (scopeType == AnalysisScope.DIRECTORY) {
        final PsiElement element = scope.getElement();
        if (element != null) {
            final VirtualFile vFile = ((PsiFileSystemItem) element).getVirtualFile();
            final Module[] modules = ModuleManager.getInstance(project).getModules();
            getModulesContainedInDirectory(modules, vFile, result);
            getContainingModule(vFile, project, result);
        }
    } else if (scopeType == AnalysisScope.VIRTUAL_FILES) {
        final Set<VirtualFile> files = scope.getFiles();
        final Module[] modules = ModuleManager.getInstance(project).getModules();
        for (VirtualFile vFile : files) {
            getModulesContainedInDirectory(modules, vFile, result);
            getContainingModule(vFile, project, result);
        }
    } else {
        result.addAll(Arrays.asList(ModuleManager.getInstance(project).getModules()));
    }
    return result;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) Module(com.intellij.openapi.module.Module) PsiFileSystemItem(com.intellij.psi.PsiFileSystemItem) PsiElement(com.intellij.psi.PsiElement) HashSet(java.util.HashSet) NotNull(org.jetbrains.annotations.NotNull)

Example 47 with PsiFileSystemItem

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

the class PsiFileReferenceHelper method getContexts.

@Override
@NotNull
public Collection<PsiFileSystemItem> getContexts(final Project project, @NotNull final VirtualFile file) {
    final PsiFileSystemItem item = getPsiFileSystemItem(project, file);
    if (item != null) {
        final PsiFileSystemItem parent = item.getParent();
        if (parent != null) {
            final ProjectFileIndex index = ProjectRootManager.getInstance(project).getFileIndex();
            final VirtualFile parentFile = parent.getVirtualFile();
            assert parentFile != null;
            VirtualFile root = index.getSourceRootForFile(parentFile);
            if (root != null) {
                String path = VfsUtilCore.getRelativePath(parentFile, root, '.');
                if (path != null) {
                    final Module module = ModuleUtilCore.findModuleForFile(file, project);
                    if (module != null) {
                        OrderEntry orderEntry = ModuleRootManager.getInstance(module).getFileIndex().getOrderEntryForFile(file);
                        if (orderEntry instanceof ModuleSourceOrderEntry) {
                            for (ContentEntry e : ((ModuleSourceOrderEntry) orderEntry).getRootModel().getContentEntries()) {
                                for (SourceFolder sf : e.getSourceFolders(JavaModuleSourceRootTypes.SOURCES)) {
                                    if (root.equals(sf.getFile())) {
                                        String s = sf.getPackagePrefix();
                                        if (!s.isEmpty()) {
                                            path = s + "." + path;
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                        return getContextsForModule(module, path, module.getModuleWithDependenciesScope());
                    }
                }
            // TODO: content root
            }
            return Collections.singleton(parent);
        }
    }
    return Collections.emptyList();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiFileSystemItem(com.intellij.psi.PsiFileSystemItem) Module(com.intellij.openapi.module.Module) NotNull(org.jetbrains.annotations.NotNull)

Example 48 with PsiFileSystemItem

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

the class AntMissingPropertiesFileInspection method checkDomElement.

protected void checkDomElement(DomElement element, DomElementAnnotationHolder holder, DomHighlightingHelper helper) {
    if (element instanceof AntDomProperty) {
        final AntDomProperty property = (AntDomProperty) element;
        final GenericAttributeValue<PsiFileSystemItem> fileValue = property.getFile();
        final String fileName = fileValue.getStringValue();
        if (fileName != null) {
            final PropertiesFile propertiesFile = property.getPropertiesFile();
            if (propertiesFile == null) {
                final PsiFileSystemItem file = fileValue.getValue();
                if (file instanceof XmlFile) {
                    holder.createProblem(fileValue, AntBundle.message("file.type.xml.not.supported", fileName));
                } else if (file instanceof PsiFile) {
                    holder.createProblem(fileValue, AntBundle.message("file.type.not.supported", fileName));
                } else {
                    holder.createProblem(fileValue, AntBundle.message("file.doesnt.exist", fileName));
                }
            }
        }
    }
}
Also used : XmlFile(com.intellij.psi.xml.XmlFile) PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile) PsiFile(com.intellij.psi.PsiFile) PsiFileSystemItem(com.intellij.psi.PsiFileSystemItem) AntDomProperty(com.intellij.lang.ant.dom.AntDomProperty)

Example 49 with PsiFileSystemItem

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

the class NavBarPanel method getData.

@Override
@Nullable
public Object getData(String dataId) {
    if (CommonDataKeys.PROJECT.is(dataId)) {
        return !myProject.isDisposed() ? myProject : null;
    }
    if (LangDataKeys.MODULE.is(dataId)) {
        final Module module = getSelectedElement(Module.class);
        if (module != null && !module.isDisposed())
            return module;
        final PsiElement element = getSelectedElement(PsiElement.class);
        if (element != null) {
            return ModuleUtilCore.findModuleForPsiElement(element);
        }
        return null;
    }
    if (LangDataKeys.MODULE_CONTEXT.is(dataId)) {
        final PsiDirectory directory = getSelectedElement(PsiDirectory.class);
        if (directory != null) {
            final VirtualFile dir = directory.getVirtualFile();
            if (ProjectRootsUtil.isModuleContentRoot(dir, myProject)) {
                return ModuleUtilCore.findModuleForPsiElement(directory);
            }
        }
        return null;
    }
    if (CommonDataKeys.PSI_ELEMENT.is(dataId)) {
        final PsiElement element = getSelectedElement(PsiElement.class);
        return element != null && element.isValid() ? element : null;
    }
    if (LangDataKeys.PSI_ELEMENT_ARRAY.is(dataId)) {
        final List<PsiElement> elements = getSelectedElements(PsiElement.class);
        if (elements == null || elements.isEmpty())
            return null;
        List<PsiElement> result = new ArrayList<>();
        for (PsiElement element : elements) {
            if (element != null && element.isValid()) {
                result.add(element);
            }
        }
        return result.isEmpty() ? null : result.toArray(new PsiElement[result.size()]);
    }
    if (CommonDataKeys.VIRTUAL_FILE_ARRAY.is(dataId)) {
        PsiElement[] psiElements = (PsiElement[]) getData(LangDataKeys.PSI_ELEMENT_ARRAY.getName());
        if (psiElements == null)
            return null;
        Set<VirtualFile> files = new LinkedHashSet<>();
        for (PsiElement element : psiElements) {
            PsiFile file = element.getContainingFile();
            if (file != null) {
                final VirtualFile virtualFile = file.getVirtualFile();
                if (virtualFile != null) {
                    files.add(virtualFile);
                }
            } else if (element instanceof PsiFileSystemItem) {
                files.add(((PsiFileSystemItem) element).getVirtualFile());
            }
        }
        return !files.isEmpty() ? VfsUtilCore.toVirtualFileArray(files) : null;
    }
    if (CommonDataKeys.NAVIGATABLE_ARRAY.is(dataId)) {
        final List<Navigatable> elements = getSelectedElements(Navigatable.class);
        return elements == null || elements.isEmpty() ? null : elements.toArray(new Navigatable[elements.size()]);
    }
    if (PlatformDataKeys.CONTEXT_COMPONENT.is(dataId)) {
        return this;
    }
    if (PlatformDataKeys.CUT_PROVIDER.is(dataId)) {
        return myCopyPasteDelegator.getCutProvider();
    }
    if (PlatformDataKeys.COPY_PROVIDER.is(dataId)) {
        return myCopyPasteDelegator.getCopyProvider();
    }
    if (PlatformDataKeys.PASTE_PROVIDER.is(dataId)) {
        return myCopyPasteDelegator.getPasteProvider();
    }
    if (PlatformDataKeys.DELETE_ELEMENT_PROVIDER.is(dataId)) {
        return getSelectedElement(Module.class) != null ? myDeleteModuleProvider : new DeleteHandler.DefaultDeleteProvider();
    }
    if (LangDataKeys.IDE_VIEW.is(dataId)) {
        return myIdeView;
    }
    return null;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) DeleteHandler(com.intellij.ide.util.DeleteHandler) PsiFileSystemItem(com.intellij.psi.PsiFileSystemItem) Navigatable(com.intellij.pom.Navigatable) PsiDirectory(com.intellij.psi.PsiDirectory) PsiFile(com.intellij.psi.PsiFile) Module(com.intellij.openapi.module.Module) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 50 with PsiFileSystemItem

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

the class ImagePreviewComponent method getPreviewComponent.

public static JComponent getPreviewComponent(@Nullable final PsiElement parent) {
    if (parent == null) {
        return null;
    }
    final PsiReference[] references = parent.getReferences();
    for (final PsiReference reference : references) {
        final PsiElement fileItem = reference.resolve();
        if (fileItem instanceof PsiFileSystemItem) {
            final PsiFileSystemItem item = (PsiFileSystemItem) fileItem;
            if (!item.isDirectory()) {
                final VirtualFile file = item.getVirtualFile();
                if (file != null && supportedExtensions.contains(file.getExtension())) {
                    try {
                        refresh(file);
                        SoftReference<BufferedImage> imageRef = file.getUserData(BUFFERED_IMAGE_REF_KEY);
                        final BufferedImage image = SoftReference.dereference(imageRef);
                        if (image != null) {
                            return new ImagePreviewComponent(image, file.getLength());
                        }
                    } catch (IOException ignored) {
                    // nothing
                    }
                }
            }
        }
    }
    return null;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiReference(com.intellij.psi.PsiReference) IOException(java.io.IOException) PsiFileSystemItem(com.intellij.psi.PsiFileSystemItem) PsiElement(com.intellij.psi.PsiElement) BufferedImage(java.awt.image.BufferedImage)

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