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

The following examples show how to use com.intellij.openapi.roots.ProjectFileIndex#getInstance() . 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: PsiTypeUtils.java    From intellij-quarkus with Eclipse Public License 2.0 5 votes vote down vote up
public static VirtualFile getRootDirectory(PsiFile file) {
    ProjectFileIndex index = ProjectFileIndex.getInstance(file.getProject());
    VirtualFile directory = index.getSourceRootForFile(file.getVirtualFile());
    if (directory == null) {
        directory = index.getClassRootForFile(file.getVirtualFile());
    }
    return directory;
}
 
Example 2
Source File: CreateDirectoryOrPackageAction.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
@RequiredUIAccess
private static Trinity<ContentFolderTypeProvider, PsiDirectory, ChildType> getInfo(PsiDirectory d) {
  Project project = d.getProject();
  ProjectFileIndex projectFileIndex = ProjectFileIndex.getInstance(project);

  Module moduleForPsiElement = ModuleUtilCore.findModuleForPsiElement(d);
  if (moduleForPsiElement != null) {
    boolean isPackageSupported = false;
    ModuleRootManager moduleRootManager = ModuleRootManager.getInstance(moduleForPsiElement);
    List<PsiPackageSupportProvider> extensions = PsiPackageSupportProvider.EP_NAME.getExtensionList();
    for (ModuleExtension moduleExtension : moduleRootManager.getExtensions()) {
      for (PsiPackageSupportProvider supportProvider : extensions) {
        if (supportProvider.isSupported(moduleExtension)) {
          isPackageSupported = true;
          break;
        }
      }
    }

    if (isPackageSupported) {
      ContentFolderTypeProvider contentFolderTypeForFile = projectFileIndex.getContentFolderTypeForFile(d.getVirtualFile());
      if (contentFolderTypeForFile != null) {
        Image childPackageIcon = contentFolderTypeForFile.getChildPackageIcon();
        return Trinity.create(contentFolderTypeForFile, d, childPackageIcon != null ? ChildType.Package : ChildType.Directory);
      }
    }
  }

  return Trinity.create(null, d, ChildType.Directory);
}
 
Example 3
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 4
Source File: BuckAddDependencyIntention.java    From buck with Apache License 2.0 4 votes vote down vote up
/**
 * Creates an {@link com.intellij.codeInsight.intention.IntentionAction} that will create an
 * dependency edge in both the Buck target graph and IntelliJ module graph from the nodes for the
 * given reference element to those of the given psiClass.
 *
 * <p>Note that this intention can fail to be created if either side of the edge cannot be
 * resolved to a buck file in a buck cell, in which case this method returns null. Also, invoking
 * this intention may fail to create edges in either the Buck target graph or the IntelliJ module
 * graph (or both).
 */
@Nullable
public static BuckAddDependencyIntention create(PsiReference reference, PsiClass psiClass) {
  VirtualFile editSourceFile = reference.getElement().getContainingFile().getVirtualFile();
  if (editSourceFile == null) {
    return null;
  }
  Project project = reference.getElement().getProject();
  BuckTargetLocator buckTargetLocator = BuckTargetLocator.getInstance(project);
  VirtualFile editBuildFile =
      buckTargetLocator.findBuckFileForVirtualFile(editSourceFile).orElse(null);
  if (editBuildFile == null) {
    return null;
  }
  VirtualFile importSourceFile = psiClass.getContainingFile().getVirtualFile();
  if (importSourceFile == null) {
    return null;
  }
  VirtualFile importBuildFile =
      buckTargetLocator.findBuckFileForVirtualFile(importSourceFile).orElse(null);
  if (importBuildFile == null) {
    return null;
  }
  if (importBuildFile.equals(editBuildFile)) {
    return null;
  }
  ProjectFileIndex projectFileIndex = ProjectFileIndex.getInstance(project);
  Module editModule = projectFileIndex.getModuleForFile(editSourceFile);
  if (editModule == null) {
    return null;
  }
  Module importModule = projectFileIndex.getModuleForFile(importSourceFile);
  if (importModule == null) {
    return null;
  }
  BuckTarget editSourceTarget =
      buckTargetLocator
          .findTargetPatternForVirtualFile(editSourceFile)
          .flatMap(BuckTargetPattern::asBuckTarget)
          .orElse(null);
  if (editSourceTarget == null) {
    return null;
  }
  BuckTarget importSourceTarget =
      buckTargetLocator
          .findTargetPatternForVirtualFile(importSourceFile)
          .flatMap(BuckTargetPattern::asBuckTarget)
          .orElse(null);
  if (importSourceTarget == null) {
    return null;
  }
  return new BuckAddDependencyIntention(
      project,
      reference,
      editBuildFile,
      editSourceFile,
      editSourceTarget,
      editModule,
      psiClass,
      importBuildFile,
      importSourceFile,
      importSourceTarget,
      importModule);
}
 
Example 5
Source File: ResourceCompiler.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Inject
public ResourceCompiler(Project project) {
  myResourceCompilerConfiguration = ResourceCompilerConfiguration.getInstance(project);
  myProjectFileIndex = ProjectFileIndex.getInstance(project);
}
 
Example 6
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 7
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);
}
 
Example 8
Source File: ProjectAPI.java    From saros with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Returns whether the given virtual file is part of the content of the given project.
 *
 * <p>A resource is seen as part of the content of a project if it is located under the content
 * root of any of the modules of the project.
 *
 * @param project the project for which to check
 * @param virtualFile the virtual file to check
 * @return whether the given virtual file is part of the content of the given project
 * @see ProjectFileIndex#isInContent(VirtualFile)
 */
public static boolean isInProjectContent(
    @NotNull Project project, @NotNull VirtualFile virtualFile) {
  ProjectFileIndex projectFileIndex = ProjectFileIndex.getInstance(project);
  return FilesystemRunner.runReadAction(() -> projectFileIndex.isInContent(virtualFile));
}
 
Example 9
Source File: ProjectAPI.java    From saros with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Returns whether the given virtual file is seen as excluded for the given project.
 *
 * @param project the project to check for
 * @param virtualFile the virtual file to check
 * @return whether the given virtual file is seen as excluded for the given project
 * @see ProjectFileIndex#isExcluded(VirtualFile)
 */
public static boolean isExcluded(@NotNull Project project, @NotNull VirtualFile virtualFile) {
  ProjectFileIndex projectFileIndex = ProjectFileIndex.getInstance(project);
  return FilesystemRunner.runReadAction(() -> projectFileIndex.isExcluded(virtualFile));
}