Search in sources :

Example 11 with FileIndexFacade

use of com.intellij.openapi.roots.FileIndexFacade in project intellij-community by JetBrains.

the class JavaModulePresentationProvider method getPresentation.

@Override
public ItemPresentation getPresentation(@NotNull final PsiJavaModule item) {
    return new ItemPresentation() {

        @Override
        public String getPresentableText() {
            return item.getName();
        }

        @Nullable
        @Override
        public String getLocationString() {
            VirtualFile file = PsiImplUtil.getModuleVirtualFile(item);
            FileIndexFacade index = FileIndexFacade.getInstance(item.getProject());
            if (index.isInLibraryClasses(file)) {
                Matcher matcher = JAR_NAME.matcher(file.getPath());
                if (matcher.find()) {
                    return matcher.group(1);
                }
            } else if (index.isInSource(file)) {
                Module module = index.getModuleForFile(file);
                if (module != null) {
                    return '[' + module.getName() + ']';
                }
            }
            return null;
        }

        @Nullable
        @Override
        public Icon getIcon(boolean unused) {
            return AllIcons.Nodes.JavaModule;
        }
    };
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Matcher(java.util.regex.Matcher) ItemPresentation(com.intellij.navigation.ItemPresentation) PsiJavaModule(com.intellij.psi.PsiJavaModule) Module(com.intellij.openapi.module.Module) FileIndexFacade(com.intellij.openapi.roots.FileIndexFacade)

Example 12 with FileIndexFacade

use of com.intellij.openapi.roots.FileIndexFacade in project intellij-community by JetBrains.

the class PsiClassFavoriteNodeProvider method checkClassUnderSources.

private boolean checkClassUnderSources(final PsiElement element, final Project project) {
    final PsiFile file = element.getContainingFile();
    if (file != null && file.getVirtualFile() != null) {
        final FileIndexFacade indexFacade = FileIndexFacade.getInstance(project);
        final VirtualFile vf = file.getVirtualFile();
        return indexFacade.isInSource(vf) || indexFacade.isInSourceContent(vf);
    }
    return false;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiFile(com.intellij.psi.PsiFile) FileIndexFacade(com.intellij.openapi.roots.FileIndexFacade)

Example 13 with FileIndexFacade

use of com.intellij.openapi.roots.FileIndexFacade in project intellij-community by JetBrains.

the class PsiUtilCore method getPsiFile.

/**
   * Tries to find PSI file for a virtual file and throws assertion error with debug info if it is null.
   */
@NotNull
public static PsiFile getPsiFile(@NotNull Project project, @NotNull VirtualFile file) {
    PsiManager psiManager = PsiManager.getInstance(project);
    PsiFile psi = psiManager.findFile(file);
    if (psi != null)
        return psi;
    FileType fileType = file.getFileType();
    FileViewProvider viewProvider = psiManager.findViewProvider(file);
    Document document = FileDocumentManager.getInstance().getDocument(file);
    boolean ignored = !(file instanceof LightVirtualFile) && FileTypeRegistry.getInstance().isFileIgnored(file);
    VirtualFile vDir = file.getParent();
    PsiDirectory psiDir = vDir == null ? null : PsiManager.getInstance(project).findDirectory(vDir);
    FileIndexFacade indexFacade = FileIndexFacade.getInstance(project);
    StringBuilder sb = new StringBuilder();
    sb.append("valid=").append(file.isValid()).append(" isDirectory=").append(file.isDirectory()).append(" hasDocument=").append(document != null).append(" length=").append(file.getLength());
    sb.append("\nproject=").append(project.getName()).append(" default=").append(project.isDefault()).append(" open=").append(project.isOpen());
    sb.append("\nfileType=").append(fileType.getName()).append("/").append(fileType.getClass().getName());
    sb.append("\nisIgnored=").append(ignored);
    sb.append(" underIgnored=").append(indexFacade.isUnderIgnored(file));
    sb.append(" inLibrary=").append(indexFacade.isInLibrarySource(file) || indexFacade.isInLibraryClasses(file));
    sb.append(" parentDir=").append(vDir == null ? "no-vfs" : vDir.isDirectory() ? "has-vfs-dir" : "has-vfs-file").append("/").append(psiDir == null ? "no-psi" : "has-psi");
    sb.append("\nviewProvider=").append(viewProvider == null ? "null" : viewProvider.getClass().getName());
    if (viewProvider != null) {
        List<PsiFile> files = viewProvider.getAllFiles();
        sb.append(" language=").append(viewProvider.getBaseLanguage().getID());
        sb.append(" physical=").append(viewProvider.isPhysical());
        sb.append(" rootCount=").append(files.size());
        for (PsiFile o : files) {
            sb.append("\n  root=").append(o.getLanguage().getID()).append("/").append(o.getClass().getName());
        }
    }
    LOG.error("no PSI for file '" + file.getName() + "'", new Attachment(file.getPresentableUrl(), sb.toString()));
    throw new AssertionError();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) LightVirtualFile(com.intellij.testFramework.LightVirtualFile) Attachment(com.intellij.openapi.diagnostic.Attachment) Document(com.intellij.openapi.editor.Document) FileType(com.intellij.openapi.fileTypes.FileType) LightVirtualFile(com.intellij.testFramework.LightVirtualFile) FileIndexFacade(com.intellij.openapi.roots.FileIndexFacade) NotNull(org.jetbrains.annotations.NotNull)

Example 14 with FileIndexFacade

use of com.intellij.openapi.roots.FileIndexFacade in project intellij-community by JetBrains.

the class ModuleDefaultVcsRootPolicy method getVcsRootFor.

@Override
@Nullable
public VirtualFile getVcsRootFor(@NotNull VirtualFile file) {
    FileIndexFacade indexFacade = PeriodicalTasksCloser.getInstance().safeGetService(myProject, FileIndexFacade.class);
    if (myBaseDir != null && indexFacade.isValidAncestor(myBaseDir, file)) {
        LOG.debug("File " + file + " is under project base dir " + myBaseDir);
        return myBaseDir;
    }
    VirtualFile contentRoot = ProjectRootManager.getInstance(myProject).getFileIndex().getContentRootForFile(file, Registry.is("ide.hide.excluded.files"));
    if (contentRoot != null) {
        LOG.debug("Content root for file " + file + " is " + contentRoot);
        if (contentRoot.isDirectory()) {
            return contentRoot;
        }
        VirtualFile parent = contentRoot.getParent();
        LOG.debug("Content root is not a directory, using its parent " + parent);
        return parent;
    }
    if (ProjectKt.isDirectoryBased(myProject)) {
        VirtualFile ideaDir = ProjectKt.getStateStore(myProject).getDirectoryStoreFile();
        if (ideaDir != null && VfsUtilCore.isAncestor(ideaDir, file, false)) {
            LOG.debug("File " + file + " is under .idea");
            return ideaDir;
        }
    }
    LOG.debug("Couldn't find proper root for " + file);
    return null;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FileIndexFacade(com.intellij.openapi.roots.FileIndexFacade) Nullable(org.jetbrains.annotations.Nullable)

Example 15 with FileIndexFacade

use of com.intellij.openapi.roots.FileIndexFacade in project intellij-community by JetBrains.

the class MappingsToRoots method getDetailedVcsMappings.

// not only set mappings, but include all modules inside: modules might have different settings
public List<VirtualFile> getDetailedVcsMappings(final AbstractVcs vcs) {
    // same as above, but no compression
    final List<VirtualFile> result = myMappings.getMappingsAsFilesUnderVcs(vcs);
    boolean addInnerModules = true;
    final String vcsName = vcs.getName();
    final List<VcsDirectoryMapping> directoryMappings = myMappings.getDirectoryMappings(vcsName);
    for (VcsDirectoryMapping directoryMapping : directoryMappings) {
        if (directoryMapping.isDefaultMapping()) {
            addInnerModules = false;
            break;
        }
    }
    Collections.sort(result, FilePathComparator.getInstance());
    if (addInnerModules) {
        final FileIndexFacade facade = PeriodicalTasksCloser.getInstance().safeGetService(myProject, FileIndexFacade.class);
        final Collection<VirtualFile> modules = DefaultVcsRootPolicy.getInstance(myProject).getDefaultVcsRoots(myMappings, vcsName);
        ApplicationManager.getApplication().runReadAction(() -> {
            Iterator<VirtualFile> iterator = modules.iterator();
            while (iterator.hasNext()) {
                final VirtualFile module = iterator.next();
                boolean included = false;
                for (VirtualFile root : result) {
                    if (facade.isValidAncestor(root, module)) {
                        included = true;
                        break;
                    }
                }
                if (!included) {
                    iterator.remove();
                }
            }
        });
        result.addAll(modules);
    }
    result.removeIf(file -> !file.isDirectory());
    return result;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) VcsDirectoryMapping(com.intellij.openapi.vcs.VcsDirectoryMapping) FileIndexFacade(com.intellij.openapi.roots.FileIndexFacade)

Aggregations

FileIndexFacade (com.intellij.openapi.roots.FileIndexFacade)15 VirtualFile (com.intellij.openapi.vfs.VirtualFile)15 NotNull (org.jetbrains.annotations.NotNull)4 Nullable (org.jetbrains.annotations.Nullable)4 ProcessCanceledException (com.intellij.openapi.progress.ProcessCanceledException)2 LightVirtualFile (com.intellij.testFramework.LightVirtualFile)2 ItemPresentation (com.intellij.navigation.ItemPresentation)1 Attachment (com.intellij.openapi.diagnostic.Attachment)1 Document (com.intellij.openapi.editor.Document)1 FileType (com.intellij.openapi.fileTypes.FileType)1 Module (com.intellij.openapi.module.Module)1 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)1 IndexNotReadyException (com.intellij.openapi.project.IndexNotReadyException)1 Project (com.intellij.openapi.project.Project)1 Computable (com.intellij.openapi.util.Computable)1 Ref (com.intellij.openapi.util.Ref)1 AbstractVcs (com.intellij.openapi.vcs.AbstractVcs)1 VcsDirectoryMapping (com.intellij.openapi.vcs.VcsDirectoryMapping)1 VcsException (com.intellij.openapi.vcs.VcsException)1 PsiElement (com.intellij.psi.PsiElement)1