Removed renderTypeWithShortNames from API of DescriptorRenderer.

This commit is contained in:
Evgeny Gerashchenko
2012-12-11 22:22:16 +04:00
parent cd2e619364
commit f6e9b0e234
6 changed files with 30 additions and 32 deletions
@@ -81,6 +81,7 @@ public class DescriptorRendererImpl implements Renderer<DeclarationDescriptor> {
}
};
public static final DescriptorRendererImpl TEXT = new DescriptorRendererImpl();
public static final DescriptorRendererImpl SHORT_NAMES_IN_TYPES = new DescriptorRendererImpl(true);
public static final DescriptorRendererImpl DEBUG_TEXT = new DescriptorRendererImpl() {
@Override
protected boolean hasDefaultValue(ValueParameterDescriptor descriptor) {
@@ -108,6 +109,16 @@ public class DescriptorRendererImpl implements Renderer<DeclarationDescriptor> {
};
private final RenderDeclarationDescriptorVisitor rootVisitor = new RenderDeclarationDescriptorVisitor();
private final boolean shortNames;
public DescriptorRendererImpl() {
this(false);
}
public DescriptorRendererImpl(boolean shortNames) {
this.shortNames = shortNames;
}
protected boolean hasDefaultValue(ValueParameterDescriptor descriptor) {
return descriptor.hasDefaultValue();
}
@@ -139,14 +150,6 @@ public class DescriptorRendererImpl implements Renderer<DeclarationDescriptor> {
}
public String renderType(JetType type) {
return renderType(type, false);
}
public String renderTypeWithShortNames(JetType type) {
return renderType(type, true);
}
private String renderType(JetType type, boolean shortNamesOnly) {
if (type == null) {
return escape("[NULL]");
}
@@ -157,20 +160,20 @@ public class DescriptorRendererImpl implements Renderer<DeclarationDescriptor> {
return escape(KotlinBuiltIns.UNIT_ALIAS + (type.isNullable() ? "?" : ""));
}
else if (KotlinBuiltIns.getInstance().isFunctionType(type)) {
return escape(renderFunctionType(type, shortNamesOnly));
return escape(renderFunctionType(type));
}
else {
return escape(renderDefaultType(type, shortNamesOnly));
return escape(renderDefaultType(type));
}
}
private String renderDefaultType(JetType type, boolean shortNamesOnly) {
private String renderDefaultType(JetType type) {
StringBuilder sb = new StringBuilder();
sb.append(renderTypeName(type.getConstructor(), shortNamesOnly));
sb.append(renderTypeName(type.getConstructor()));
if (!type.getArguments().isEmpty()) {
sb.append("<");
appendTypeProjections(sb, type.getArguments(), shortNamesOnly);
appendTypeProjections(sb, type.getArguments());
sb.append(">");
}
if (type.isNullable()) {
@@ -179,7 +182,7 @@ public class DescriptorRendererImpl implements Renderer<DeclarationDescriptor> {
return sb.toString();
}
private String renderTypeName(@NotNull TypeConstructor typeConstructor, boolean shortNamesOnly) {
private String renderTypeName(@NotNull TypeConstructor typeConstructor) {
ClassifierDescriptor cd = typeConstructor.getDeclarationDescriptor();
if (cd == null) {
return typeConstructor.toString();
@@ -188,7 +191,7 @@ public class DescriptorRendererImpl implements Renderer<DeclarationDescriptor> {
return renderName(cd.getName());
}
else {
if (shortNamesOnly) {
if (shortNames) {
List<Name> qualifiedNameElements = Lists.newArrayList();
// for nested classes qualified name should be used
@@ -209,32 +212,32 @@ public class DescriptorRendererImpl implements Renderer<DeclarationDescriptor> {
}
}
private void appendTypeProjections(StringBuilder result, List<TypeProjection> typeProjections, boolean shortNamesOnly) {
private void appendTypeProjections(StringBuilder result, List<TypeProjection> typeProjections) {
for (Iterator<TypeProjection> iterator = typeProjections.iterator(); iterator.hasNext(); ) {
TypeProjection typeProjection = iterator.next();
if (typeProjection.getProjectionKind() != Variance.INVARIANT) {
result.append(typeProjection.getProjectionKind()).append(" ");
}
result.append(renderType(typeProjection.getType(), shortNamesOnly));
result.append(renderType(typeProjection.getType()));
if (iterator.hasNext()) {
result.append(", ");
}
}
}
private String renderFunctionType(JetType type, boolean shortNamesOnly) {
private String renderFunctionType(JetType type) {
StringBuilder sb = new StringBuilder();
JetType receiverType = KotlinBuiltIns.getInstance().getReceiverType(type);
if (receiverType != null) {
sb.append(renderType(receiverType, shortNamesOnly));
sb.append(renderType(receiverType));
sb.append(".");
}
sb.append("(");
appendTypeProjections(sb, KotlinBuiltIns.getInstance().getParameterTypeProjectionsFromFunctionType(type), shortNamesOnly);
appendTypeProjections(sb, KotlinBuiltIns.getInstance().getParameterTypeProjectionsFromFunctionType(type));
sb.append(") -> ");
sb.append(renderType(KotlinBuiltIns.getInstance().getReturnTypeFromFunctionType(type), shortNamesOnly));
sb.append(renderType(KotlinBuiltIns.getInstance().getReturnTypeFromFunctionType(type)));
if (type.isNullable()) {
return "(" + sb + ")?";
@@ -106,7 +106,7 @@ public class TypeSubstitutorTest extends KotlinTestWithEnvironment {
JetType result = substitutor.substitute(initialType, Variance.INVARIANT);
assertEquals(expectedTypeStr, DescriptorRendererImpl.TEXT.renderTypeWithShortNames(result));
assertEquals(expectedTypeStr, DescriptorRendererImpl.SHORT_NAMES_IN_TYPES.renderType(result));
}
private Map<TypeConstructor, TypeProjection> stringsToSubstitutionMap(Pair<String, String>[] substitutionStrs) {
@@ -182,7 +182,7 @@ public class GenerateJavaToKotlinMethodMap {
// "special case": remove(Int) and remove(Any?) in MutableList
if (method.getName().equals("remove") && method.getContainingClass().getName().equals("List")) {
String psiType = method.getParameterList().getParameters()[0].getType().getPresentableText();
String jetType = DescriptorRendererImpl.TEXT.renderTypeWithShortNames(fun.getValueParameters().get(0).getType());
String jetType = DescriptorRendererImpl.SHORT_NAMES_IN_TYPES.renderType(fun.getValueParameters().get(0).getType());
String string = psiType + "|" + jetType;
return "int|Int".equals(string) || "Object|Any?".equals(string);
@@ -141,8 +141,8 @@ public abstract class OverrideImplementMethodsHandler implements LanguageCodeIns
addReceiverParameter(descriptor, bodyBuilder);
bodyBuilder.append(descriptor.getName()).append(" : ").append(DescriptorRendererImpl.COMPACT_WITH_MODIFIERS.renderTypeWithShortNames(
descriptor.getType()));
bodyBuilder.append(descriptor.getName()).append(" : ").append(
DescriptorRendererImpl.SHORT_NAMES_IN_TYPES.renderType(descriptor.getType()));
String initializer = defaultInitializer(descriptor.getType(), KotlinBuiltIns.getInstance());
if (initializer != null) {
bodyBuilder.append(" = ").append(initializer);
@@ -53,17 +53,12 @@ import static org.jetbrains.jet.plugin.codeInsight.ktSignature.KotlinSignatureUt
* @since 16 Aug 2012
*/
public class AddKotlinSignatureAnnotation extends BaseIntentionAction implements Iconable {
private static final DescriptorRendererImpl RENDERER = new DescriptorRendererImpl() {
private static final DescriptorRendererImpl RENDERER = new DescriptorRendererImpl(true) {
@Override
protected boolean shouldRenderDefinedIn() {
return false;
}
@Override
public String renderType(JetType type) {
return renderTypeWithShortNames(type);
}
@Override
protected boolean shouldRenderModifiers() {
return false;
@@ -151,7 +151,7 @@ public class JetFunctionParameterInfoHandler implements
builder.append("vararg ");
}
builder.append(descriptor.getName()).append(": ").
append(DescriptorRendererImpl.TEXT.renderTypeWithShortNames(getActualParameterType(descriptor)));
append(DescriptorRendererImpl.SHORT_NAMES_IN_TYPES.renderType(getActualParameterType(descriptor)));
if (descriptor.hasDefaultValue()) {
PsiElement element = BindingContextUtils.descriptorToDeclaration(bindingContext, descriptor);
String defaultExpression = "?";