Search in sources :

Example 16 with VirtualFileVisitor

use of com.intellij.openapi.vfs.VirtualFileVisitor in project intellij-community by JetBrains.

the class FindInProjectTask method collectFilesInScope.

// must return non-binary files
@NotNull
private Collection<VirtualFile> collectFilesInScope(@NotNull final Set<VirtualFile> alreadySearched, final boolean skipIndexed) {
    SearchScope customScope = myFindModel.isCustomScope() ? myFindModel.getCustomScope() : null;
    final GlobalSearchScope globalCustomScope = customScope == null ? null : GlobalSearchScopeUtil.toGlobalSearchScope(customScope, myProject);
    final ProjectFileIndex fileIndex = ProjectFileIndex.SERVICE.getInstance(myProject);
    final boolean hasTrigrams = hasTrigrams(myStringToFindInIndices);
    class EnumContentIterator implements ContentIterator {

        private final Set<VirtualFile> myFiles = new LinkedHashSet<>();

        @Override
        public boolean processFile(@NotNull final VirtualFile virtualFile) {
            ApplicationManager.getApplication().runReadAction(new Runnable() {

                @Override
                public void run() {
                    ProgressManager.checkCanceled();
                    if (virtualFile.isDirectory() || !virtualFile.isValid() || !myFileMask.value(virtualFile) || globalCustomScope != null && !globalCustomScope.contains(virtualFile)) {
                        return;
                    }
                    if (skipIndexed && isCoveredByIndex(virtualFile) && (fileIndex.isInContent(virtualFile) || fileIndex.isInLibraryClasses(virtualFile) || fileIndex.isInLibrarySource(virtualFile))) {
                        return;
                    }
                    Pair.NonNull<PsiFile, VirtualFile> pair = findFile(virtualFile);
                    if (pair == null)
                        return;
                    VirtualFile sourceVirtualFile = pair.second;
                    if (sourceVirtualFile != null && !alreadySearched.contains(sourceVirtualFile)) {
                        myFiles.add(sourceVirtualFile);
                    }
                }

                private final FileBasedIndexImpl fileBasedIndex = (FileBasedIndexImpl) FileBasedIndex.getInstance();

                private boolean isCoveredByIndex(VirtualFile file) {
                    FileType fileType = file.getFileType();
                    if (hasTrigrams) {
                        return TrigramIndex.isIndexable(fileType) && fileBasedIndex.isIndexingCandidate(file, TrigramIndex.INDEX_ID);
                    }
                    return IdIndex.isIndexable(fileType) && fileBasedIndex.isIndexingCandidate(file, IdIndex.NAME);
                }
            });
            return true;
        }

        @NotNull
        private Collection<VirtualFile> getFiles() {
            return myFiles;
        }
    }
    final EnumContentIterator iterator = new EnumContentIterator();
    if (customScope instanceof LocalSearchScope) {
        for (VirtualFile file : GlobalSearchScopeUtil.getLocalScopeFiles((LocalSearchScope) customScope)) {
            iterator.processFile(file);
        }
    } else if (customScope instanceof Iterable) {
        //noinspection unchecked
        for (VirtualFile file : (Iterable<VirtualFile>) customScope) {
            iterator.processFile(file);
        }
    } else if (myDirectory != null) {
        final boolean checkExcluded = !ProjectFileIndex.SERVICE.getInstance(myProject).isExcluded(myDirectory);
        VirtualFileVisitor.Option limit = VirtualFileVisitor.limit(myFindModel.isWithSubdirectories() ? -1 : 1);
        VfsUtilCore.visitChildrenRecursively(myDirectory, new VirtualFileVisitor(limit) {

            @Override
            public boolean visitFile(@NotNull VirtualFile file) {
                if (checkExcluded && myProjectFileIndex.isExcluded(file))
                    return false;
                iterator.processFile(file);
                return true;
            }
        });
    } else {
        boolean success = myFileIndex.iterateContent(iterator);
        if (success && globalCustomScope != null && globalCustomScope.isSearchInLibraries()) {
            final VirtualFile[] librarySources = ReadAction.compute(() -> {
                OrderEnumerator enumerator = myModule == null ? OrderEnumerator.orderEntries(myProject) : OrderEnumerator.orderEntries(myModule);
                return enumerator.withoutModuleSourceEntries().withoutDepModules().getSourceRoots();
            });
            iterateAll(librarySources, globalCustomScope, iterator);
        }
    }
    return iterator.getFiles();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) VirtualFileVisitor(com.intellij.openapi.vfs.VirtualFileVisitor) NotNull(org.jetbrains.annotations.NotNull) FileType(com.intellij.openapi.fileTypes.FileType) FileBasedIndexImpl(com.intellij.util.indexing.FileBasedIndexImpl) NotNull(org.jetbrains.annotations.NotNull)

Example 17 with VirtualFileVisitor

use of com.intellij.openapi.vfs.VirtualFileVisitor in project intellij-community by JetBrains.

the class LightJavaModule method getExports.

@NotNull
@Override
public Iterable<PsiPackageAccessibilityStatement> getExports() {
    if (myExports == null) {
        List<PsiPackageAccessibilityStatement> exports = ContainerUtil.newArrayList();
        VfsUtilCore.visitChildrenRecursively(myJarRoot, new VirtualFileVisitor() {

            private JavaDirectoryService service = JavaDirectoryService.getInstance();

            @Override
            public boolean visitFile(@NotNull VirtualFile file) {
                if (file.isDirectory() && !myJarRoot.equals(file)) {
                    PsiDirectory directory = myManager.findDirectory(file);
                    if (directory != null) {
                        PsiPackage pkg = service.getPackage(directory);
                        if (pkg != null) {
                            String packageName = pkg.getQualifiedName();
                            if (!packageName.isEmpty() && !PsiUtil.isPackageEmpty(new PsiDirectory[] { directory }, packageName)) {
                                exports.add(new LightPackageAccessibilityStatement(myManager, packageName));
                            }
                        }
                    }
                }
                return true;
            }
        });
        myExports = exports;
    }
    return myExports;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) VirtualFileVisitor(com.intellij.openapi.vfs.VirtualFileVisitor) NotNull(org.jetbrains.annotations.NotNull)

Example 18 with VirtualFileVisitor

use of com.intellij.openapi.vfs.VirtualFileVisitor in project intellij-community by JetBrains.

the class MagicConstantInspectionTest method setupRootModel.

@Override
protected void setupRootModel(@NotNull String testDir, @NotNull VirtualFile[] sourceDir, String sdkName) {
    super.setupRootModel(testDir, sourceDir, sdkName);
    VirtualFile projectDir = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(new File(testDir));
    // allow to load AST for all files to highlight
    VfsUtilCore.visitChildrenRecursively(projectDir, new VirtualFileVisitor() {

        @Override
        public boolean visitFile(@NotNull VirtualFile v) {
            myFilter.allowTreeAccessForFile(v);
            return super.visitFile(v);
        }
    });
    // and JFrame
    PsiClass cls = JavaPsiFacade.getInstance(getProject()).findClass("javax.swing.JFrame", GlobalSearchScope.allScope(getProject()));
    PsiClass aClass = (PsiClass) cls.getNavigationElement();
    // must to have sources
    assertTrue(aClass instanceof PsiClassImpl);
    myFilter.allowTreeAccessForFile(aClass.getContainingFile().getVirtualFile());
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiClass(com.intellij.psi.PsiClass) PsiClassImpl(com.intellij.psi.impl.source.PsiClassImpl) VirtualFileVisitor(com.intellij.openapi.vfs.VirtualFileVisitor) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File)

Example 19 with VirtualFileVisitor

use of com.intellij.openapi.vfs.VirtualFileVisitor in project intellij-community by JetBrains.

the class CommonRefactoringUtil method collectReadOnlyFiles.

public static void collectReadOnlyFiles(@NotNull VirtualFile vFile, @NotNull final Collection<VirtualFile> list) {
    final FileTypeManager fileTypeManager = FileTypeManager.getInstance();
    VfsUtilCore.visitChildrenRecursively(vFile, new VirtualFileVisitor(VirtualFileVisitor.NO_FOLLOW_SYMLINKS) {

        @Override
        public boolean visitFile(@NotNull VirtualFile file) {
            final boolean ignored = fileTypeManager.isFileIgnored(file);
            if (!file.isWritable() && !ignored) {
                list.add(file);
            }
            return !ignored;
        }
    });
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FileTypeManager(com.intellij.openapi.fileTypes.FileTypeManager) VirtualFileVisitor(com.intellij.openapi.vfs.VirtualFileVisitor)

Example 20 with VirtualFileVisitor

use of com.intellij.openapi.vfs.VirtualFileVisitor in project intellij-community by JetBrains.

the class DirectoryDataTest method testVisitRecursively.

@Test
public void testVisitRecursively() throws Exception {
    DirectoryData data = new DirectoryData(myProject.getBaseDir());
    try {
        data.clear();
        data.create();
        File subDir = new File(data.getBase().getPath(), "DL0N1");
        final VirtualFile vSubDir = LocalFileSystem.getInstance().findFileByIoFile(subDir);
        assertNotNull(vSubDir);
        VfsUtilCore.visitChildrenRecursively(data.getBase(), new VirtualFileVisitor() {

            @Override
            public boolean visitFile(@NotNull VirtualFile file) {
                assertTrue(!VfsUtilCore.isAncestor(vSubDir, file, true));
                return !vSubDir.equals(file);
            }
        });
    } finally {
        data.clear();
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) VirtualFileVisitor(com.intellij.openapi.vfs.VirtualFileVisitor) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) Test(org.junit.Test)

Aggregations

VirtualFile (com.intellij.openapi.vfs.VirtualFile)23 VirtualFileVisitor (com.intellij.openapi.vfs.VirtualFileVisitor)23 NotNull (org.jetbrains.annotations.NotNull)12 FileTypeManager (com.intellij.openapi.fileTypes.FileTypeManager)4 File (java.io.File)4 FileType (com.intellij.openapi.fileTypes.FileType)3 IOException (java.io.IOException)3 ProcessCanceledException (com.intellij.openapi.progress.ProcessCanceledException)2 ArrayList (java.util.ArrayList)2 Nullable (org.jetbrains.annotations.Nullable)2 Result (com.intellij.openapi.application.Result)1 PatchApplier (com.intellij.openapi.diff.impl.patch.formove.PatchApplier)1 FileTypeRegistry (com.intellij.openapi.fileTypes.FileTypeRegistry)1 Module (com.intellij.openapi.module.Module)1 ModuleImpl (com.intellij.openapi.module.impl.ModuleImpl)1 Project (com.intellij.openapi.project.Project)1 Sdk (com.intellij.openapi.projectRoots.Sdk)1 SdkModificator (com.intellij.openapi.projectRoots.SdkModificator)1 ContentIterator (com.intellij.openapi.roots.ContentIterator)1 ModifiableRootModel (com.intellij.openapi.roots.ModifiableRootModel)1