mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
improve presentation for Python items in "goto symbol" and "show implementations"
This commit is contained in:
@@ -25,6 +25,7 @@
|
||||
</library>
|
||||
</orderEntry>
|
||||
<orderEntry type="module" module-name="platform-impl" />
|
||||
<orderEntry type="module" module-name="vcs-api" />
|
||||
<orderEntryProperties />
|
||||
</component>
|
||||
<component name="copyright">
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -142,4 +142,12 @@ public class PyFunctionImpl extends PyPresentableElementImpl<PyFunctionStub> 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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<T extends StubElement> extends Py
|
||||
}
|
||||
|
||||
public String getLocationString() {
|
||||
return "(" + getContainingFile().getName() + ")";
|
||||
return getElementLocation();
|
||||
}
|
||||
|
||||
public Icon getIcon(final boolean open) {
|
||||
@@ -41,4 +45,23 @@ public abstract class PyPresentableElementImpl<T extends StubElement> 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();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user