From dc5a91cf120660ef75d4dc303f1ca433ef40732d Mon Sep 17 00:00:00 2001 From: Dmitry Jemerov Date: Mon, 24 Mar 2008 17:23:49 +0300 Subject: [PATCH] improve presentation for Python items in "goto symbol" and "show implementations" --- python/python.iml | 1 + .../PyElementListCellRenderer.java | 52 +++---------------- .../python/psi/impl/PyFunctionImpl.java | 8 +++ .../psi/impl/PyPresentableElementImpl.java | 25 ++++++++- 4 files changed, 41 insertions(+), 45 deletions(-) diff --git a/python/python.iml b/python/python.iml index 51178bc4c680..92b9d8bf3fe4 100644 --- a/python/python.iml +++ b/python/python.iml @@ -25,6 +25,7 @@ + diff --git a/python/src/com/jetbrains/python/codeInsight/PyElementListCellRenderer.java b/python/src/com/jetbrains/python/codeInsight/PyElementListCellRenderer.java index a94b0d360c00..c3d71db0b9ae 100644 --- a/python/src/com/jetbrains/python/codeInsight/PyElementListCellRenderer.java +++ b/python/src/com/jetbrains/python/codeInsight/PyElementListCellRenderer.java @@ -1,67 +1,31 @@ package com.jetbrains.python.codeInsight; import com.intellij.ide.util.PsiElementListCellRenderer; +import com.intellij.navigation.ItemPresentation; +import com.intellij.navigation.NavigationItem; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiNamedElement; -import com.intellij.util.ui.UIUtil; - -import javax.swing.*; -import java.awt.*; /** * @author yole */ public class PyElementListCellRenderer extends PsiElementListCellRenderer { - private static final PyLocationRenderer myLocationRenderer = new PyLocationRenderer(); - public String getElementText(final PsiElement element) { final String name = ((PsiNamedElement)element).getName(); return name == null ? "" : name; } protected String getContainerText(final PsiElement element, final String name) { + if (element instanceof NavigationItem) { + final ItemPresentation presentation = ((NavigationItem)element).getPresentation(); + if (presentation != null) { + return presentation.getLocationString(); + } + } return null; } protected int getIconFlags() { return 0; } - - protected DefaultListCellRenderer getRightCellRenderer() { - return myLocationRenderer; - } - - private static class PyLocationRenderer extends DefaultListCellRenderer { - private String myText; - - public String getText() { - return myText; - } - - public Component getListCellRendererComponent(final JList list, - final Object value, - final int index, - final boolean isSelected, - final boolean cellHasFocus) { - final Component listCellRendererComponent = super.getListCellRendererComponent(list, value, index, isSelected, - cellHasFocus); - customizeCellRenderer(value, isSelected); - return listCellRendererComponent; - } - - private void customizeCellRenderer(final Object value, final boolean selected) { - PsiElement element = (PsiElement) value; - if (element.isValid()) { - myText = element.getContainingFile().getName(); - } - else { - myText = ""; - } - setText(myText); - setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 2)); - setHorizontalTextPosition(SwingConstants.LEFT); - setBackground(selected ? UIUtil.getListSelectionBackground() : UIUtil.getListBackground()); - setForeground(selected ? UIUtil.getListSelectionForeground() : UIUtil.getInactiveTextColor()); - } - } } diff --git a/python/src/com/jetbrains/python/psi/impl/PyFunctionImpl.java b/python/src/com/jetbrains/python/psi/impl/PyFunctionImpl.java index 53b82bb79245..6d6ac6e21852 100644 --- a/python/src/com/jetbrains/python/psi/impl/PyFunctionImpl.java +++ b/python/src/com/jetbrains/python/psi/impl/PyFunctionImpl.java @@ -142,4 +142,12 @@ public class PyFunctionImpl extends PyPresentableElementImpl imp public String getDocString() { return DocStringAnnotator.findDocString(getStatementList()); } + + protected String getElementLocation() { + final PyClass containingClass = getContainingClass(); + if (containingClass != null) { + return "(" + containingClass.getName() + " in " + getPackageForFile(getContainingFile()) + ")"; + } + return super.getElementLocation(); + } } diff --git a/python/src/com/jetbrains/python/psi/impl/PyPresentableElementImpl.java b/python/src/com/jetbrains/python/psi/impl/PyPresentableElementImpl.java index 55b9ed5600aa..ffd6d85131e6 100644 --- a/python/src/com/jetbrains/python/psi/impl/PyPresentableElementImpl.java +++ b/python/src/com/jetbrains/python/psi/impl/PyPresentableElementImpl.java @@ -3,6 +3,10 @@ package com.jetbrains.python.psi.impl; import com.intellij.lang.ASTNode; import com.intellij.navigation.ItemPresentation; import com.intellij.openapi.editor.colors.TextAttributesKey; +import com.intellij.openapi.vcs.impl.ExcludedFileIndex; +import com.intellij.openapi.vfs.VirtualFile; +import com.intellij.psi.PsiDirectory; +import com.intellij.psi.PsiFile; import com.intellij.psi.PsiNamedElement; import com.intellij.psi.stubs.IStubElementType; import com.intellij.psi.stubs.StubElement; @@ -29,7 +33,7 @@ public abstract class PyPresentableElementImpl extends Py } public String getLocationString() { - return "(" + getContainingFile().getName() + ")"; + return getElementLocation(); } public Icon getIcon(final boolean open) { @@ -41,4 +45,23 @@ public abstract class PyPresentableElementImpl extends Py } }; } + + protected String getElementLocation() { + return "(" + getPackageForFile(getContainingFile()) + ")"; + } + + protected static String getPackageForFile(final PsiFile containingFile) { + final VirtualFile vFile = containingFile.getVirtualFile(); + StringBuilder result = new StringBuilder(vFile != null ? vFile.getNameWithoutExtension() : containingFile.getName()); + PsiDirectory dir = containingFile.getContainingDirectory(); + while(dir != null) { + if (!ExcludedFileIndex.getInstance(containingFile.getProject()).isInContent(dir.getVirtualFile())) { + break; + } + if (dir.findFile("__init__.py") == null) break; + result.insert(0, dir.getName() + "."); + dir = dir.getParentDirectory(); + } + return result.toString(); + } }