Java Code Examples for com.intellij.openapi.roots.ProjectFileIndex#isInContent()

The following examples show how to use com.intellij.openapi.roots.ProjectFileIndex#isInContent() . 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: IdeaGateway.java    From consulo with Apache License 2.0 6 votes vote down vote up
public boolean isVersioned(@Nonnull VirtualFile f, boolean shouldBeInContent) {
  if (!f.isInLocalFileSystem()) return false;

  if (!f.isDirectory() && StringUtil.endsWith(f.getNameSequence(), ".class")) return false;

  VersionedFilterData versionedFilterData = getVersionedFilterData();

  boolean isInContent = false;
  int numberOfOpenProjects = versionedFilterData.myOpenedProjects.size();
  for (int i = 0; i < numberOfOpenProjects; ++i) {
    if (f.equals(versionedFilterData.myWorkspaceFiles.get(i))) return false;
    ProjectFileIndex index = versionedFilterData.myProjectFileIndices.get(i);

    if (index.isExcluded(f)) return false;
    isInContent |= index.isInContent(f);
  }
  if (shouldBeInContent && !isInContent) return false;

  // optimisation: FileTypeManager.isFileIgnored(f) already checked inside ProjectFileIndex.isIgnored()
  return numberOfOpenProjects != 0 || !FileTypeManager.getInstance().isFileIgnored(f);
}
 
Example 2
Source File: HighlightingSettingsPerFile.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public boolean shouldInspect(@Nonnull PsiElement psiRoot) {
  if (ApplicationManager.getApplication().isUnitTestMode()) return true;

  if (!shouldHighlight(psiRoot)) return false;
  final Project project = psiRoot.getProject();
  final VirtualFile virtualFile = psiRoot.getContainingFile().getVirtualFile();
  if (virtualFile == null || !virtualFile.isValid()) return false;

  if (ProjectCoreUtil.isProjectOrWorkspaceFile(virtualFile)) return false;

  final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex();
  if (ProjectScope.getLibrariesScope(project).contains(virtualFile) && !fileIndex.isInContent(virtualFile)) return false;

  if (SingleRootFileViewProvider.isTooLargeForIntelligence(virtualFile)) return false;

  final FileHighlightingSetting settingForRoot = getHighlightingSettingForRoot(psiRoot);
  return settingForRoot != FileHighlightingSetting.SKIP_INSPECTION;
}
 
Example 3
Source File: HighlightLevelUtil.java    From consulo with Apache License 2.0 6 votes vote down vote up
public static boolean shouldInspect(@Nonnull PsiElement psiRoot) {
  if (ApplicationManager.getApplication().isUnitTestMode()) return true;

  if (!shouldHighlight(psiRoot)) return false;
  final Project project = psiRoot.getProject();
  final VirtualFile virtualFile = psiRoot.getContainingFile().getVirtualFile();
  if (virtualFile == null || !virtualFile.isValid()) return false;

  if (ProjectCoreUtil.isProjectOrWorkspaceFile(virtualFile)) return false;

  final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex();
  if (ProjectScope.getLibrariesScope(project).contains(virtualFile) && !fileIndex.isInContent(virtualFile)) return false;

  if (SingleRootFileViewProvider.isTooLargeForIntelligence(virtualFile)) return false;

  final HighlightingSettingsPerFile component = HighlightingSettingsPerFile.getInstance(project);
  if (component == null) return true;

  final FileHighlightingSetting settingForRoot = component.getHighlightingSettingForRoot(psiRoot);
  return settingForRoot != FileHighlightingSetting.SKIP_INSPECTION;
}
 
Example 4
Source File: DependenciesPanel.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nullable
private AnalysisScope getScope() {
  final Set<PsiFile> selectedScope = getSelectedScope(myRightTree);
  Set<PsiFile> result = new HashSet<PsiFile>();
  ((PackageDependenciesNode)myLeftTree.getModel().getRoot()).fillFiles(result, !mySettings.UI_FLATTEN_PACKAGES);
  selectedScope.removeAll(result);
  if (selectedScope.isEmpty()) return null;
  List<VirtualFile> files = new ArrayList<VirtualFile>();
  final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(myProject).getFileIndex();
  for (PsiFile psiFile : selectedScope) {
    final VirtualFile file = psiFile.getVirtualFile();
    LOG.assertTrue(file != null);
    if (fileIndex.isInContent(file)) {
      files.add(file);
    }
  }
  if (!files.isEmpty()) {
    return new AnalysisScope(myProject, files);
  }
  return null;
}
 
Example 5
Source File: VFSUtils.java    From SmartIM4IntelliJ with Apache License 2.0 5 votes vote down vote up
public static String getPath(VirtualFile file) {
    String result = null;
    Document document = FileDocumentManager.getInstance().getDocument(file);

    Project[] openProjects = VFSUtils.getOpenProjects();
    for (int i = 0; i < openProjects.length && result == null; i++) {
        Project openProject = openProjects[i];
        if (!openProject.isInitialized() && !ApplicationManager.getApplication().isUnitTestMode())
            continue;
        if (document != null) {
            PsiFile psiFile = PsiDocumentManager.getInstance(openProject).getPsiFile(document);
            result = PsiFileTypeFactory.create(psiFile).getQualifiedName(psiFile);
        }
        ProjectFileIndex projectFileIndex = ProjectRootManager.getInstance(openProject).getFileIndex();
        if (projectFileIndex.isInSource(file)) {
            VirtualFile sourceRoot = projectFileIndex.getSourceRootForFile(file);
            result = (getRelativePath(file, sourceRoot));
        }

        if (projectFileIndex.isInContent(file)) {
            VirtualFile contentRoot = projectFileIndex.getContentRootForFile(file);
            result = (getRelativePath(file, contentRoot));
        }

    }
    return result;
}
 
Example 6
Source File: TestsLocationProviderUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static List<VirtualFile> findSuitableFilesFor(final String filePath, final Project project) {
  final ProjectFileIndex index = ProjectRootManager.getInstance(project).getFileIndex();

  // at first let's try to find file as is, by it's real path
  // and check that file belongs to current project
  // this location provider designed for tests thus we will check only project content
  // (we cannot check just sources or tests folders because RM doesn't use it
  final VirtualFile file = getByFullPath(filePath);
  final boolean inProjectContent = file != null && (index.isInContent(file));

  if (inProjectContent) {
    return Collections.singletonList(file);
  }

  //split file by "/" in parts
  final LinkedList<String> folders = new LinkedList<String>();
  final StringTokenizer st = new StringTokenizer(filePath, "/", false);
  String fileName = null;
  while (st.hasMoreTokens()) {
    final String pathComponent = st.nextToken();
    if (st.hasMoreTokens()) {
      folders.addFirst(pathComponent);
    } else {
      // last token
      fileName = pathComponent;
    }
  }
  if (fileName == null) {
    return Collections.emptyList();
  }
  return findFilesClosestToTarget(folders, collectCandidates(project, fileName, true), MIN_PROXIMITY_THRESHOLD);
}
 
Example 7
Source File: NonProjectFileWritingAccessProvider.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static boolean isProjectFile(@Nonnull VirtualFile file, @Nonnull Project project) {
  for (NonProjectFileWritingAccessExtension each : Extensions.getExtensions(NonProjectFileWritingAccessExtension.EP_NAME, project)) {
    if (each.isWritable(file)) return true;
    if (each.isNotWritable(file)) return false;
  }

  ProjectFileIndex fileIndex = ProjectFileIndex.SERVICE.getInstance(project);
  if (fileIndex.isInContent(file)) return true;
  if (!Registry.is("ide.hide.excluded.files") && fileIndex.isExcluded(file) && !fileIndex.isUnderIgnored(file)) return true;

  return false;
}
 
Example 8
Source File: CheckinHandlerUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static boolean isFileUnderSourceRoot(@Nonnull Project project, @Nonnull VirtualFile file) {
  ProjectFileIndex index = ProjectRootManager.getInstance(project).getFileIndex();
  if (InternalStdFileTypes.JAVA == file.getFileType()) {
    return index.isInSource(file) && !index.isInLibrarySource(file);
  } else {
    return index.isInContent(file) && !index.isInLibrarySource(file) ;
  }
}
 
Example 9
Source File: AbstractFileTreeTable.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
protected void appendChildrenTo(@Nonnull final Collection<ConvenientNode> children) {
  VirtualFile[] childrenf = getObject().getChildren();
  ProjectFileIndex fileIndex = ProjectRootManager.getInstance(myProject).getFileIndex();
  for (VirtualFile child : childrenf) {
    if (myFilter.accept(child) && fileIndex.isInContent(child)) {
      children.add(new FileNode(child, myProject, myFilter));
    }
  }
}
 
Example 10
Source File: GotoFileItemProvider.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
private PsiFileSystemItem getFileByAbsolutePath(@Nonnull String pattern) {
  if (pattern.contains("/") || pattern.contains("\\")) {
    String path = FileUtil.toSystemIndependentName(ChooseByNamePopup.getTransformedPattern(pattern, myModel));
    VirtualFile vFile = LocalFileSystem.getInstance().findFileByPathIfCached(path);
    if (vFile != null) {
      ProjectFileIndex index = ProjectFileIndex.getInstance(myProject);
      if (index.isInContent(vFile) || index.isInLibrary(vFile)) {
        return PsiUtilCore.findFileSystemItem(myProject, vFile);
      }
    }
  }
  return null;
}
 
Example 11
Source File: BaseProjectViewDirectoryHelper.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static List<VirtualFile> getTopLevelRoots(Project project) {
  List<VirtualFile> topLevelContentRoots = new ArrayList<>();
  ProjectRootManager prm = ProjectRootManager.getInstance(project);
  ProjectFileIndex index = prm.getFileIndex();

  for (VirtualFile root : prm.getContentRoots()) {
    VirtualFile parent = root.getParent();
    if (parent == null || !index.isInContent(parent)) {
      topLevelContentRoots.add(root);
    }
  }
  return topLevelContentRoots;
}
 
Example 12
Source File: DirectoryChooserUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
public static PsiDirectory selectDirectory(Project project,
                                           PsiDirectory[] packageDirectories,
                                           PsiDirectory defaultDirectory,
                                           String postfixToShow) {
  ProjectFileIndex projectFileIndex = ProjectRootManager.getInstance(project).getFileIndex();

  ArrayList<PsiDirectory> possibleDirs = new ArrayList<PsiDirectory>();
  for (PsiDirectory dir : packageDirectories) {
    if (!dir.isValid()) continue;
    if (!dir.isWritable()) continue;
    if (possibleDirs.contains(dir)) continue;
    if (!projectFileIndex.isInContent(dir.getVirtualFile())) continue;
    possibleDirs.add(dir);
  }

  if (possibleDirs.isEmpty()) return null;
  if (possibleDirs.size() == 1) return possibleDirs.get(0);

  if (ApplicationManager.getApplication().isUnitTestMode()) return possibleDirs.get(0);

  DirectoryChooser chooser = new DirectoryChooser(project);
  chooser.setTitle(IdeBundle.message("title.choose.destination.directory"));
  chooser.fillList(possibleDirs.toArray(new PsiDirectory[possibleDirs.size()]), defaultDirectory, project, postfixToShow);
  chooser.show();
  return chooser.isOK() ? chooser.getSelectedDirectory() : null;
}
 
Example 13
Source File: SimpleCoverageAnnotator.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nullable
protected DirCoverageInfo collectFolderCoverage(@Nonnull final VirtualFile dir,
                                                final @Nonnull CoverageDataManager dataManager,
                                                final Annotator annotator,
                                                final ProjectData projectInfo, boolean trackTestFolders,
                                                @Nonnull final ProjectFileIndex index,
                                                @Nonnull final CoverageEngine coverageEngine,
                                                Set<VirtualFile> visitedDirs,
                                                @Nonnull final Map<String, String> normalizedFiles2Files)
{
  if (!index.isInContent(dir)) {
    return null;
  }

  if (visitedDirs.contains(dir)) {
    return null;
  }

  visitedDirs.add(dir);

  final boolean isInTestSrcContent = TestSourcesFilter.isTestSources(dir, getProject());

  // Don't count coverage for tests folders if track test folders is switched off
  if (!trackTestFolders && isInTestSrcContent) {
    return null;
  }

  final VirtualFile[] children = dataManager.doInReadActionIfProjectOpen(new Computable<VirtualFile[]>() {
    public VirtualFile[] compute() {
      return dir.getChildren();
    }
  });

  if (children == null) {
    return null;
  }

  final DirCoverageInfo dirCoverageInfo = new DirCoverageInfo();

  for (VirtualFile fileOrDir : children) {
    if (fileOrDir.isDirectory()) {
      final DirCoverageInfo childCoverageInfo =
        collectFolderCoverage(fileOrDir, dataManager, annotator, projectInfo, trackTestFolders, index,
                              coverageEngine, visitedDirs, normalizedFiles2Files);

      if (childCoverageInfo != null) {
        dirCoverageInfo.totalFilesCount += childCoverageInfo.totalFilesCount;
        dirCoverageInfo.coveredFilesCount += childCoverageInfo.coveredFilesCount;
        dirCoverageInfo.totalLineCount += childCoverageInfo.totalLineCount;
        dirCoverageInfo.coveredLineCount += childCoverageInfo.coveredLineCount;
      }
    }
    else if (coverageEngine.coverageProjectViewStatisticsApplicableTo(fileOrDir)) {
      // let's count statistics only for ruby-based files

      final FileCoverageInfo fileInfo =
        collectBaseFileCoverage(fileOrDir, annotator, projectInfo, normalizedFiles2Files);

      if (fileInfo != null) {
        dirCoverageInfo.totalLineCount += fileInfo.totalLineCount;
        dirCoverageInfo.totalFilesCount++;

        if (fileInfo.coveredLineCount > 0) {
          dirCoverageInfo.coveredFilesCount++;
          dirCoverageInfo.coveredLineCount += fileInfo.coveredLineCount;
        }
      }
    }
  }


  //TODO - toplevelFilesCoverage - is unused variable!

  // no sense to include directories without ruby files
  if (dirCoverageInfo.totalFilesCount == 0) {
    return null;
  }

  final String dirPath = normalizeFilePath(dir.getPath());
  if (isInTestSrcContent) {
    annotator.annotateTestDirectory(dirPath, dirCoverageInfo);
  }
  else {
    annotator.annotateSourceDirectory(dirPath, dirCoverageInfo);
  }

  return dirCoverageInfo;
}
 
Example 14
Source File: ConfigureHighlightingLevel.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nullable
public static JBPopup getConfigureHighlightingLevelPopup(DataContext context) {
  PsiFile psi = context.getData(CommonDataKeys.PSI_FILE);
  if (psi == null) {
    return null;
  }

  if (!psi.isValid() || psi.getProject().isDisposed()) return null;

  FileViewProvider provider = psi.getViewProvider();

  Set<Language> languages = provider.getLanguages();

  if (languages.isEmpty()) return null;

  VirtualFile file = psi.getVirtualFile();
  if (file == null) {
    return null;
  }

  ProjectFileIndex index = ProjectFileIndex.getInstance(psi.getProject());

  boolean isAllInspectionsEnabled = index.isInContent(file) || !index.isInLibrary(file);
  boolean isSeparatorNeeded = languages.size() > 1;

  DefaultActionGroup group = new DefaultActionGroup();

  languages.stream().sorted((o1, o2) -> o1.getDisplayName().compareTo(o2.getDisplayName())).forEach(it -> {
    if (isSeparatorNeeded) {
      group.add(AnSeparator.create(it.getDisplayName()));
    }
    group.add(new LevelAction(InspectionsLevel.NONE, provider, it));
    group.add(new LevelAction(InspectionsLevel.ERRORS, provider, it));
    if (isAllInspectionsEnabled) {
      group.add(new LevelAction(InspectionsLevel.ALL, provider, it));
    }
  });

  group.add(AnSeparator.create());
  group.add(new ConfigureInspectionsAction());
  String title = DaemonBundle.message("popup.title.configure.highlighting.level", psi.getVirtualFile().getPresentableName());

  return JBPopupFactory.getInstance().createActionGroupPopup(title, group, context, true, null, 100);
}
 
Example 15
Source File: PsiVFSListener.java    From consulo with Apache License 2.0 4 votes vote down vote up
private boolean isInRootModel(@Nonnull VirtualFile file) {
  ProjectFileIndex index = ProjectFileIndex.getInstance(myProject);
  return index.isInContent(file) || index.isInLibrary(file);
}