Reduced number of constant renderers in DescriptorRenderer

This commit is contained in:
Valentin Kipyatkov
2015-06-05 18:46:42 +03:00
parent 4263c47f4d
commit ef550e1f21
7 changed files with 39 additions and 50 deletions
@@ -17,6 +17,7 @@
package org.jetbrains.kotlin.diagnostics.rendering; package org.jetbrains.kotlin.diagnostics.rendering;
import com.intellij.openapi.extensions.ExtensionPointName; import com.intellij.openapi.extensions.ExtensionPointName;
import kotlin.Unit;
import kotlin.jvm.functions.Function1; import kotlin.jvm.functions.Function1;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@@ -29,6 +30,8 @@ import org.jetbrains.kotlin.lexer.JetModifierKeywordToken;
import org.jetbrains.kotlin.psi.JetExpression; import org.jetbrains.kotlin.psi.JetExpression;
import org.jetbrains.kotlin.psi.JetSimpleNameExpression; import org.jetbrains.kotlin.psi.JetSimpleNameExpression;
import org.jetbrains.kotlin.psi.JetTypeConstraint; import org.jetbrains.kotlin.psi.JetTypeConstraint;
import org.jetbrains.kotlin.renderer.DescriptorRenderer;
import org.jetbrains.kotlin.renderer.DescriptorRendererOptions;
import org.jetbrains.kotlin.renderer.MultiRenderer; import org.jetbrains.kotlin.renderer.MultiRenderer;
import org.jetbrains.kotlin.renderer.Renderer; import org.jetbrains.kotlin.renderer.Renderer;
import org.jetbrains.kotlin.resolve.varianceChecker.VarianceChecker.VarianceConflictDiagnosticData; import org.jetbrains.kotlin.resolve.varianceChecker.VarianceChecker.VarianceConflictDiagnosticData;
@@ -94,6 +97,18 @@ public class DefaultErrorMessages {
return null; return null;
} }
public static final DescriptorRenderer DEPRECATION_RENDERER = DescriptorRenderer.ONLY_NAMES_WITH_SHORT_TYPES.withOptions(
new Function1<DescriptorRendererOptions, Unit>() {
@Override
public Unit invoke(DescriptorRendererOptions options) {
options.setWithoutTypeParameters(false);
options.setReceiverAfterName(false);
options.setRenderAccessors(true);
return Unit.INSTANCE$;
}
}
);
static { static {
MAP.put(UNRESOLVED_REFERENCE, "Unresolved reference: {0}", ELEMENT_TEXT); MAP.put(UNRESOLVED_REFERENCE, "Unresolved reference: {0}", ELEMENT_TEXT);
@@ -268,8 +283,8 @@ public class DefaultErrorMessages {
MAP.put(MANY_COMPANION_OBJECTS, "Only one companion object is allowed per class"); MAP.put(MANY_COMPANION_OBJECTS, "Only one companion object is allowed per class");
MAP.put(COMPANION_OBJECT_NOT_ALLOWED, "A companion object is not allowed here"); MAP.put(COMPANION_OBJECT_NOT_ALLOWED, "A companion object is not allowed here");
MAP.put(DEPRECATED_SYMBOL, "''{0}'' is deprecated.", DEPRECATION); MAP.put(DEPRECATED_SYMBOL, "''{0}'' is deprecated.", DEPRECATION_RENDERER);
MAP.put(DEPRECATED_SYMBOL_WITH_MESSAGE, "''{0}'' is deprecated. {1}", DEPRECATION, STRING); MAP.put(DEPRECATED_SYMBOL_WITH_MESSAGE, "''{0}'' is deprecated. {1}", DEPRECATION_RENDERER, STRING);
MAP.put(LOCAL_OBJECT_NOT_ALLOWED, "Named object ''{0}'' is a singleton and cannot be local. Try to use anonymous object instead", NAME); MAP.put(LOCAL_OBJECT_NOT_ALLOWED, "Named object ''{0}'' is a singleton and cannot be local. Try to use anonymous object instead", NAME);
MAP.put(LOCAL_ENUM_NOT_ALLOWED, "Enum class ''{0}'' cannot be local", NAME); MAP.put(LOCAL_ENUM_NOT_ALLOWED, "Enum class ''{0}'' cannot be local", NAME);
@@ -25,3 +25,4 @@ public fun <P> renderParameter(parameter: P, renderer: Renderer<P>?): Any = rend
public fun ClassDescriptor.renderKindWithName(): String = DescriptorRenderer.getClassKindPrefix(this) + " '" + getName() + "'" public fun ClassDescriptor.renderKindWithName(): String = DescriptorRenderer.getClassKindPrefix(this) + " '" + getName() + "'"
public fun ClassDescriptor.renderKind(): String = DescriptorRenderer.getClassKindPrefix(this) public fun ClassDescriptor.renderKind(): String = DescriptorRenderer.getClassKindPrefix(this)
@@ -99,12 +99,6 @@ public interface DescriptorRenderer : Renderer<DeclarationDescriptor> {
parameterNameRenderingPolicy = ParameterNameRenderingPolicy.ONLY_NON_SYNTHESIZED parameterNameRenderingPolicy = ParameterNameRenderingPolicy.ONLY_NON_SYNTHESIZED
} }
public val STARTS_FROM_NAME: DescriptorRenderer = withOptions {
withDefinedIn = false
modifiers = emptySet()
startFromName = true
}
public val ONLY_NAMES_WITH_SHORT_TYPES: DescriptorRenderer = withOptions { public val ONLY_NAMES_WITH_SHORT_TYPES: DescriptorRenderer = withOptions {
withDefinedIn = false withDefinedIn = false
modifiers = emptySet() modifiers = emptySet()
@@ -133,43 +127,10 @@ public interface DescriptorRenderer : Renderer<DeclarationDescriptor> {
flexibleTypesForCode = true flexibleTypesForCode = true
} }
public val HTML_COMPACT_WITH_MODIFIERS: DescriptorRenderer = withOptions {
withDefinedIn = false
textFormat = RenderingFormat.HTML
}
public val HTML_NAMES_WITH_SHORT_TYPES: DescriptorRenderer = withOptions {
withDefinedIn = false
nameShortness = NameShortness.SHORT
renderCompanionObjectName = true
textFormat = RenderingFormat.HTML
}
public val HTML: DescriptorRenderer = withOptions { public val HTML: DescriptorRenderer = withOptions {
textFormat = RenderingFormat.HTML textFormat = RenderingFormat.HTML
} }
public val HTML_FOR_UNINFERRED_TYPE_PARAMS: DescriptorRenderer = withOptions {
uninferredTypeParameterAsName = true
modifiers = emptySet()
nameShortness = NameShortness.SHORT
textFormat = RenderingFormat.HTML
}
public val DEPRECATION: DescriptorRenderer = withOptions {
withDefinedIn = false
modifiers = emptySet()
nameShortness = NameShortness.SHORT
withoutTypeParameters = false
parameterNameRenderingPolicy = ParameterNameRenderingPolicy.NONE
receiverAfterName = false
renderCompanionObjectName = true
renderAccessors = true
withoutSuperTypes = true
renderDefaultValues = false
startFromName = true
}
public fun getClassKindPrefix(klass: ClassDescriptor): String { public fun getClassKindPrefix(klass: ClassDescriptor): String {
if (klass.isCompanionObject()) { if (klass.isCompanionObject()) {
return "companion object" return "companion object"
@@ -110,7 +110,7 @@ public class IdeErrorMessages {
MAP.put(MANY_IMPL_MEMBER_NOT_IMPLEMENTED, "<html>{0} must override {1}<br />because it inherits many implementations of it</html>", MAP.put(MANY_IMPL_MEMBER_NOT_IMPLEMENTED, "<html>{0} must override {1}<br />because it inherits many implementations of it</html>",
RENDER_CLASS_OR_OBJECT, DescriptorRenderer.HTML); RENDER_CLASS_OR_OBJECT, DescriptorRenderer.HTML);
MAP.put(CONFLICTING_OVERLOADS, "<html>''{0}''<br />is already defined in {1}</html>", MAP.put(CONFLICTING_OVERLOADS, "<html>''{0}''<br />is already defined in {1}</html>",
DescriptorRenderer.HTML_COMPACT_WITH_MODIFIERS, STRING); IdeRenderers.HTML_COMPACT_WITH_MODIFIERS, STRING);
MAP.put(RESULT_TYPE_MISMATCH, "<html>Function return type mismatch." + MAP.put(RESULT_TYPE_MISMATCH, "<html>Function return type mismatch." +
"<table><tr><td>Expected:</td><td>{1}</td></tr>" + "<table><tr><td>Expected:</td><td>{1}</td></tr>" +
@@ -70,14 +70,18 @@ public object IdeRenderers {
DescriptorRenderer.HTML.renderType(returnType) DescriptorRenderer.HTML.renderType(returnType)
} }
public val HTML_CONFLICTING_JVM_DECLARATIONS_DATA: Renderer<ConflictingJvmDeclarationsData> = Renderer<ConflictingJvmDeclarationsData> { public val HTML_COMPACT_WITH_MODIFIERS: DescriptorRenderer = DescriptorRenderer.HTML.withOptions {
withDefinedIn = false
}
public val HTML_CONFLICTING_JVM_DECLARATIONS_DATA: Renderer<ConflictingJvmDeclarationsData> = Renderer {
data: ConflictingJvmDeclarationsData -> data: ConflictingJvmDeclarationsData ->
val conflicts = data.signatureOrigins val conflicts = data.signatureOrigins
.map { it.descriptor } .map { it.descriptor }
.filterNotNull() .filterNotNull()
.sortBy(MemberComparator.INSTANCE) .sortBy(MemberComparator.INSTANCE)
.joinToString("") { "<li>" + DescriptorRenderer.HTML_COMPACT_WITH_MODIFIERS.render(it) + "</li>\n" } .joinToString("") { "<li>" + HTML_COMPACT_WITH_MODIFIERS.render(it) + "</li>\n" }
"The following declarations have the same JVM signature (<code>${data.signature.name}${data.signature.desc}</code>):<br/>\n<ul>\n$conflicts</ul>" "The following declarations have the same JVM signature (<code>${data.signature.name}${data.signature.desc}</code>):<br/>\n<ul>\n$conflicts</ul>"
} }
@@ -21,6 +21,8 @@ import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor
import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor
import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.renderer.DescriptorRenderer import org.jetbrains.kotlin.renderer.DescriptorRenderer
import org.jetbrains.kotlin.renderer.NameShortness
import org.jetbrains.kotlin.renderer.RenderingFormat
import org.jetbrains.kotlin.resolve.DescriptorUtils import org.jetbrains.kotlin.resolve.DescriptorUtils
import org.jetbrains.kotlin.resolve.calls.callUtil.hasTypeMismatchErrorOnParameter import org.jetbrains.kotlin.resolve.calls.callUtil.hasTypeMismatchErrorOnParameter
import org.jetbrains.kotlin.resolve.calls.callUtil.hasUnmappedArguments import org.jetbrains.kotlin.resolve.calls.callUtil.hasUnmappedArguments
@@ -36,8 +38,15 @@ public fun renderError(o: Any): String = RED_TEMPLATE.format(o)
public fun renderStrong(o: Any, error: Boolean): String = (if (error) RED_TEMPLATE else STRONG_TEMPLATE).format(o) public fun renderStrong(o: Any, error: Boolean): String = (if (error) RED_TEMPLATE else STRONG_TEMPLATE).format(o)
private val HTML_FOR_UNINFERRED_TYPE_PARAMS: DescriptorRenderer = DescriptorRenderer.withOptions {
uninferredTypeParameterAsName = true
modifiers = emptySet()
nameShortness = NameShortness.SHORT
textFormat = RenderingFormat.HTML
}
fun <D : CallableDescriptor> renderResolvedCall(resolvedCall: ResolvedCall<D>): String { fun <D : CallableDescriptor> renderResolvedCall(resolvedCall: ResolvedCall<D>): String {
val htmlRenderer = DescriptorRenderer.HTML_FOR_UNINFERRED_TYPE_PARAMS val htmlRenderer = HTML_FOR_UNINFERRED_TYPE_PARAMS
val stringBuilder = StringBuilder("") val stringBuilder = StringBuilder("")
val indent = "&nbsp;&nbsp;" val indent = "&nbsp;&nbsp;"
@@ -83,13 +83,12 @@ public class KotlinQuickDocumentationProvider : AbstractDocumentationProvider()
companion object { companion object {
private val LOG = Logger.getInstance(javaClass<KotlinQuickDocumentationProvider>()) private val LOG = Logger.getInstance(javaClass<KotlinQuickDocumentationProvider>())
val quickDocNameFormat = DescriptorRenderer.withOptions { private val DESCRIPTOR_RENDERER = DescriptorRenderer.HTML.withOptions {
withDefinedIn = true
nameShortness = NameShortness.SHORT nameShortness = NameShortness.SHORT
renderCompanionObjectName = true renderCompanionObjectName = true
textFormat = RenderingFormat.HTML
} }
private val MIX_KOTLIN_TO_JAVA_RENDERER = DESCRIPTOR_RENDERER.withOptions { withDefinedIn = false }
private fun getText(element: PsiElement, originalElement: PsiElement?, quickNavigation: Boolean): String? { private fun getText(element: PsiElement, originalElement: PsiElement?, quickNavigation: Boolean): String? {
if (element is JetDeclaration) { if (element is JetDeclaration) {
@@ -126,7 +125,7 @@ public class KotlinQuickDocumentationProvider : AbstractDocumentationProvider()
return "No documentation available" return "No documentation available"
} }
var renderedDecl = quickDocNameFormat.render(declarationDescriptor) var renderedDecl = DESCRIPTOR_RENDERER.render(declarationDescriptor)
if (!quickNavigation) { if (!quickNavigation) {
renderedDecl = "<pre>" + renderedDecl + "</pre>" renderedDecl = "<pre>" + renderedDecl + "</pre>"
} }
@@ -141,7 +140,7 @@ public class KotlinQuickDocumentationProvider : AbstractDocumentationProvider()
private fun mixKotlinToJava(declarationDescriptor: DeclarationDescriptor, element: PsiElement, originalElement: PsiElement?): String? { private fun mixKotlinToJava(declarationDescriptor: DeclarationDescriptor, element: PsiElement, originalElement: PsiElement?): String? {
val originalInfo = JavaDocumentationProvider().getQuickNavigateInfo(element, originalElement) val originalInfo = JavaDocumentationProvider().getQuickNavigateInfo(element, originalElement)
if (originalInfo != null) { if (originalInfo != null) {
val renderedDecl = DescriptorRenderer.HTML_NAMES_WITH_SHORT_TYPES.render(declarationDescriptor) val renderedDecl = MIX_KOTLIN_TO_JAVA_RENDERER.render(declarationDescriptor)
return renderedDecl + "<br/>Java declaration:<br/>" + originalInfo return renderedDecl + "<br/>Java declaration:<br/>" + originalInfo
} }