IDEA-154706 Show relative paths in Favorites (from project root folder)

This commit is contained in:
Vassiliy.Kudryashov
2016-04-15 14:26:45 +03:00
parent a60a7c4fab
commit 375e08dd15
4 changed files with 31 additions and 7 deletions
@@ -116,6 +116,20 @@ public class VfsUtilCore {
return FileUtil.isAncestor(ancestor, file, strict);
}
/**
* Gets relative path of <code>file</code> to <code>root</code> when it's possible
* This method is designed to be used for file descriptions (in trees, lists etc.)
* @param file the file
* @param root candidate to be parent file (Project base dir, any content roots etc.)
* @return relative path of {@code file} or full path if {@code root} is not actual ancestor of {@code file}
*/
@Nullable
public static String getRelativeLocation(@Nullable VirtualFile file, @NotNull VirtualFile root) {
if (file == null) return null;
String path = getRelativePath(file, root);
return path != null ? path : file.getPresentableUrl();
}
@Nullable
public static String getRelativePath(@NotNull VirtualFile file, @NotNull VirtualFile ancestor) {
return getRelativePath(file, ancestor, VFS_SEPARATOR_CHAR);
@@ -20,6 +20,7 @@ import com.intellij.openapi.editor.markup.TextAttributes;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vcs.FileStatus;
import com.intellij.openapi.vcs.FileStatusManager;
import com.intellij.openapi.vfs.VfsUtilCore;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
@@ -76,6 +77,7 @@ public class BookmarkItem extends ItemWrapper implements Comparable<BookmarkItem
renderer.append(":", SimpleTextAttributes.GRAYED_ATTRIBUTES);
renderer.append(String.valueOf(bookmark.getLine() + 1), SimpleTextAttributes.GRAYED_ATTRIBUTES);
}
renderer.append(" (" + VfsUtilCore.getRelativeLocation(file, project.getBaseDir()) + ")", SimpleTextAttributes.GRAY_ATTRIBUTES);
if (!selected) {
FileColorManager colorManager = FileColorManager.getInstance(project);
@@ -79,7 +79,7 @@ public class FavoritesPanel {
o = ((DefaultMutableTreeNode)o).getUserObject();
if (o instanceof FavoritesTreeNodeDescriptor) {
FavoritesTreeNodeDescriptor root = ((FavoritesTreeNodeDescriptor)o).getFavoritesRoot();
if (root != o) {
if (root != null && root != o) {
o = root.getElement();
if (o instanceof FavoritesListNode && ((FavoritesListNode)o).getProvider() == null) {
return new DnDDragStartBean(path);
@@ -25,11 +25,16 @@ import com.intellij.ide.util.treeView.PresentableNodeDescriptor;
import com.intellij.openapi.extensions.Extensions;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.VfsUtil;
import com.intellij.openapi.vfs.VfsUtilCore;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.SmartPsiElementPointer;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.File;
/**
* User: anna
@@ -59,19 +64,18 @@ public class FavoritesTreeNodeDescriptor extends PresentableNodeDescriptor<Abstr
return getLocation(myElement, myProject);
}
public static String getLocation(final AbstractTreeNode element, final Project project) {
public static String getLocation(final AbstractTreeNode element, @NotNull final Project project) {
Object nodeElement = element.getValue();
if (nodeElement instanceof SmartPsiElementPointer) {
nodeElement = ((SmartPsiElementPointer)nodeElement).getElement();
}
if (nodeElement instanceof PsiElement) {
if (nodeElement instanceof PsiDirectory) {
return ((PsiDirectory)nodeElement).getVirtualFile().getPresentableUrl();
return VfsUtilCore.getRelativeLocation(((PsiDirectory)nodeElement).getVirtualFile(), project.getBaseDir());
}
if (nodeElement instanceof PsiFile) {
final PsiFile containingFile = (PsiFile)nodeElement;
final VirtualFile virtualFile = containingFile.getVirtualFile();
return virtualFile != null ? virtualFile.getPresentableUrl() : "";
return VfsUtilCore.getRelativeLocation(containingFile.getVirtualFile(), project.getBaseDir());
}
}
@@ -83,6 +87,9 @@ public class FavoritesTreeNodeDescriptor extends PresentableNodeDescriptor<Abstr
final Module module = namedLibraryElement.getModule();
return (module != null ? module.getName() : "") + ":" + namedLibraryElement.getOrderEntry().getPresentableName();
}
if (nodeElement instanceof File) {
return VfsUtilCore.getRelativeLocation(VfsUtil.findFileByIoFile((File)nodeElement, false), project.getBaseDir());
}
final FavoriteNodeProvider[] nodeProviders = Extensions.getExtensions(FavoriteNodeProvider.EP_NAME, project);
for (FavoriteNodeProvider provider : nodeProviders) {
@@ -106,9 +113,10 @@ public class FavoritesTreeNodeDescriptor extends PresentableNodeDescriptor<Abstr
return myElement.hashCode();
}
@Nullable
public FavoritesTreeNodeDescriptor getFavoritesRoot() {
FavoritesTreeNodeDescriptor descriptor = this;
while (descriptor.getParentDescriptor() instanceof FavoritesTreeNodeDescriptor) {
while (descriptor != null && descriptor.getParentDescriptor() instanceof FavoritesTreeNodeDescriptor) {
FavoritesTreeNodeDescriptor parent = (FavoritesTreeNodeDescriptor)descriptor.getParentDescriptor();
if (parent != null && parent.getParentDescriptor() == null) {
return descriptor;