@Nullable
 private VirtualFile getFileRoot(VirtualFile file) {
   if (myIndex.isLibraryClassFile(file)) {
     return myIndex.getClassRootForFile(file);
   }
   if (myIndex.isInContent(file)) {
     return myIndex.getSourceRootForFile(file);
   }
   if (myIndex.isInLibraryClasses(file)) {
     return myIndex.getClassRootForFile(file);
   }
   return null;
 }
 public static boolean isRoot(PsiFileSystemItem directory) {
   if (directory == null) return true;
   VirtualFile vFile = directory.getVirtualFile();
   if (vFile == null) return true;
   ProjectFileIndex fileIndex = ProjectFileIndex.SERVICE.getInstance(directory.getProject());
   return Comparing.equal(fileIndex.getClassRootForFile(vFile), vFile)
       || Comparing.equal(fileIndex.getContentRootForFile(vFile), vFile)
       || Comparing.equal(fileIndex.getSourceRootForFile(vFile), vFile);
 }
Exemplo n.º 3
0
  public static String getRootDirectoryForPackage(PsiDirectory directory) {
    PsiManager manager = directory.getManager();
    final VirtualFile virtualFile = directory.getVirtualFile();
    final ProjectFileIndex fileIndex =
        ProjectRootManager.getInstance(manager.getProject()).getFileIndex();
    VirtualFile root = fileIndex.getSourceRootForFile(virtualFile);

    if (root == null) {
      root = fileIndex.getClassRootForFile(virtualFile);
    }
    if (root != null) {
      return root.getPresentableUrl();
    }
    return null;
  }
 private static void addSourceDirectoriesFromLibraries(
     @NotNull Project project,
     @NotNull VirtualFile directory,
     @NotNull Collection<VirtualFile> outSourceRoots) {
   ProjectFileIndex index = ProjectFileIndex.SERVICE.getInstance(project);
   VirtualFile classRoot = index.getClassRootForFile(directory);
   if (classRoot == null) return;
   String relativePath = VfsUtilCore.getRelativePath(directory, classRoot);
   if (relativePath == null) return;
   for (OrderEntry orderEntry : index.getOrderEntriesForFile(directory)) {
     for (VirtualFile sourceRoot : orderEntry.getFiles(OrderRootType.SOURCES)) {
       VirtualFile sourceFile = sourceRoot.findFileByRelativePath(relativePath);
       if (sourceFile != null) {
         outSourceRoots.add(sourceFile);
       }
     }
   }
 }
  @Nullable
  public BundleManifest getManifest(@NotNull PsiFileSystemItem item) {
    VirtualFile file = item.getVirtualFile();
    if (file != null) {
      List<OrderEntry> entries = myIndex.getOrderEntriesForFile(file);
      if (entries.size() == 1 && entries.get(0) instanceof JdkOrderEntry) {
        return new JdkBundleManifest();
      }

      Module module = myIndex.getModuleForFile(file);
      if (module != null) {
        return getManifest(module);
      }

      VirtualFile libRoot = myIndex.getClassRootForFile(file);
      if (libRoot != null) {
        return getManifest(libRoot);
      }
    }

    return null;
  }
 @Override
 public String toString() {
   if (myText == null) {
     Module module = ModuleUtilCore.findModuleForPsiElement(myClass);
     if (module != null) {
       myText = module.getName();
     } else {
       VirtualFile virtualFile = myClass.getContainingFile().getVirtualFile();
       final ProjectFileIndex index =
           ProjectRootManager.getInstance(myClass.getProject()).getFileIndex();
       VirtualFile root = index.getSourceRootForFile(virtualFile);
       if (root == null) {
         root = index.getClassRootForFile(virtualFile);
       }
       if (root != null) {
         myText = root.getName();
       } else {
         myText = virtualFile.getPath();
       }
     }
   }
   return myText;
 }