Text displayed in hovers is a little nicer now
This commit is contained in:
@@ -22,6 +22,7 @@ import org.jetbrains.jet.lang.psi.JetFunction;
|
|||||||
import org.jetbrains.jet.lang.resolve.AnalyzingUtils;
|
import org.jetbrains.jet.lang.resolve.AnalyzingUtils;
|
||||||
import org.jetbrains.jet.lang.resolve.BindingContext;
|
import org.jetbrains.jet.lang.resolve.BindingContext;
|
||||||
import org.jetbrains.jet.lang.types.FunctionDescriptor;
|
import org.jetbrains.jet.lang.types.FunctionDescriptor;
|
||||||
|
import org.jetbrains.jet.resolve.DescriptorUtil;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.awt.event.MouseEvent;
|
import java.awt.event.MouseEvent;
|
||||||
@@ -81,7 +82,7 @@ public class JetLineMarkerProvider implements LineMarkerProvider {
|
|||||||
public String getElementText(PsiElement element) {
|
public String getElementText(PsiElement element) {
|
||||||
if (element instanceof JetFunction) {
|
if (element instanceof JetFunction) {
|
||||||
JetFunction function = (JetFunction) element;
|
JetFunction function = (JetFunction) element;
|
||||||
return bindingContext.getFunctionDescriptor(function).toString();
|
return DescriptorUtil.renderPresentableText(bindingContext.getFunctionDescriptor(function));
|
||||||
}
|
}
|
||||||
return super.getElementText(element);
|
return super.getElementText(element);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import com.google.common.collect.Sets;
|
|||||||
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.types.*;
|
import org.jetbrains.jet.lang.types.*;
|
||||||
|
import org.jetbrains.jet.resolve.DescriptorUtil;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@@ -160,4 +161,9 @@ public class MutableClassDescriptor extends MutableDeclarationDescriptor impleme
|
|||||||
public boolean hasConstructors() {
|
public boolean hasConstructors() {
|
||||||
return !constructors.isEmpty();
|
return !constructors.isEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return DescriptorUtil.renderPresentableText(this) + "[" + getClass().getCanonicalName() + "@" + System.identityHashCode(this) + "]";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,140 +10,22 @@ import java.util.List;
|
|||||||
* @author abreslav
|
* @author abreslav
|
||||||
*/
|
*/
|
||||||
public class DescriptorUtil {
|
public class DescriptorUtil {
|
||||||
private DescriptorUtil() {}
|
|
||||||
|
private static final DeclarationDescriptorVisitor<Void, StringBuilder> rootVisitor = new RenderDeclarationDescriptorVisitor();
|
||||||
|
private static final DeclarationDescriptorVisitor<Void, StringBuilder> subVisitor = new RenderDeclarationDescriptorVisitor() {
|
||||||
|
@Override
|
||||||
|
protected void renderName(DeclarationDescriptor descriptor, StringBuilder stringBuilder) {
|
||||||
|
stringBuilder.append(descriptor.getName());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
public static String renderPresentableText(DeclarationDescriptor declarationDescriptor) {
|
public static String renderPresentableText(DeclarationDescriptor declarationDescriptor) {
|
||||||
if (declarationDescriptor == null) return "<null>";
|
if (declarationDescriptor == null) return "<null>";
|
||||||
StringBuilder stringBuilder = new StringBuilder();
|
StringBuilder stringBuilder = new StringBuilder();
|
||||||
declarationDescriptor.accept(
|
declarationDescriptor.accept(rootVisitor, stringBuilder);
|
||||||
new DeclarationDescriptorVisitor<Void, StringBuilder>() {
|
|
||||||
@Override
|
|
||||||
public Void visitValueParameterDescriptor(ValueParameterDescriptor descriptor, StringBuilder builder) {
|
|
||||||
builder.append("value-parameter ");
|
|
||||||
return super.visitValueParameterDescriptor(descriptor, builder);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Void visitVariableDescriptor(VariableDescriptor descriptor, StringBuilder builder) {
|
|
||||||
JetType outType = descriptor.getOutType();
|
|
||||||
JetType inType = descriptor.getInType();
|
|
||||||
String typeString = "<no type>";
|
|
||||||
if (inType != null && outType != null) {
|
|
||||||
builder.append("var ");
|
|
||||||
if (inType.equals(outType)) {
|
|
||||||
typeString = outType.toString();
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
typeString = "<in " + inType + " out " + outType + ">";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (outType != null) {
|
|
||||||
builder.append("val ");
|
|
||||||
typeString = outType.toString();
|
|
||||||
}
|
|
||||||
else if (inType != null) {
|
|
||||||
builder.append("<write-only> ");
|
|
||||||
typeString = inType.toString();
|
|
||||||
}
|
|
||||||
builder.append(renderName(descriptor)).append(" : ").append(typeString);
|
|
||||||
return super.visitVariableDescriptor(descriptor, builder);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Void visitFunctionDescriptor(FunctionDescriptor descriptor, StringBuilder builder) {
|
|
||||||
builder.append("fun ").append(renderName(descriptor));
|
|
||||||
List<TypeParameterDescriptor> typeParameters = descriptor.getTypeParameters();
|
|
||||||
renderTypeParameters(typeParameters, builder);
|
|
||||||
builder.append("(");
|
|
||||||
for (Iterator<ValueParameterDescriptor> iterator = descriptor.getUnsubstitutedValueParameters().iterator(); iterator.hasNext(); ) {
|
|
||||||
ValueParameterDescriptor parameterDescriptor = iterator.next();
|
|
||||||
visitVariableDescriptor(parameterDescriptor, builder);
|
|
||||||
if (iterator.hasNext()) {
|
|
||||||
builder.append(", ");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
builder.append(") : ").append(descriptor.getUnsubstitutedReturnType());
|
|
||||||
return super.visitFunctionDescriptor(descriptor, builder);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void renderTypeParameters(List<TypeParameterDescriptor> typeParameters, StringBuilder builder) {
|
|
||||||
if (!typeParameters.isEmpty()) {
|
|
||||||
builder.append("<");
|
|
||||||
for (Iterator<TypeParameterDescriptor> iterator = typeParameters.iterator(); iterator.hasNext(); ) {
|
|
||||||
TypeParameterDescriptor typeParameterDescriptor = iterator.next();
|
|
||||||
renderTypeParameter(typeParameterDescriptor, builder);
|
|
||||||
if (iterator.hasNext()) {
|
|
||||||
builder.append(", ");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
builder.append(">");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Void visitTypeParameterDescriptor(TypeParameterDescriptor descriptor, StringBuilder builder) {
|
|
||||||
builder.append("<");
|
|
||||||
renderTypeParameter(descriptor, builder);
|
|
||||||
builder.append(">");
|
|
||||||
return super.visitTypeParameterDescriptor(descriptor, builder);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Void visitNamespaceDescriptor(NamespaceDescriptor namespaceDescriptor, StringBuilder builder) {
|
|
||||||
builder.append("namespace ").append(renderName(namespaceDescriptor));
|
|
||||||
return super.visitNamespaceDescriptor(namespaceDescriptor, builder);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Void visitClassDescriptor(ClassDescriptor descriptor, StringBuilder builder) {
|
|
||||||
builder.append("class ").append(renderName(descriptor));
|
|
||||||
renderTypeParameters(descriptor.getTypeConstructor().getParameters(), builder);
|
|
||||||
Collection<? extends JetType> supertypes = descriptor.getTypeConstructor().getSupertypes();
|
|
||||||
if (!supertypes.isEmpty()) {
|
|
||||||
builder.append(" : ");
|
|
||||||
for (Iterator<? extends JetType> iterator = supertypes.iterator(); iterator.hasNext(); ) {
|
|
||||||
JetType supertype = iterator.next();
|
|
||||||
builder.append(supertype);
|
|
||||||
if (iterator.hasNext()) {
|
|
||||||
builder.append(", ");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return super.visitClassDescriptor(descriptor, builder);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
stringBuilder);
|
|
||||||
return stringBuilder.toString();
|
return stringBuilder.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static StringBuilder renderName(DeclarationDescriptor descriptor) {
|
|
||||||
StringBuilder stringBuilder = new StringBuilder();
|
|
||||||
renderName(descriptor, stringBuilder);
|
|
||||||
return stringBuilder;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void renderName(DeclarationDescriptor descriptor, StringBuilder stringBuilder) {
|
|
||||||
DeclarationDescriptor containingDeclaration = descriptor.getContainingDeclaration();
|
|
||||||
if (containingDeclaration != null) {
|
|
||||||
renderName(containingDeclaration, stringBuilder);
|
|
||||||
stringBuilder.append("::");
|
|
||||||
}
|
|
||||||
stringBuilder.append(descriptor.getName());
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void renderTypeParameter(TypeParameterDescriptor descriptor, StringBuilder builder) {
|
|
||||||
builder.append(renderName(descriptor));
|
|
||||||
if (!descriptor.getUpperBounds().isEmpty()) {
|
|
||||||
JetType bound = descriptor.getUpperBounds().iterator().next();
|
|
||||||
if (bound != JetStandardClasses.getAnyType()) {
|
|
||||||
builder.append(" : ").append(bound);
|
|
||||||
if (descriptor.getUpperBounds().size() > 1) {
|
|
||||||
builder.append(" (...)");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String getFQName(DeclarationDescriptor descriptor) {
|
public static String getFQName(DeclarationDescriptor descriptor) {
|
||||||
DeclarationDescriptor container = descriptor.getContainingDeclaration();
|
DeclarationDescriptor container = descriptor.getContainingDeclaration();
|
||||||
if (container != null && !(container instanceof ModuleDescriptor)) {
|
if (container != null && !(container instanceof ModuleDescriptor)) {
|
||||||
@@ -153,4 +35,129 @@ public class DescriptorUtil {
|
|||||||
|
|
||||||
return descriptor.getName();
|
return descriptor.getName();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private DescriptorUtil() {}
|
||||||
|
|
||||||
|
private static class RenderDeclarationDescriptorVisitor extends DeclarationDescriptorVisitor<Void, StringBuilder> {
|
||||||
|
@Override
|
||||||
|
public Void visitValueParameterDescriptor(ValueParameterDescriptor descriptor, StringBuilder builder) {
|
||||||
|
builder.append("value-parameter ");
|
||||||
|
return super.visitValueParameterDescriptor(descriptor, builder);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Void visitVariableDescriptor(VariableDescriptor descriptor, StringBuilder builder) {
|
||||||
|
JetType outType = descriptor.getOutType();
|
||||||
|
JetType inType = descriptor.getInType();
|
||||||
|
String typeString = "<no type>";
|
||||||
|
if (inType != null && outType != null) {
|
||||||
|
builder.append("var ");
|
||||||
|
if (inType.equals(outType)) {
|
||||||
|
typeString = outType.toString();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
typeString = "<in " + inType + " out " + outType + ">";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (outType != null) {
|
||||||
|
builder.append("val ");
|
||||||
|
typeString = outType.toString();
|
||||||
|
}
|
||||||
|
else if (inType != null) {
|
||||||
|
builder.append("<write-only> ");
|
||||||
|
typeString = inType.toString();
|
||||||
|
}
|
||||||
|
renderName(descriptor, builder);
|
||||||
|
builder.append(" : ").append(typeString);
|
||||||
|
return super.visitVariableDescriptor(descriptor, builder);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Void visitFunctionDescriptor(FunctionDescriptor descriptor, StringBuilder builder) {
|
||||||
|
builder.append("fun ");
|
||||||
|
renderName(descriptor, builder);
|
||||||
|
List<TypeParameterDescriptor> typeParameters = descriptor.getTypeParameters();
|
||||||
|
renderTypeParameters(typeParameters, builder);
|
||||||
|
builder.append("(");
|
||||||
|
for (Iterator<ValueParameterDescriptor> iterator = descriptor.getUnsubstitutedValueParameters().iterator(); iterator.hasNext(); ) {
|
||||||
|
ValueParameterDescriptor parameterDescriptor = iterator.next();
|
||||||
|
parameterDescriptor.accept(subVisitor, builder);
|
||||||
|
if (iterator.hasNext()) {
|
||||||
|
builder.append(", ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
builder.append(") : ").append(descriptor.getUnsubstitutedReturnType());
|
||||||
|
return super.visitFunctionDescriptor(descriptor, builder);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void renderTypeParameters(List<TypeParameterDescriptor> typeParameters, StringBuilder builder) {
|
||||||
|
if (!typeParameters.isEmpty()) {
|
||||||
|
builder.append("<");
|
||||||
|
for (Iterator<TypeParameterDescriptor> iterator = typeParameters.iterator(); iterator.hasNext(); ) {
|
||||||
|
TypeParameterDescriptor typeParameterDescriptor = iterator.next();
|
||||||
|
typeParameterDescriptor.accept(subVisitor, builder);
|
||||||
|
if (iterator.hasNext()) {
|
||||||
|
builder.append(", ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
builder.append(">");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Void visitTypeParameterDescriptor(TypeParameterDescriptor descriptor, StringBuilder builder) {
|
||||||
|
builder.append("<");
|
||||||
|
renderTypeParameter(descriptor, builder);
|
||||||
|
builder.append(">");
|
||||||
|
return super.visitTypeParameterDescriptor(descriptor, builder);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Void visitNamespaceDescriptor(NamespaceDescriptor namespaceDescriptor, StringBuilder builder) {
|
||||||
|
builder.append("namespace ");
|
||||||
|
renderName(namespaceDescriptor, builder);
|
||||||
|
return super.visitNamespaceDescriptor(namespaceDescriptor, builder);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Void visitClassDescriptor(ClassDescriptor descriptor, StringBuilder builder) {
|
||||||
|
builder.append("class ");
|
||||||
|
renderName(descriptor, builder);
|
||||||
|
renderTypeParameters(descriptor.getTypeConstructor().getParameters(), builder);
|
||||||
|
Collection<? extends JetType> supertypes = descriptor.getTypeConstructor().getSupertypes();
|
||||||
|
if (!supertypes.isEmpty()) {
|
||||||
|
builder.append(" : ");
|
||||||
|
for (Iterator<? extends JetType> iterator = supertypes.iterator(); iterator.hasNext(); ) {
|
||||||
|
JetType supertype = iterator.next();
|
||||||
|
builder.append(supertype);
|
||||||
|
if (iterator.hasNext()) {
|
||||||
|
builder.append(", ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return super.visitClassDescriptor(descriptor, builder);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void renderName(DeclarationDescriptor descriptor, StringBuilder stringBuilder) {
|
||||||
|
DeclarationDescriptor containingDeclaration = descriptor.getContainingDeclaration();
|
||||||
|
if (containingDeclaration != null) {
|
||||||
|
renderName(containingDeclaration, stringBuilder);
|
||||||
|
stringBuilder.append("::");
|
||||||
|
}
|
||||||
|
stringBuilder.append(descriptor.getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
private void renderTypeParameter(TypeParameterDescriptor descriptor, StringBuilder builder) {
|
||||||
|
renderName(descriptor, builder);
|
||||||
|
if (!descriptor.getUpperBounds().isEmpty()) {
|
||||||
|
JetType bound = descriptor.getUpperBounds().iterator().next();
|
||||||
|
if (bound != JetStandardClasses.getAnyType()) {
|
||||||
|
builder.append(" : ").append(bound);
|
||||||
|
if (descriptor.getUpperBounds().size() > 1) {
|
||||||
|
builder.append(" (...)");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user