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(); }
@NotNull private static String getFileFqn(final PsiFile file) { final VirtualFile virtualFile = file.getVirtualFile(); if (virtualFile == null) { return file.getName(); } final Project project = file.getProject(); final LogicalRoot logicalRoot = LogicalRootsManager.getLogicalRootsManager(project).findLogicalRoot(virtualFile); if (logicalRoot != null) { String logical = FileUtil.toSystemIndependentName(VfsUtil.virtualToIoFile(logicalRoot.getVirtualFile()).getPath()); String path = FileUtil.toSystemIndependentName(VfsUtil.virtualToIoFile(virtualFile).getPath()); return "/" + FileUtil.getRelativePath(logical, path, '/'); } final VirtualFile contentRoot = ProjectRootManager.getInstance(project).getFileIndex().getContentRootForFile(virtualFile); if (contentRoot != null) { return "/" + FileUtil.getRelativePath(VfsUtil.virtualToIoFile(contentRoot), VfsUtil.virtualToIoFile(virtualFile)); } return virtualFile.getPath(); }
@Nonnull public static String getFileFqn(final PsiFile file) { final VirtualFile virtualFile = file.getVirtualFile(); if (virtualFile == null) { return file.getName(); } final Project project = file.getProject(); final LogicalRoot logicalRoot = LogicalRootsManager.getLogicalRootsManager(project).findLogicalRoot(virtualFile); if (logicalRoot != null) { String logical = FileUtil.toSystemIndependentName(VfsUtil.virtualToIoFile(logicalRoot.getVirtualFile()).getPath()); String path = FileUtil.toSystemIndependentName(VfsUtil.virtualToIoFile(virtualFile).getPath()); return "/" + FileUtil.getRelativePath(logical, path, '/'); } final VirtualFile contentRoot = ProjectRootManager.getInstance(project).getFileIndex().getContentRootForFile(virtualFile); if (contentRoot != null) { return "/" + FileUtil.getRelativePath(VfsUtil.virtualToIoFile(contentRoot), VfsUtil.virtualToIoFile(virtualFile)); } return virtualFile.getPath(); }
@Nullable private static LogicalRoot findLogicalRoot(PsiElement element) { if (element == null) return null; final PsiFile psiFile = element.getContainingFile(); if (psiFile == null) return null; final VirtualFile file = psiFile.getVirtualFile(); if (file == null) return null; return LogicalRootsManager.getLogicalRootsManager(element.getProject()).findLogicalRoot(file); }