Search in sources :

Example 6 with ContentIterator

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

the class PsiPackageFavoriteNodeProvider method elementContainsFile.

@Override
public boolean elementContainsFile(final Object element, final VirtualFile vFile) {
    if (element instanceof PackageElement) {
        final Set<Boolean> find = new HashSet<>();
        final ContentIterator contentIterator = new ContentIterator() {

            @Override
            public boolean processFile(VirtualFile fileOrDir) {
                if (fileOrDir != null && fileOrDir.getPath().equals(vFile.getPath())) {
                    find.add(Boolean.TRUE);
                }
                return true;
            }
        };
        final PackageElement packageElement = (PackageElement) element;
        final PsiPackage aPackage = packageElement.getPackage();
        final Project project = aPackage.getProject();
        final GlobalSearchScope scope = packageElement.getModule() != null ? GlobalSearchScope.moduleScope(packageElement.getModule()) : GlobalSearchScope.projectScope(project);
        final ProjectFileIndex projectFileIndex = ProjectRootManager.getInstance(project).getFileIndex();
        final PsiDirectory[] directories = aPackage.getDirectories(scope);
        for (PsiDirectory directory : directories) {
            projectFileIndex.iterateContentUnderDirectory(directory.getVirtualFile(), contentIterator);
        }
        return !find.isEmpty();
    }
    return false;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) ContentIterator(com.intellij.openapi.roots.ContentIterator) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) ProjectFileIndex(com.intellij.openapi.roots.ProjectFileIndex) PsiDirectory(com.intellij.psi.PsiDirectory) PsiPackage(com.intellij.psi.PsiPackage) HashSet(java.util.HashSet)

Example 7 with ContentIterator

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

the class DirectoryIndexRestoreTest method testDeepDeleteAndRecreate.

public void testDeepDeleteAndRecreate() throws IOException {
    AtomicInteger counter = new AtomicInteger(0);
    ContentIterator iterator = (file) -> {
        boolean found = file.getPath().equals(myTestDirPath);
        if (found)
            counter.incrementAndGet();
        return !found;
    };
    File topFile = new File(myTempVFile.getPath(), "top"), bakFile = new File(myTempVFile.getPath(), "top.bak");
    String topPath = myTempVFile.getPath() + "/top";
    myFileIndex.iterateContent(iterator);
    assertEquals(1, counter.get());
    FileUtil.rename(topFile, bakFile);
    List<String> events1 = print(getEvents(() -> myTempVFile.refresh(false, true)));
    assertEquals(singletonList("D : " + topPath), events1);
    myFileIndex.iterateContent(iterator);
    assertEquals(1, counter.get());
    FileUtil.rename(bakFile, topFile);
    List<String> events2 = print(getEvents(() -> myTempVFile.refresh(false, true)));
    assertEquals(singletonList("C : " + topPath), events2);
    myFileIndex.iterateContent(iterator);
    assertEquals(2, counter.get());
}
Also used : VfsTestUtil.getEvents(com.intellij.testFramework.VfsTestUtil.getEvents) PsiTestUtil(com.intellij.testFramework.PsiTestUtil) VirtualFile(com.intellij.openapi.vfs.VirtualFile) PlatformTestCase(com.intellij.testFramework.PlatformTestCase) ContentIterator(com.intellij.openapi.roots.ContentIterator) ProjectFileIndex(com.intellij.openapi.roots.ProjectFileIndex) IOException(java.io.IOException) LocalFileSystem(com.intellij.openapi.vfs.LocalFileSystem) VirtualFileManager(com.intellij.openapi.vfs.VirtualFileManager) File(java.io.File) Collections.singletonList(java.util.Collections.singletonList) List(java.util.List) ModuleRootModificationUtil(com.intellij.openapi.roots.ModuleRootModificationUtil) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IdeaTestCase(com.intellij.testFramework.IdeaTestCase) FileUtil(com.intellij.openapi.util.io.FileUtil) ProjectRootManager(com.intellij.openapi.roots.ProjectRootManager) VfsTestUtil.print(com.intellij.testFramework.VfsTestUtil.print) ContentIterator(com.intellij.openapi.roots.ContentIterator) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File)

Example 8 with ContentIterator

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

the class ExpressionStatisticsAction method collectJavaFiles.

@NotNull
private static List<VirtualFile> collectJavaFiles(VirtualFile dir, Project project) {
    final List<VirtualFile> javaFiles = ContainerUtil.newArrayList();
    ProjectFileIndex.SERVICE.getInstance(project).iterateContentUnderDirectory(dir, new ContentIterator() {

        @Override
        public boolean processFile(VirtualFile file) {
            if (file.getName().endsWith(".java")) {
                javaFiles.add(file);
            }
            return true;
        }
    });
    return javaFiles;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ContentIterator(com.intellij.openapi.roots.ContentIterator) NotNull(org.jetbrains.annotations.NotNull)

Example 9 with ContentIterator

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

the class MatcherImpl method findMatches.

private boolean findMatches(MatchOptions options, CompiledPattern compiledPattern) {
    SearchScope searchScope = compiledPattern.getScope();
    final boolean ourOptimizedScope = searchScope != null;
    if (!ourOptimizedScope)
        searchScope = options.getScope();
    if (searchScope instanceof GlobalSearchScope) {
        final GlobalSearchScope scope = (GlobalSearchScope) searchScope;
        final ContentIterator ci = new ContentIterator() {

            @Override
            public boolean processFile(final VirtualFile fileOrDir) {
                if (!fileOrDir.isDirectory() && scope.contains(fileOrDir) && fileOrDir.getFileType() != FileTypes.UNKNOWN) {
                    ++totalFilesToScan;
                    scheduler.addOneTask(new MatchOneVirtualFile(fileOrDir));
                }
                return true;
            }
        };
        ApplicationManager.getApplication().runReadAction(() -> FileBasedIndex.getInstance().iterateIndexableFiles(ci, project, progress));
        progress.setText2("");
    } else {
        final PsiElement[] elementsToScan = ((LocalSearchScope) searchScope).getScope();
        totalFilesToScan = elementsToScan.length;
        for (int i = 0; i < elementsToScan.length; ++i) {
            final PsiElement psiElement = elementsToScan[i];
            if (psiElement == null)
                continue;
            scheduler.addOneTask(new MatchOnePsiFile(psiElement));
            // to prevent long PsiElement reference
            if (ourOptimizedScope)
                elementsToScan[i] = null;
        }
    }
    return true;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) LocalSearchScope(com.intellij.psi.search.LocalSearchScope) ContentIterator(com.intellij.openapi.roots.ContentIterator) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) SearchScope(com.intellij.psi.search.SearchScope) LocalSearchScope(com.intellij.psi.search.LocalSearchScope) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope)

Example 10 with ContentIterator

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

the class LightProjectDescriptor method registerSourceRoot.

protected void registerSourceRoot(Project project, VirtualFile srcRoot) {
    IndexableFileSet indexableFileSet = new IndexableFileSet() {

        @Override
        public boolean isInSet(@NotNull VirtualFile file) {
            return file.getFileSystem() == srcRoot.getFileSystem() && project.isOpen();
        }

        @Override
        public void iterateIndexableFilesIn(@NotNull VirtualFile file, @NotNull ContentIterator iterator) {
            VfsUtilCore.visitChildrenRecursively(file, new VirtualFileVisitor() {

                @Override
                public boolean visitFile(@NotNull VirtualFile file) {
                    iterator.processFile(file);
                    return true;
                }
            });
        }
    };
    FileBasedIndex.getInstance().registerIndexableSet(indexableFileSet, null);
    Disposer.register(project, () -> FileBasedIndex.getInstance().removeIndexableSet(indexableFileSet));
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ContentIterator(com.intellij.openapi.roots.ContentIterator) IndexableFileSet(com.intellij.util.indexing.IndexableFileSet) VirtualFileVisitor(com.intellij.openapi.vfs.VirtualFileVisitor) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

ContentIterator (com.intellij.openapi.roots.ContentIterator)20 VirtualFile (com.intellij.openapi.vfs.VirtualFile)19 NotNull (org.jetbrains.annotations.NotNull)8 Project (com.intellij.openapi.project.Project)6 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)4 IOException (java.io.IOException)4 ProjectFileIndex (com.intellij.openapi.roots.ProjectFileIndex)3 ProjectRootManager (com.intellij.openapi.roots.ProjectRootManager)3 File (java.io.File)3 Nullable (org.jetbrains.annotations.Nullable)3 Module (com.intellij.openapi.module.Module)2 ModuleFileIndex (com.intellij.openapi.roots.ModuleFileIndex)2 ModuleRootManager (com.intellij.openapi.roots.ModuleRootManager)2 Condition (com.intellij.openapi.util.Condition)2 FileUtil (com.intellij.openapi.util.io.FileUtil)2 LocalFileSystem (com.intellij.openapi.vfs.LocalFileSystem)2 VirtualFileManager (com.intellij.openapi.vfs.VirtualFileManager)2 VirtualFileVisitor (com.intellij.openapi.vfs.VirtualFileVisitor)2 VirtualFileWithId (com.intellij.openapi.vfs.VirtualFileWithId)2 PsiDirectory (com.intellij.psi.PsiDirectory)2