Revise JetStructureViewElement
- remove 'my' prefix - inline ItemPresentation - use descriptor renderer instead of custom one
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2010-2013 JetBrains s.r.o.
|
* Copyright 2010-2014 JetBrains s.r.o.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -54,6 +54,13 @@ public interface DescriptorRenderer extends Renderer<DeclarationDescriptor> {
|
|||||||
.setModifiers()
|
.setModifiers()
|
||||||
.setStartFromName(true).build();
|
.setStartFromName(true).build();
|
||||||
|
|
||||||
|
DescriptorRenderer STARTS_FROM_NAME_WITH_SHORT_TYPES = new DescriptorRendererBuilder()
|
||||||
|
.setWithDefinedIn(false)
|
||||||
|
.setUnitReturnType(false)
|
||||||
|
.setModifiers()
|
||||||
|
.setShortNames(true)
|
||||||
|
.setStartFromName(true).build();
|
||||||
|
|
||||||
DescriptorRenderer FQ_NAMES_IN_TYPES = new DescriptorRendererBuilder().build();
|
DescriptorRenderer FQ_NAMES_IN_TYPES = new DescriptorRendererBuilder().build();
|
||||||
|
|
||||||
DescriptorRenderer SHORT_NAMES_IN_TYPES = new DescriptorRendererBuilder().setShortNames(true).setIncludeSynthesizedParameterNames(false).build();
|
DescriptorRenderer SHORT_NAMES_IN_TYPES = new DescriptorRendererBuilder().setShortNames(true).setIncludeSynthesizedParameterNames(false).build();
|
||||||
|
|||||||
@@ -22,110 +22,93 @@ import com.intellij.navigation.ColoredItemPresentation;
|
|||||||
import com.intellij.navigation.ItemPresentation;
|
import com.intellij.navigation.ItemPresentation;
|
||||||
import com.intellij.openapi.editor.colors.CodeInsightColors;
|
import com.intellij.openapi.editor.colors.CodeInsightColors;
|
||||||
import com.intellij.openapi.editor.colors.TextAttributesKey;
|
import com.intellij.openapi.editor.colors.TextAttributesKey;
|
||||||
|
import com.intellij.openapi.util.Iconable;
|
||||||
import com.intellij.openapi.util.text.StringUtil;
|
import com.intellij.openapi.util.text.StringUtil;
|
||||||
import com.intellij.psi.NavigatablePsiElement;
|
import com.intellij.psi.NavigatablePsiElement;
|
||||||
|
import com.intellij.util.ArrayUtil;
|
||||||
import com.intellij.util.Function;
|
import com.intellij.util.Function;
|
||||||
import com.intellij.util.PsiIconUtil;
|
import com.intellij.util.PsiIconUtil;
|
||||||
|
import com.intellij.util.containers.ContainerUtil;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
import org.jetbrains.jet.lang.descriptors.*;
|
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
|
||||||
import org.jetbrains.jet.lang.psi.*;
|
import org.jetbrains.jet.lang.psi.*;
|
||||||
import org.jetbrains.jet.lang.resolve.BindingContext;
|
import org.jetbrains.jet.plugin.JetDescriptorIconProvider;
|
||||||
import org.jetbrains.jet.lang.resolve.DescriptorUtils;
|
|
||||||
import org.jetbrains.jet.lang.types.JetType;
|
|
||||||
import org.jetbrains.jet.plugin.caches.resolve.ResolvePackage;
|
import org.jetbrains.jet.plugin.caches.resolve.ResolvePackage;
|
||||||
import org.jetbrains.jet.renderer.DescriptorRenderer;
|
import org.jetbrains.jet.renderer.DescriptorRenderer;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class JetStructureViewElement implements StructureViewTreeElement {
|
public class JetStructureViewElement implements StructureViewTreeElement, ColoredItemPresentation {
|
||||||
private final NavigatablePsiElement myElement;
|
private final NavigatablePsiElement element;
|
||||||
|
|
||||||
// For file context will be updated after each construction
|
|
||||||
// For other tree sub-elements it's immutable.
|
|
||||||
private BindingContext context;
|
|
||||||
|
|
||||||
private String elementText;
|
private String elementText;
|
||||||
|
|
||||||
public JetStructureViewElement(NavigatablePsiElement element, BindingContext context) {
|
public JetStructureViewElement(NavigatablePsiElement element) {
|
||||||
myElement = element;
|
this.element = element;
|
||||||
this.context = context;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public JetStructureViewElement(JetFile fileElement) {
|
public JetStructureViewElement(@NotNull JetFile fileElement) {
|
||||||
myElement = fileElement;
|
element = fileElement;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static DeclarationDescriptor getDescriptor(JetDeclaration declaration) {
|
||||||
|
return declaration.isValid() ? ResolvePackage.getLazyResolveSession(declaration).resolveToDescriptor(declaration) : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public NavigatablePsiElement getElement() {
|
||||||
|
return element;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object getValue() {
|
public Object getValue() {
|
||||||
return myElement;
|
return element;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void navigate(boolean requestFocus) {
|
public void navigate(boolean requestFocus) {
|
||||||
myElement.navigate(requestFocus);
|
element.navigate(requestFocus);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean canNavigate() {
|
public boolean canNavigate() {
|
||||||
return myElement.canNavigate();
|
return element.canNavigate();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean canNavigateToSource() {
|
public boolean canNavigateToSource() {
|
||||||
return myElement.canNavigateToSource();
|
return element.canNavigateToSource();
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public ItemPresentation getPresentation() {
|
public ItemPresentation getPresentation() {
|
||||||
return new ColoredItemPresentation() {
|
return this;
|
||||||
@Override
|
|
||||||
public String getPresentableText() {
|
|
||||||
if (elementText == null) {
|
|
||||||
elementText = getElementText();
|
|
||||||
}
|
|
||||||
|
|
||||||
return elementText;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getLocationString() {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Icon getIcon(boolean unused) {
|
|
||||||
if (myElement.isValid()) {
|
|
||||||
return PsiIconUtil.getProvidersIcon(myElement, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Nullable
|
|
||||||
@Override
|
|
||||||
public TextAttributesKey getTextAttributesKey() {
|
|
||||||
if (myElement instanceof JetModifierListOwner && JetPsiUtil.isDeprecated((JetModifierListOwner) myElement)) {
|
|
||||||
return CodeInsightColors.DEPRECATED_ATTRIBUTES;
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public TreeElement[] getChildren() {
|
public TreeElement[] getChildren() {
|
||||||
if (myElement instanceof JetFile) {
|
List<JetDeclaration> childrenDeclarations = getChildrenDeclarations();
|
||||||
JetFile jetFile = (JetFile) myElement;
|
return ArrayUtil.toObjectArray(ContainerUtil.map(childrenDeclarations, new Function<JetDeclaration, TreeElement>() {
|
||||||
context = ResolvePackage.getBindingContext(jetFile);
|
@Override
|
||||||
return wrapDeclarations(jetFile.getDeclarations());
|
public TreeElement fun(JetDeclaration declaration) {
|
||||||
|
return new JetStructureViewElement(declaration);
|
||||||
|
}
|
||||||
|
}), TreeElement.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<JetDeclaration> getChildrenDeclarations() {
|
||||||
|
if (element instanceof JetFile) {
|
||||||
|
JetFile jetFile = (JetFile) element;
|
||||||
|
return jetFile.getDeclarations();
|
||||||
}
|
}
|
||||||
else if (myElement instanceof JetClass) {
|
else if (element instanceof JetClass) {
|
||||||
JetClass jetClass = (JetClass) myElement;
|
JetClass jetClass = (JetClass) element;
|
||||||
List<JetDeclaration> declarations = new ArrayList<JetDeclaration>();
|
List<JetDeclaration> declarations = new ArrayList<JetDeclaration>();
|
||||||
for (JetParameter parameter : jetClass.getPrimaryConstructorParameters()) {
|
for (JetParameter parameter : jetClass.getPrimaryConstructorParameters()) {
|
||||||
if (parameter.hasValOrVarNode()) {
|
if (parameter.hasValOrVarNode()) {
|
||||||
@@ -133,105 +116,87 @@ public class JetStructureViewElement implements StructureViewTreeElement {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
declarations.addAll(jetClass.getDeclarations());
|
declarations.addAll(jetClass.getDeclarations());
|
||||||
return wrapDeclarations(declarations);
|
return declarations;
|
||||||
}
|
}
|
||||||
else if (myElement instanceof JetClassOrObject) {
|
else if (element instanceof JetClassOrObject) {
|
||||||
return wrapDeclarations(((JetClassOrObject) myElement).getDeclarations());
|
return ((JetClassOrObject) element).getDeclarations();
|
||||||
}
|
}
|
||||||
else if (myElement instanceof JetClassObject) {
|
else if (element instanceof JetClassObject) {
|
||||||
JetObjectDeclaration objectDeclaration = ((JetClassObject) myElement).getObjectDeclaration();
|
JetObjectDeclaration objectDeclaration = ((JetClassObject) element).getObjectDeclaration();
|
||||||
return wrapDeclarations(objectDeclaration.getDeclarations());
|
return objectDeclaration.getDeclarations();
|
||||||
}
|
}
|
||||||
|
|
||||||
return EMPTY_ARRAY;
|
return Collections.emptyList();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
@Override
|
||||||
|
public TextAttributesKey getTextAttributesKey() {
|
||||||
|
if (element instanceof JetModifierListOwner && JetPsiUtil.isDeprecated((JetModifierListOwner) element)) {
|
||||||
|
return CodeInsightColors.DEPRECATED_ATTRIBUTES;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
@Override
|
||||||
|
public String getPresentableText() {
|
||||||
|
if (elementText == null) {
|
||||||
|
elementText = getElementText();
|
||||||
|
}
|
||||||
|
|
||||||
|
return elementText;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
@Override
|
||||||
|
public String getLocationString() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
@Override
|
||||||
|
public Icon getIcon(boolean unused) {
|
||||||
|
if (!element.isValid()) return null;
|
||||||
|
|
||||||
|
if (element instanceof JetDeclaration) {
|
||||||
|
DeclarationDescriptor descriptor = getDescriptor((JetDeclaration) element);
|
||||||
|
if (descriptor != null) {
|
||||||
|
JetDescriptorIconProvider.getIcon(descriptor, element, Iconable.ICON_FLAG_VISIBILITY);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return PsiIconUtil.getProvidersIcon(element, Iconable.ICON_FLAG_VISIBILITY);
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getElementText() {
|
private String getElementText() {
|
||||||
String text = "";
|
String text = "";
|
||||||
|
|
||||||
// Try to find text in correspondent descriptor
|
// Try to find text in correspondent descriptor
|
||||||
if (myElement instanceof JetDeclaration) {
|
if (element instanceof JetDeclaration) {
|
||||||
JetDeclaration declaration = (JetDeclaration) myElement;
|
JetDeclaration declaration = (JetDeclaration) element;
|
||||||
|
|
||||||
DeclarationDescriptor descriptor = context.get(BindingContext.DECLARATION_TO_DESCRIPTOR, declaration);
|
DeclarationDescriptor descriptor = getDescriptor(declaration);
|
||||||
if (descriptor != null) {
|
if (descriptor != null) {
|
||||||
text = getDescriptorTreeText(descriptor);
|
//text = getDescriptorTreeText(descriptor);
|
||||||
|
text = DescriptorRenderer.STARTS_FROM_NAME_WITH_SHORT_TYPES.render(descriptor);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (StringUtil.isEmpty(text)) {
|
if (StringUtil.isEmpty(text)) {
|
||||||
text = myElement.getName();
|
text = element.getName();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (StringUtil.isEmpty(text)) {
|
if (StringUtil.isEmpty(text)) {
|
||||||
if (myElement instanceof JetClassInitializer) {
|
if (element instanceof JetClassInitializer) {
|
||||||
return "<class initializer>";
|
return "<class initializer>";
|
||||||
}
|
}
|
||||||
|
|
||||||
if (myElement instanceof JetClassObject) {
|
if (element instanceof JetClassObject) {
|
||||||
return "<class object>";
|
return "<class object>";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return text;
|
return text;
|
||||||
}
|
}
|
||||||
|
|
||||||
private TreeElement[] wrapDeclarations(List<JetDeclaration> declarations) {
|
|
||||||
TreeElement[] result = new TreeElement[declarations.size()];
|
|
||||||
for (int i = 0; i < declarations.size(); i++) {
|
|
||||||
result[i] = new JetStructureViewElement(declarations.get(i), context);
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String getDescriptorTreeText(@NotNull DeclarationDescriptor descriptor) {
|
|
||||||
StringBuilder textBuilder;
|
|
||||||
|
|
||||||
if (descriptor instanceof FunctionDescriptor) {
|
|
||||||
textBuilder = new StringBuilder();
|
|
||||||
|
|
||||||
FunctionDescriptor functionDescriptor = (FunctionDescriptor) descriptor;
|
|
||||||
ReceiverParameterDescriptor receiver = functionDescriptor.getReceiverParameter();
|
|
||||||
if (receiver != null) {
|
|
||||||
textBuilder.append(DescriptorRenderer.FQ_NAMES_IN_TYPES.renderType(receiver.getType())).append(".");
|
|
||||||
}
|
|
||||||
|
|
||||||
textBuilder.append(functionDescriptor.getName());
|
|
||||||
|
|
||||||
String parametersString = StringUtil.join(
|
|
||||||
functionDescriptor.getValueParameters(),
|
|
||||||
new Function<ValueParameterDescriptor, String>() {
|
|
||||||
@Override
|
|
||||||
public String fun(ValueParameterDescriptor valueParameterDescriptor) {
|
|
||||||
return valueParameterDescriptor.getName() + ":" +
|
|
||||||
DescriptorRenderer.FQ_NAMES_IN_TYPES.renderType(valueParameterDescriptor.getType());
|
|
||||||
}
|
|
||||||
},
|
|
||||||
",");
|
|
||||||
|
|
||||||
textBuilder.append("(").append(parametersString).append(")");
|
|
||||||
|
|
||||||
JetType returnType = functionDescriptor.getReturnType();
|
|
||||||
assert returnType != null;
|
|
||||||
textBuilder.append(":").append(DescriptorRenderer.FQ_NAMES_IN_TYPES.renderType(returnType));
|
|
||||||
}
|
|
||||||
else if (descriptor instanceof VariableDescriptor) {
|
|
||||||
JetType outType = ((VariableDescriptor) descriptor).getType();
|
|
||||||
|
|
||||||
textBuilder = new StringBuilder(descriptor.getName().asString());
|
|
||||||
textBuilder.append(":").append(DescriptorRenderer.FQ_NAMES_IN_TYPES.renderType(outType));
|
|
||||||
}
|
|
||||||
else if (descriptor instanceof ClassDescriptor) {
|
|
||||||
textBuilder = new StringBuilder(descriptor.getName().asString());
|
|
||||||
textBuilder
|
|
||||||
.append(" (")
|
|
||||||
.append(DescriptorUtils.getFqName(descriptor.getContainingDeclaration()))
|
|
||||||
.append(")");
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return DescriptorRenderer.FQ_NAMES_IN_TYPES.render(descriptor);
|
|
||||||
}
|
|
||||||
|
|
||||||
return textBuilder.toString();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user