com.intellij.openapi.roots.ContentIterator Java Examples

The following examples show how to use com.intellij.openapi.roots.ContentIterator. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ShowAllProblemsInProjectAction.java    From CppTools with Apache License 2.0 6 votes vote down vote up
MyBlockingCommand(Project project) {
  this(0, project, new ArrayList<VirtualFile>(), new LinkedHashMap<String, VirtualFile>());

  final ProjectFileIndex moduleFileIndex = ProjectRootManager.getInstance(project).getFileIndex();
  moduleFileIndex.iterateContent(new ContentIterator() {
    public boolean processFile(VirtualFile virtualFile) {
      if (!virtualFile.isDirectory() &&
          virtualFile.getFileType() == CppSupportLoader.CPP_FILETYPE &&
          CppSupportLoader.isInSource(virtualFile, moduleFileIndex)
         ) {
        filesToProcess.add(virtualFile);
        String key = virtualFile.getPath();
        if (bug373) key = key.toLowerCase();
        nameToFilesMap.put(key, virtualFile);
      }
      return true;
    }
  });
}
 
Example #2
Source File: IntelliUtils.java    From floobits-intellij with Apache License 2.0 6 votes vote down vote up
public static ArrayList<String> getAllNestedFilePaths(VirtualFile vFile) {
    final ArrayList<String> filePaths = new ArrayList<String>();
    if (!vFile.isDirectory()) {
        filePaths.add(vFile.getPath());
        return filePaths;
    }
    VfsUtil.iterateChildrenRecursively(vFile, null, new ContentIterator() {
        @Override
        public boolean processFile(VirtualFile file) {
            if (!file.isDirectory()) {
                filePaths.add(file.getPath());
            }
            return true;
        }
    });
    return filePaths;
}
 
Example #3
Source File: FileTreeModelBuilder.java    From consulo with Apache License 2.0 6 votes vote down vote up
private void countFiles(Project project) {
  final Integer fileCount = project.getUserData(FILE_COUNT);
  if (fileCount == null) {
    myFileIndex.iterateContent(new ContentIterator() {
      @Override
      public boolean processFile(VirtualFile fileOrDir) {
        if (!fileOrDir.isDirectory()) {
          counting();
        }
        return true;
      }
    });
    project.putUserData(FILE_COUNT, myTotalFileCount);
  } else {
    myTotalFileCount = fileCount.intValue();
  }
}
 
Example #4
Source File: AbstractFileProcessor.java    From consulo with Apache License 2.0 6 votes vote down vote up
protected static void findFiles(final Module module, final List<PsiFile> files) {
  final ModuleFileIndex idx = ModuleRootManager.getInstance(module).getFileIndex();

  final VirtualFile[] roots = ModuleRootManager.getInstance(module).getContentRoots();

  for (VirtualFile root : roots) {
    idx.iterateContentUnderDirectory(root, new ContentIterator() {
      public boolean processFile(final VirtualFile dir) {
        if (dir.isDirectory()) {
          final PsiDirectory psiDir = PsiManager.getInstance(module.getProject()).findDirectory(dir);
          if (psiDir != null) {
            findFiles(files, psiDir, false);
          }
        }
        return true;
      }
    });
  }
}
 
Example #5
Source File: HaxeUtil.java    From intellij-haxe with Apache License 2.0 6 votes vote down vote up
public static void reparseProjectFiles(@NotNull final Project project) {
  Task.Backgroundable task = new Task.Backgroundable(project, HaxeBundle.message("haxe.project.reparsing"), false) {
    public void run(@NotNull ProgressIndicator indicator) {
      final Collection<VirtualFile> haxeFiles = new ArrayList<VirtualFile>();
      final VirtualFile baseDir = project.getBaseDir();
      if (baseDir != null) {
        FileBasedIndex.getInstance().iterateIndexableFiles(new ContentIterator() {
          public boolean processFile(VirtualFile file) {
            if (HaxeFileType.HAXE_FILE_TYPE == file.getFileType()) {
              haxeFiles.add(file);
            }
            return true;
          }
        }, project, indicator);
      }
      ApplicationManager.getApplication().invokeAndWait(new Runnable() {
        public void run() {
          FileContentUtil.reparseFiles(project, haxeFiles, !project.isDefault());
        }
      }, ModalityState.NON_MODAL);
    }
  };
  ProgressManager.getInstance().run(task);
}
 
Example #6
Source File: IdFilter.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
private static IdFilter buildProjectIdFilter(Project project, boolean includeNonProjectItems) {
  long started = System.currentTimeMillis();
  final BitSet idSet = new BitSet();

  ContentIterator iterator = fileOrDir -> {
    idSet.set(((VirtualFileWithId)fileOrDir).getId());
    ProgressManager.checkCanceled();
    return true;
  };

  if (!includeNonProjectItems) {
    ProjectRootManager.getInstance(project).getFileIndex().iterateContent(iterator);
  }
  else {
    FileBasedIndex.getInstance().iterateIndexableFiles(iterator, project, ProgressIndicatorProvider.getGlobalProgressIndicator());
  }

  if (LOG.isDebugEnabled()) {
    long elapsed = System.currentTimeMillis() - started;
    LOG.debug("Done filter (includeNonProjectItems=" + includeNonProjectItems + ") " + "in " + elapsed + "ms. Total files in set: " + idSet.cardinality());
  }
  return new IdFilter() {
    @Override
    public boolean containsFileId(int id) {
      return id >= 0 && idSet.get(id);
    }

    @Nonnull
    @Override
    public GlobalSearchScope getEffectiveFilteringScope() {
      return includeNonProjectItems ? GlobalSearchScope.allScope(project) : GlobalSearchScope.projectScope(project);
    }
  };
}
 
Example #7
Source File: IndexedFilesListener.java    From consulo with Apache License 2.0 5 votes vote down vote up
protected void buildIndicesForFileRecursively(@Nonnull final VirtualFile file, final boolean contentChange) {
  if (file.isDirectory()) {
    final ContentIterator iterator = fileOrDir -> {
      myEventMerger.recordFileEvent(fileOrDir, contentChange);
      return true;
    };

    iterateIndexableFiles(file, iterator);
  }
  else {
    myEventMerger.recordFileEvent(file, contentChange);
  }
}
 
Example #8
Source File: FileBasedIndexImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
protected void iterateIndexableFiles(@Nonnull VirtualFile file, @Nonnull ContentIterator iterator) {
  for (IndexableFileSet set : myManager.myIndexableSets) {
    if (set.isInSet(file)) {
      set.iterateIndexableFilesIn(file, iterator);
    }
  }
}
 
Example #9
Source File: DumpDirectoryInfoAction.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void actionPerformed(AnActionEvent e) {
  final Project project = e.getData(CommonDataKeys.PROJECT);
  final DirectoryIndex index = DirectoryIndex.getInstance(project);
  if (project != null) {
    final VirtualFile root = e.getData(PlatformDataKeys.VIRTUAL_FILE);
    ProgressManager.getInstance().runProcessWithProgressSynchronously(new Runnable() {
      @Override
      public void run() {
        final ContentIterator contentIterator = new ContentIterator() {
          @Override
          public boolean processFile(VirtualFile fileOrDir) {
            LOG.info(fileOrDir.getPath());

            final DirectoryInfo directoryInfo = index.getInfoForDirectory(fileOrDir);
            if (directoryInfo != null) {
              LOG.info(directoryInfo.toString());
            }
            return true;
          }
        };
        if (root != null) {
          ProjectRootManager.getInstance(project).getFileIndex().iterateContentUnderDirectory(root, contentIterator);
        } else {
          ProjectRootManager.getInstance(project).getFileIndex().iterateContent(contentIterator);
        }
      }
    }, "Dumping directory index", true, project);
  }
}
 
Example #10
Source File: LoadAllContentsAction.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void actionPerformed(AnActionEvent e) {
  final Project project = e.getDataContext().getData(CommonDataKeys.PROJECT);
  String m = "Started loading content";
  LOG.info(m);
  System.out.println(m);
  long start = System.currentTimeMillis();
  count.set(0);
  totalSize.set(0);
  ApplicationManagerEx.getApplicationEx().runProcessWithProgressSynchronously(new Runnable() {
    @Override
    public void run() {
      ProjectRootManager.getInstance(project).getFileIndex().iterateContent(new ContentIterator() {
        @Override
        public boolean processFile(VirtualFile fileOrDir) {
          if (fileOrDir.isDirectory() || fileOrDir.is(VFileProperty.SPECIAL)) return true;
          try {
            count.incrementAndGet();
            byte[] bytes = FileUtil.loadFileBytes(new File(fileOrDir.getPath()));
            totalSize.addAndGet(bytes.length);
            ProgressManager.getInstance().getProgressIndicator().setText(fileOrDir.getPresentableUrl());
          }
          catch (IOException e1) {
            LOG.error(e1);
          }
          return true;
        }
      });
     }
  }, "Loading", false, project);
  long end = System.currentTimeMillis();
  String message = "Finished loading content of " + count + " files. Total size=" + StringUtil.formatFileSize(totalSize.get()) +
                   ". Elapsed=" + ((end - start) / 1000) + "sec.";
  LOG.info(message);
  System.out.println(message);
}
 
Example #11
Source File: FileUtil.java    From idea-php-advanced-autocomplete with MIT License 5 votes vote down vote up
public static String[] getRelativeFiles(PsiFile baseFile)
    {
        final List<String> files = new ArrayList<String>();
        Project project = baseFile.getProject();
        final VirtualFile projectDirectory = project.getBaseDir();
        PsiDirectory directory = baseFile.getContainingDirectory();

        if(directory == null)
            return null;

        final VirtualFile originDirectory = directory.getVirtualFile();

        ProjectFileIndex fileIndex = ProjectFileIndex.SERVICE.getInstance(project);

/*
        for(VirtualFile file : originDirectory.getChildren()) {
            files.add(file.getPath().replace(projectDirectory.getPath() + "/", ""));
        }
*/

        fileIndex.iterateContentUnderDirectory(originDirectory, new ContentIterator() {
                @Override
                public boolean processFile(VirtualFile file) {
                    //files.add(file.getName());
                    if(!file.isDirectory()) {
                        files.add(file.getPath().replace(originDirectory.getPath() + "/", ""));
                    }

                    return true;
                }
            }
        );

        return files.toArray(new String[files.size()]);
    }
 
Example #12
Source File: OneProjectItemCompileScope.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
public VirtualFile[] getFiles(final FileType fileType, final boolean inSourceOnly) {
  final List<VirtualFile> files = new ArrayList<VirtualFile>(1);
  final FileIndex projectFileIndex = ProjectRootManager.getInstance(myProject).getFileIndex();
  final ContentIterator iterator = new CompilerContentIterator(fileType, projectFileIndex, inSourceOnly, files);
  if (myFile.isDirectory()){
    projectFileIndex.iterateContentUnderDirectory(myFile, iterator);
  }
  else{
    iterator.processFile(myFile);
  }
  return VfsUtil.toVirtualFileArray(files);
}
 
Example #13
Source File: ProjectFileIndexImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public boolean iterateContent(@Nonnull ContentIterator processor, @Nullable VirtualFileFilter filter) {
  Module[] modules = AccessRule.read(() -> ModuleManager.getInstance(myProject).getModules());
  for (final Module module : modules) {
    for (VirtualFile contentRoot : getRootsToIterate(module)) {
      if (!iterateContentUnderDirectory(contentRoot, processor, filter)) {
        return false;
      }
    }
  }
  return true;
}
 
Example #14
Source File: VfsUtilCore.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static boolean iterateChildrenRecursively(@Nonnull final VirtualFile root, @Nullable final VirtualFileFilter filter, @Nonnull final ContentIterator iterator) {
  final VirtualFileVisitor.Result result = visitChildrenRecursively(root, new VirtualFileVisitor() {
    @Nonnull
    @Override
    public Result visitFileEx(@Nonnull VirtualFile file) {
      if (filter != null && !filter.accept(file)) return SKIP_CHILDREN;
      if (!iterator.processFile(file)) return skipTo(root);
      return CONTINUE;
    }
  });
  return !Comparing.equal(result.skipToParent, root);
}
 
Example #15
Source File: LocalFilesystemModificationHandler.java    From saros with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Generates and dispatches a folder deletion activity for the deleted folder.
 *
 * <p>Also explicitly creates deletion activities for all contained (non-excluded) resources.
 * Contained files are processed first (in ascending depth order), followed by the contained
 * folders in descending depth order. This ensures that deletion activities for folders are only
 * send once the activities for all contained resources were sent, allowing the receiver to also
 * explicitly handle all resource deletions.
 *
 * @param deletedFolder the folder that was deleted
 */
private void generateFolderDeletionActivity(@NotNull VirtualFile deletedFolder) {
  Set<IReferencePoint> sharedReferencePoints = session.getReferencePoints();

  IContainer container =
      (IContainer) VirtualFileConverter.convertToResource(sharedReferencePoints, deletedFolder);

  if (container == null || !session.isShared(container)) {
    if (log.isTraceEnabled()) {
      log.trace("Ignoring non-shared folder deletion: " + deletedFolder);
    }

    return;

  } else if (container.getType() == REFERENCE_POINT) {
    log.error(
        "Local representation of reference point "
            + container
            + " was deleted. Leaving the session.");

    NotificationPanel.showError(
        MessageFormat.format(
            Messages.LocalFilesystemModificationHandler_deleted_reference_point_message,
            deletedFolder.getPath()),
        Messages.LocalFilesystemModificationHandler_deleted_reference_point_title);

    CollaborationUtils.leaveSessionUnconditionally();

    return;
  }

  IReferencePoint baseReferencePoint = container.getReferencePoint();

  User user = session.getLocalUser();

  Deque<IActivity> queuedDeletionActivities = new ConcurrentLinkedDeque<>();

  VirtualFileFilter virtualFileFilter = getVirtualFileFilter();

  /*
   * Defines the actions executed on the base directory and every valid
   * child resource (defined through the virtualFileFilter).
   *
   * Returns whether the content iteration should be continued.
   */
  ContentIterator contentIterator =
      fileOrDir -> {
        if (!fileOrDir.isDirectory()) {
          generateFileDeletionActivity(fileOrDir);

          return true;
        }

        IFolder childFolder =
            (IFolder) VirtualFileConverter.convertToResource(fileOrDir, baseReferencePoint);

        if (childFolder == null || !session.isShared(childFolder)) {
          log.debug(
              "Ignoring non-shared child folder deletion: "
                  + fileOrDir
                  + ", parent folder: "
                  + container);

          return true;
        }

        IActivity newFolderDeletedActivity = new FolderDeletedActivity(user, childFolder);

        queuedDeletionActivities.addFirst(newFolderDeletedActivity);

        return true;
      };

  /*
   * Calls the above defined contentIterator on the base directory and every contained resource.
   * Directories are only stepped into if they match the above defined virtualFileFilter. This
   * also applies to the base directory.
   */
  VfsUtilCore.iterateChildrenRecursively(deletedFolder, virtualFileFilter, contentIterator);

  while (!queuedDeletionActivities.isEmpty()) {
    dispatchActivity(queuedDeletionActivities.pop());
  }
}
 
Example #16
Source File: FileBasedIndex.java    From consulo with Apache License 2.0 4 votes vote down vote up
public void iterateIndexableFilesConcurrently(@Nonnull ContentIterator processor, @Nonnull Project project, @Nonnull ProgressIndicator indicator) {
  iterateIndexableFiles(processor, project, indicator);
}
 
Example #17
Source File: FileIndexBase.java    From consulo with Apache License 2.0 4 votes vote down vote up
private static boolean iterateContentUnderDirectoryWithFilter(@Nonnull VirtualFile dir, @Nonnull ContentIterator iterator, @Nonnull VirtualFileFilter filter) {
  return VfsUtilCore.iterateChildrenRecursively(dir, filter, iterator);
}
 
Example #18
Source File: FileIndexBase.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public boolean iterateContentUnderDirectory(@Nonnull VirtualFile dir, @Nonnull ContentIterator processor) {
  return iterateContentUnderDirectory(dir, processor, null);
}
 
Example #19
Source File: ScanSourceCommentsAction.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public void actionPerformed(AnActionEvent e) {

  final Project p = e.getDataContext().getData(CommonDataKeys.PROJECT);
  final String file =
    Messages.showInputDialog(p, "Enter path to the file comments will be extracted to", "Comments File Path", Messages.getQuestionIcon());

  try {
    final PrintStream stream = new PrintStream(file);
    stream.println("Comments in " + p.getName());

    ProgressManager.getInstance().runProcessWithProgressSynchronously(new Runnable() {
      @Override
      public void run() {
        final ProgressIndicator indicator = ProgressManager.getInstance().getProgressIndicator();
        ProjectRootManager.getInstance(p).getFileIndex().iterateContent(new ContentIterator() {
          @Override
          public boolean processFile(VirtualFile fileOrDir) {
            if (fileOrDir.isDirectory()) {
              indicator.setText("Extracting comments");
              indicator.setText2(fileOrDir.getPresentableUrl());
            }
            scanCommentsInFile(p, fileOrDir);
            return true;
          }
        });

        indicator.setText2("");
        int count = 1;
        for (CommentDescriptor descriptor : myComments.values()) {
          stream.println("#" + count + " ---------------------------------------------------------------");
          descriptor.print(stream);
          stream.println();
          count++;
        }

      }
    }, "Generating Comments", true, p);


    stream.close();

  }
  catch (Throwable e1) {
    LOG.error(e1);
    Messages.showErrorDialog(p, "Error writing? " + e1.getMessage(), "Problem writing");
  }
}
 
Example #20
Source File: FileIndexBase.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public boolean iterateContentUnderDirectory(@Nonnull VirtualFile dir, @Nonnull ContentIterator processor, @Nullable VirtualFileFilter customFilter) {
  VirtualFileFilter filter = customFilter != null ? file -> myContentFilter.accept(file) && customFilter.accept(file) : myContentFilter;
  return iterateContentUnderDirectoryWithFilter(dir, processor, filter);
}
 
Example #21
Source File: FileBasedIndexImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public void iterateIndexableFilesConcurrently(@Nonnull ContentIterator processor, @Nonnull Project project, @Nonnull ProgressIndicator indicator) {
  PushedFilePropertiesUpdaterImpl.invokeConcurrentlyIfPossible(collectScanRootRunnables(processor, project, indicator));
}
 
Example #22
Source File: FileBasedIndexImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public void iterateIndexableFiles(@Nonnull final ContentIterator processor, @Nonnull final Project project, final ProgressIndicator indicator) {
  for (Runnable r : collectScanRootRunnables(processor, project, indicator)) r.run();
}
 
Example #23
Source File: FileBasedIndexImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nonnull
private static List<Runnable> collectScanRootRunnables(@Nonnull final ContentIterator processor, @Nonnull final Project project, final ProgressIndicator indicator) {
  FileBasedIndexScanRunnableCollector collector = FileBasedIndexScanRunnableCollector.getInstance(project);
  return collector.collectScanRootRunnables(processor, indicator);
}
 
Example #24
Source File: FileIndexBase.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public boolean iterateContent(@Nonnull ContentIterator processor) {
  return iterateContent(processor, null);
}
 
Example #25
Source File: FileBasedIndexScanRunnableCollector.java    From consulo with Apache License 2.0 votes vote down vote up
public abstract List<Runnable> collectScanRootRunnables(@Nonnull final ContentIterator processor, final ProgressIndicator indicator); 
Example #26
Source File: IndexedFilesListener.java    From consulo with Apache License 2.0 votes vote down vote up
protected abstract void iterateIndexableFiles(@Nonnull VirtualFile file, @Nonnull ContentIterator iterator); 
Example #27
Source File: FileBasedIndex.java    From consulo with Apache License 2.0 votes vote down vote up
public abstract void iterateIndexableFiles(@Nonnull ContentIterator processor, @Nonnull Project project, ProgressIndicator indicator); 
Example #28
Source File: IndexableFileSet.java    From consulo with Apache License 2.0 votes vote down vote up
void iterateIndexableFilesIn(@Nonnull VirtualFile file, @Nonnull ContentIterator iterator);