本文整理汇总了Java中com.intellij.openapi.roots.ProjectFileIndex.getContentRootForFile方法的典型用法代码示例。如果您正苦于以下问题:Java ProjectFileIndex.getContentRootForFile方法的具体用法?Java ProjectFileIndex.getContentRootForFile怎么用?Java ProjectFileIndex.getContentRootForFile使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.intellij.openapi.roots.ProjectFileIndex
的用法示例。
在下文中一共展示了ProjectFileIndex.getContentRootForFile方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getRelativePath
import com.intellij.openapi.roots.ProjectFileIndex; //导入方法依赖的package包/类
@Nullable
public static String getRelativePath(@NotNull VirtualFile virtualFile,
@NotNull 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);
}
}
示例2: getVirtualFileFqn
import com.intellij.openapi.roots.ProjectFileIndex; //导入方法依赖的package包/类
private static String getVirtualFileFqn(@NotNull VirtualFile virtualFile, @NotNull Project project) {
final LogicalRoot logicalRoot = LogicalRootsManager.getLogicalRootsManager(project).findLogicalRoot(virtualFile);
VirtualFile logicalRootFile = logicalRoot != null ? logicalRoot.getVirtualFile() : null;
if (logicalRootFile != null && !virtualFile.equals(logicalRootFile)) {
return ObjectUtils.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 ObjectUtils.assertNotNull(VfsUtilCore.getRelativePath(virtualFile, outerMostRoot, '/'));
}
return virtualFile.getPath();
}
示例3: canSelect
import com.intellij.openapi.roots.ProjectFileIndex; //导入方法依赖的package包/类
private boolean canSelect(VirtualFile vFile) {
if (vFile != null && vFile.isValid()) {
ProjectFileIndex index = ProjectRootManager.getInstance(myProject).getFileIndex();
if (index.getContentRootForFile(vFile, false) != null) {
return true;
}
if (index.isInLibraryClasses(vFile) || index.isInLibrarySource(vFile)) {
return true;
}
return Comparing.equal(vFile.getParent(), myProject.getBaseDir());
}
return false;
}
示例4: getReferencePath
import com.intellij.openapi.roots.ProjectFileIndex; //导入方法依赖的package包/类
public static String getReferencePath(Project project, VirtualFile file) {
final LogicalRoot logicalRoot = LogicalRootsManager.getLogicalRootsManager(project).findLogicalRoot(file);
if (logicalRoot != null) {
return getRelativePath(file, logicalRoot.getVirtualFile());
}
ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex();
VirtualFile sourceRoot = fileIndex.getSourceRootForFile(file);
if (sourceRoot != null) {
return getRelativePath(file, sourceRoot);
}
VirtualFile root = fileIndex.getContentRootForFile(file);
if (root != null) {
return getRelativePath(file, root);
}
return file.getPath();
}
示例5: getFilePathPresentation
import com.intellij.openapi.roots.ProjectFileIndex; //导入方法依赖的package包/类
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();
}
示例6: findRoot
import com.intellij.openapi.roots.ProjectFileIndex; //导入方法依赖的package包/类
@Override
public PsiFileSystemItem findRoot(final Project project, @NotNull final VirtualFile file) {
final ProjectFileIndex index = ProjectRootManager.getInstance(project).getFileIndex();
VirtualFile contentRootForFile = index.getContentRootForFile(file);
return contentRootForFile != null ? PsiManager.getInstance(project).findDirectory(contentRootForFile) : null;
}
示例7: getPath
import com.intellij.openapi.roots.ProjectFileIndex; //导入方法依赖的package包/类
@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;
}
示例8: getFQName
import com.intellij.openapi.roots.ProjectFileIndex; //导入方法依赖的package包/类
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, '/');
}
示例9: getMavenProjects
import com.intellij.openapi.roots.ProjectFileIndex; //导入方法依赖的package包/类
public static List<MavenProject> getMavenProjects(DataContext context) {
Project project = CommonDataKeys.PROJECT.getData(context);
if (project == null) return Collections.emptyList();
VirtualFile[] virtualFiles = CommonDataKeys.VIRTUAL_FILE_ARRAY.getData(context);
if (virtualFiles == null || virtualFiles.length == 0) return Collections.emptyList();
MavenProjectsManager projectsManager = MavenProjectsManager.getInstance(project);
if (!projectsManager.isMavenizedProject()) return Collections.emptyList();
Set<MavenProject> res = new LinkedHashSet<MavenProject>();
ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex();
for (VirtualFile file : virtualFiles) {
MavenProject mavenProject;
if (file.isDirectory()) {
VirtualFile contentRoot = fileIndex.getContentRootForFile(file);
if (!file.equals(contentRoot)) return Collections.emptyList();
Module module = fileIndex.getModuleForFile(file);
if (module == null || !projectsManager.isMavenizedModule(module)) return Collections.emptyList();
mavenProject = projectsManager.findProject(module);
}
else {
mavenProject = projectsManager.findProject(file);
}
if (mavenProject == null) return Collections.emptyList();
res.add(mavenProject);
}
return new ArrayList<MavenProject>(res);
}
示例10: getContentPsiRoot
import com.intellij.openapi.roots.ProjectFileIndex; //导入方法依赖的package包/类
@Nullable
private PsiDirectory getContentPsiRoot() {
final Project project = getElement().getProject();
final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex();
final VirtualFile file = getElement().getContainingFile().getOriginalFile().getVirtualFile();
if (file != null) {
final VirtualFile contentRoot = fileIndex.getContentRootForFile(file);
if (contentRoot != null) {
final PsiManager psiManager = PsiManager.getInstance(project);
return psiManager.findDirectory(contentRoot);
}
}
return null;
}
示例11: getTestDataBasePath
import com.intellij.openapi.roots.ProjectFileIndex; //导入方法依赖的package包/类
@Nullable
public static String getTestDataBasePath(@Nullable PsiClass psiClass) {
if (psiClass == null) return null;
final PsiAnnotation annotation =
AnnotationUtil.findAnnotationInHierarchy(psiClass, Collections.singleton(TEST_DATA_PATH_ANNOTATION_QUALIFIED_NAME));
if (annotation != null) {
final PsiAnnotationMemberValue value = annotation.findAttributeValue(PsiAnnotation.DEFAULT_REFERENCED_METHOD_NAME);
if (value instanceof PsiExpression) {
final Project project = value.getProject();
final PsiConstantEvaluationHelper evaluationHelper = JavaPsiFacade.getInstance(project).getConstantEvaluationHelper();
final Object constantValue = evaluationHelper.computeConstantExpression(value, false);
if (constantValue instanceof String) {
String path = (String) constantValue;
if (path.contains(CONTENT_ROOT_VARIABLE)) {
final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex();
final VirtualFile file = psiClass.getContainingFile().getVirtualFile();
if (file == null) {
return null;
}
final VirtualFile contentRoot = fileIndex.getContentRootForFile(file);
if (contentRoot == null) return null;
path = path.replace(CONTENT_ROOT_VARIABLE, contentRoot.getPath());
}
if (path.contains(PROJECT_ROOT_VARIABLE)) {
final VirtualFile baseDir = project.getBaseDir();
if (baseDir == null) {
return null;
}
path = path.replace(PROJECT_ROOT_VARIABLE, baseDir.getPath());
}
return path;
}
}
}
return null;
}
示例12: DirectoryNode
import com.intellij.openapi.roots.ProjectFileIndex; //导入方法依赖的package包/类
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;
}
示例13: collectRelativeLocations
import com.intellij.openapi.roots.ProjectFileIndex; //导入方法依赖的package包/类
@NotNull
protected static List<Location> collectRelativeLocations(Project project, VirtualFile file) {
if (DumbService.isDumb(project)) return Collections.emptyList();
final List<Location> locations = new ArrayList<Location>();
final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex();
if (fileIndex.isInContent(file) && !fileIndex.isInSource(file) && !fileIndex.isInLibraryClasses(file)) {
final VirtualFile parent = file.getParent();
final VirtualFile contentRoot = fileIndex.getContentRootForFile(file);
if (contentRoot != null) {
final String relativePath = VfsUtilCore.getRelativePath(parent, contentRoot, '/');
if (relativePath != null) {
final PsiSearchHelper searchHelper = PsiSearchHelper.SERVICE.getInstance(project);
final List<String> words = StringUtil.getWordsIn(relativePath);
// put longer strings first
Collections.sort(words, new Comparator<String>() {
@Override
public int compare(final String o1, final String o2) {
return o2.length() - o1.length();
}
});
final GlobalSearchScope testScope = GlobalSearchScopesCore.projectTestScope(project);
Set<PsiFile> resultFiles = null;
for (String word : words) {
if (word.length() < 5) {
continue;
}
final Set<PsiFile> files = new THashSet<PsiFile>();
searchHelper.processAllFilesWithWordInLiterals(word, testScope, new CommonProcessors.CollectProcessor<PsiFile>(files));
if (resultFiles == null) {
resultFiles = files;
}
else {
resultFiles.retainAll(files);
}
if (resultFiles.isEmpty()) break;
}
if (resultFiles != null) {
for (Iterator<PsiFile> iterator = resultFiles.iterator(); iterator.hasNext(); ) {
if (!VfsUtilCore.isAncestor(contentRoot, iterator.next().getVirtualFile(), true)) {
iterator.remove();
}
}
final String fileName = file.getName();
final String nameWithoutExtension = file.getNameWithoutExtension();
for (PsiFile resultFile : resultFiles) {
if (resultFile instanceof PsiClassOwner) {
final PsiClass[] classes = ((PsiClassOwner)resultFile).getClasses();
if (classes.length > 0) {
ContainerUtil.addIfNotNull(locations, getLocation(project, fileName, nameWithoutExtension, classes[0]));
}
}
}
}
}
}
}
return locations;
}