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

The following examples show how to use com.intellij.openapi.roots.ProjectFileIndex#getContentRootForFile() . 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: ProjectFileIndexSampleAction.java    From intellij-sdk-docs with Apache License 2.0 6 votes vote down vote up
@Override
public void actionPerformed(@NotNull final AnActionEvent event) {
  Project project = event.getProject();
  final Editor editor = event.getData(CommonDataKeys.EDITOR);
  if (project == null || editor == null) return;
  Document document = editor.getDocument();
  FileDocumentManager fileDocumentManager = FileDocumentManager.getInstance();
  VirtualFile virtualFile = fileDocumentManager.getFile(document);
  ProjectFileIndex projectFileIndex = ProjectRootManager.getInstance(project).getFileIndex();
  if (virtualFile != null) {
    Module module = projectFileIndex.getModuleForFile(virtualFile);
    String moduleName;
    moduleName = module != null ? module.getName() : "No module defined for file";

    VirtualFile moduleContentRoot = projectFileIndex.getContentRootForFile(virtualFile);
    boolean isLibraryFile = projectFileIndex.isLibraryClassFile(virtualFile);
    boolean isInLibraryClasses = projectFileIndex.isInLibraryClasses(virtualFile);
    boolean isInLibrarySource = projectFileIndex.isInLibrarySource(virtualFile);
    Messages.showInfoMessage("Module: " + moduleName + "\n" +
                             "Module content root: " + moduleContentRoot + "\n" +
                             "Is library file: " + isLibraryFile + "\n" +
                             "Is in library classes: " + isInLibraryClasses +
                             ", Is in library source: " + isInLibrarySource,
                             "Main File Info for" + virtualFile.getName());
  }
}
 
Example 2
Source File: FilePatternPackageSet.java    From consulo with Apache License 2.0 6 votes vote down vote up
@javax.annotation.Nullable
public static String getRelativePath(@Nonnull VirtualFile virtualFile,
                                     @Nonnull ProjectFileIndex index,
                                     final boolean useFQName,
                                     VirtualFile projectBaseDir) {
  final VirtualFile contentRootForFile = index.getContentRootForFile(virtualFile);
  if (contentRootForFile != null) {
    return VfsUtilCore.getRelativePath(virtualFile, contentRootForFile, '/');
  }
  final Module module = index.getModuleForFile(virtualFile);
  if (module != null) {
    if (projectBaseDir != null) {
      if (VfsUtilCore.isAncestor(projectBaseDir, virtualFile, false)){
        final String projectRelativePath = VfsUtilCore.getRelativePath(virtualFile, projectBaseDir, '/');
        return useFQName ? projectRelativePath : projectRelativePath.substring(projectRelativePath.indexOf('/') + 1);
      }
    }
    return virtualFile.getPath();
  } else {
    return getLibRelativePath(virtualFile, index);
  }
}
 
Example 3
Source File: CopyReferenceAction.java    From consulo with Apache License 2.0 6 votes vote down vote up
private static String getVirtualFileFqn(@Nonnull VirtualFile virtualFile, @Nonnull Project project) {
  final LogicalRoot logicalRoot = LogicalRootsManager.getLogicalRootsManager(project).findLogicalRoot(virtualFile);
  VirtualFile logicalRootFile = logicalRoot != null ? logicalRoot.getVirtualFile() : null;
  if (logicalRootFile != null && !virtualFile.equals(logicalRootFile)) {
    return ObjectUtil.assertNotNull(VfsUtilCore.getRelativePath(virtualFile, logicalRootFile, '/'));
  }

  VirtualFile outerMostRoot = null;
  VirtualFile each = virtualFile;
  ProjectFileIndex index = ProjectRootManager.getInstance(project).getFileIndex();
  while (each != null && (each = index.getContentRootForFile(each, false)) != null) {
    outerMostRoot = each;
    each = each.getParent();
  }

  if (outerMostRoot != null && !outerMostRoot.equals(virtualFile)) {
    return ObjectUtil.assertNotNull(VfsUtilCore.getRelativePath(virtualFile, outerMostRoot, '/'));
  }

  return virtualFile.getPath();
}
 
Example 4
Source File: ProjectViewPane.java    From consulo with Apache License 2.0 6 votes vote down vote up
public static boolean canBeSelectedInProjectView(@Nonnull Project project, @Nonnull VirtualFile file) {
  final VirtualFile archiveFile;

  if (file.getFileSystem() instanceof ArchiveFileSystem) {
    archiveFile = ArchiveVfsUtil.getVirtualFileForArchive(file);
  }
  else {
    archiveFile = null;
  }

  ProjectFileIndex index = ProjectRootManager.getInstance(project).getFileIndex();
  return (archiveFile != null && index.getContentRootForFile(archiveFile, false) != null) ||
         index.getContentRootForFile(file, false) != null ||
         index.isInLibrary(file) ||
         Comparing.equal(file.getParent(), project.getBaseDir()) ||
         ScratchUtil.isScratch(file);
}
 
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: PubRoot.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Returns the pub root for the content root containing a file or directory.
 * <p>
 * Based on the filesystem cache; doesn't refresh anything.
 */
@Nullable
public static PubRoot forDescendant(@NotNull VirtualFile fileOrDir, @NotNull Project project) {
  final ProjectFileIndex index = ProjectRootManager.getInstance(project).getFileIndex();
  final VirtualFile root = index.getContentRootForFile(fileOrDir);
  return forDirectory(root);
}
 
Example 7
Source File: PubRoot.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Returns the pub root for the content root containing a file or directory.
 * <p>
 * Based on the filesystem cache; doesn't refresh anything.
 */
@Nullable
public static PubRoot forDescendant(@NotNull VirtualFile fileOrDir, @NotNull Project project) {
  final ProjectFileIndex index = ProjectRootManager.getInstance(project).getFileIndex();
  final VirtualFile root = index.getContentRootForFile(fileOrDir);
  return forDirectory(root);
}
 
Example 8
Source File: NullFileReferenceHelper.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public PsiFileSystemItem findRoot(final Project project, @Nonnull final VirtualFile file) {
  final ProjectFileIndex index = ProjectRootManager.getInstance(project).getFileIndex();
  VirtualFile contentRootForFile = index.getContentRootForFile(file);

  return contentRootForFile != null ? PsiManager.getInstance(project).findDirectory(contentRootForFile) : null;
}
 
Example 9
Source File: FileIncludeContextHectorPanel.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
protected String getPath(final Object value) {
  final VirtualFile file = (VirtualFile)value;
  final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(myFile.getProject()).getFileIndex();
  if (file != null) {
    VirtualFile root = fileIndex.getSourceRootForFile(file);
    if (root == null) {
      root = fileIndex.getContentRootForFile(file);
    }
    if (root != null) {
      return VfsUtilCore.getRelativePath(file, root, '/');
    }
  }
  return null;
}
 
Example 10
Source File: SymbolPresentationUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static String getFilePathPresentation(PsiFile psiFile) {
  ProjectFileIndex index = ProjectRootManager.getInstance(psiFile.getProject()).getFileIndex();
  VirtualFile file = psiFile.getOriginalFile().getVirtualFile();
  VirtualFile rootForFile = file != null ? index.getContentRootForFile(file):null;

  if (rootForFile != null) {
    String relativePath = VfsUtilCore.getRelativePath(file, rootForFile, File.separatorChar);
    if (relativePath != null) return relativePath;
  }

  return psiFile.getName();
}
 
Example 11
Source File: PsiFileReferenceHelper.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public PsiFileSystemItem findRoot(final Project project, @Nonnull final VirtualFile file) {
  final ProjectFileIndex index = ProjectRootManager.getInstance(project).getFileIndex();
  VirtualFile contentRootForFile = index.getSourceRootForFile(file);
  if (contentRootForFile == null) contentRootForFile = index.getContentRootForFile(file);

  if (contentRootForFile != null) {
    return PsiManager.getInstance(project).findDirectory(contentRootForFile);
  }
  return null;
}
 
Example 12
Source File: DirectoryNode.java    From consulo with Apache License 2.0 5 votes vote down vote up
public String getFQName() {
  final ProjectFileIndex index = ProjectRootManager.getInstance(myProject).getFileIndex();
  VirtualFile directory = myVDirectory;
  VirtualFile contentRoot = index.getContentRootForFile(directory);
  if (Comparing.equal(directory, contentRoot)) {
    return "";
  }
  if (contentRoot == null) {
    return "";
  }
  return VfsUtilCore.getRelativePath(directory, contentRoot, '/');
}
 
Example 13
Source File: GotoFileCellRenderer.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
public static VirtualFile getAnyRoot(@Nonnull VirtualFile virtualFile, @Nonnull Project project) {
  ProjectFileIndex index = ProjectFileIndex.SERVICE.getInstance(project);
  VirtualFile root = index.getContentRootForFile(virtualFile);
  if (root == null) root = index.getClassRootForFile(virtualFile);
  if (root == null) root = index.getSourceRootForFile(virtualFile);
  return root;
}
 
Example 14
Source File: ProjectRootsUtil.java    From consulo with Apache License 2.0 4 votes vote down vote up
public static boolean isModuleContentRoot(@Nonnull ProjectFileIndex projectFileIndex, @Nonnull VirtualFile directoryFile) {
  final VirtualFile contentRootForFile = projectFileIndex.getContentRootForFile(directoryFile);
  return directoryFile.equals(contentRootForFile);
}
 
Example 15
Source File: DirectoryNode.java    From consulo with Apache License 2.0 4 votes vote down vote up
public DirectoryNode(VirtualFile aDirectory,
                     Project project,
                     boolean compactPackages,
                     boolean showFQName,
                     VirtualFile baseDir, final VirtualFile[] contentRoots) {
  super(project);
  myVDirectory = aDirectory;
  final ProjectRootManager projectRootManager = ProjectRootManager.getInstance(project);
  final ProjectFileIndex index = projectRootManager.getFileIndex();
  String dirName = aDirectory.getName();
  if (showFQName) {
    final VirtualFile contentRoot = index.getContentRootForFile(myVDirectory);
    if (contentRoot != null) {
      if (Comparing.equal(myVDirectory, contentRoot)) {
        myFQName = dirName;
      }
      else {
        final VirtualFile sourceRoot = index.getSourceRootForFile(myVDirectory);
        if (Comparing.equal(myVDirectory, sourceRoot)) {
          myFQName = VfsUtilCore.getRelativePath(myVDirectory, contentRoot, '/');
        }
        else if (sourceRoot != null) {
          myFQName = VfsUtilCore.getRelativePath(myVDirectory, sourceRoot, '/');
        }
        else {
          myFQName = VfsUtilCore.getRelativePath(myVDirectory, contentRoot, '/');
        }
      }

      if (contentRoots.length > 1 && ProjectRootsUtil.isModuleContentRoot(myVDirectory, project)) {
        myFQName = getContentRootName(baseDir, myFQName);
      }
    }
    else {
      myFQName = FilePatternPackageSet.getLibRelativePath(myVDirectory, index);
    }
    dirName = myFQName;
  } else {
    if (contentRoots.length > 1 && ProjectRootsUtil.isModuleContentRoot(myVDirectory, project)) {
      dirName = getContentRootName(baseDir, dirName);
    }
  }
  myDirName = dirName;
  myCompactPackages = compactPackages;
}