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();
}
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;
}
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());
}
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;
}
});
}
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();
}
}
Aggregations