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

The following examples show how to use com.intellij.openapi.roots.ProjectFileIndex#getSourceRootForFile() . 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: MakeUtil.java    From consulo with Apache License 2.0 6 votes vote down vote up
public static VirtualFile getSourceRoot(CompileContext context, Module module, VirtualFile file) {
  final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(module.getProject()).getFileIndex();
  final VirtualFile root = fileIndex.getSourceRootForFile(file);
  if (root != null) {
    return root;
  }
  // try to find among roots of generated files.
  final VirtualFile[] sourceRoots = context.getSourceRoots(module);
  for (final VirtualFile sourceRoot : sourceRoots) {
    if (fileIndex.isInSourceContent(sourceRoot)) {
      continue; // skip content source roots, need only roots for generated files
    }
    if (VfsUtil.isAncestor(sourceRoot, file, false)) {
      return sourceRoot;
    }
  }
  return null;
}
 
Example 2
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 3
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 4
Source File: JavaExtractSuperBaseDialog.java    From intellij-haxe with Apache License 2.0 5 votes vote down vote up
private PsiDirectory getDirUnderSameSourceRoot(final PsiDirectory[] directories) {
  final VirtualFile sourceFile = mySourceClass.getContainingFile().getVirtualFile();
  if (sourceFile != null) {
    final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(myProject).getFileIndex();
    final VirtualFile sourceRoot = fileIndex.getSourceRootForFile(sourceFile);
    if (sourceRoot != null) {
      for (PsiDirectory dir : directories) {
        if (Comparing.equal(fileIndex.getSourceRootForFile(dir.getVirtualFile()), sourceRoot)) {
          return dir;
        }
      }
    }
  }
  return directories[0];
}
 
Example 5
Source File: ExtractInterfaceDialog.java    From intellij-haxe with Apache License 2.0 5 votes vote down vote up
private PsiDirectory getDirUnderSameSourceRoot(final PsiDirectory[] directories) {
  final VirtualFile sourceFile = mySourceClass.getContainingFile().getVirtualFile();
  if (sourceFile != null) {
    final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(myProject).getFileIndex();
    final VirtualFile sourceRoot = fileIndex.getSourceRootForFile(sourceFile);
    if (sourceRoot != null) {
      for (PsiDirectory dir : directories) {
        if (Comparing.equal(fileIndex.getSourceRootForFile(dir.getVirtualFile()), sourceRoot)) {
          return dir;
        }
      }
    }
  }
  return directories[0];
}
 
Example 6
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 7
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 8
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 9
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;
}