[FIR] Improve readability of rendered types in diagnostics
#KT-61824 Fixed #KT-61688 Fixed
This commit is contained in:
committed by
Space Team
parent
99b852adf8
commit
4e1dfcd2a8
@@ -5,12 +5,18 @@
|
||||
|
||||
package org.jetbrains.kotlin.fir.renderer
|
||||
|
||||
import org.jetbrains.kotlin.fir.types.ConeAttributes
|
||||
import org.jetbrains.kotlin.fir.types.ConeAttribute
|
||||
|
||||
abstract class ConeAttributeRenderer {
|
||||
abstract fun render(attributes: ConeAttributes): String
|
||||
abstract fun render(attributes: Iterable<ConeAttribute<*>>): String
|
||||
|
||||
object ToString : ConeAttributeRenderer() {
|
||||
override fun render(attributes: ConeAttributes): String = attributes.joinToString(separator = " ", postfix = " ") { it.toString() }
|
||||
override fun render(attributes: Iterable<ConeAttribute<*>>): String =
|
||||
attributes.joinToString(separator = " ", postfix = " ") { it.toString() }
|
||||
}
|
||||
|
||||
object ForReadability : ConeAttributeRenderer() {
|
||||
override fun render(attributes: Iterable<ConeAttribute<*>>): String =
|
||||
attributes.joinToString(separator = " ", postfix = " ") { it.renderForReadability() }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ package org.jetbrains.kotlin.fir.renderer
|
||||
|
||||
import org.jetbrains.kotlin.builtins.functions.FunctionTypeKind
|
||||
import org.jetbrains.kotlin.fir.types.*
|
||||
import org.jetbrains.kotlin.utils.addToStdlib.ifNotEmpty
|
||||
|
||||
open class ConeTypeRenderer(
|
||||
private val attributeRenderer: ConeAttributeRenderer = ConeAttributeRenderer.ToString
|
||||
@@ -180,16 +181,16 @@ open class ConeTypeRenderer(
|
||||
builder.append(">")
|
||||
}
|
||||
|
||||
private fun ConeKotlinType.renderAttributes() {
|
||||
protected open fun ConeKotlinType.renderAttributes() {
|
||||
if (!attributes.any()) return
|
||||
builder.append(attributeRenderer.render(attributes))
|
||||
}
|
||||
|
||||
private fun ConeKotlinType.renderNonCompilerAttributes() {
|
||||
protected fun ConeKotlinType.renderNonCompilerAttributes() {
|
||||
val compilerAttributes = CompilerConeAttributes.classIdByCompilerAttributeKey
|
||||
if (attributes.any { it.key !in compilerAttributes }) {
|
||||
builder.append(attributeRenderer.render(attributes))
|
||||
}
|
||||
attributes
|
||||
.filter { it.key !in compilerAttributes }
|
||||
.ifNotEmpty { builder.append(attributeRenderer.render(this)) }
|
||||
}
|
||||
|
||||
private fun ConeTypeProjection.render() {
|
||||
|
||||
+6
-2
@@ -8,13 +8,13 @@ package org.jetbrains.kotlin.fir.renderer
|
||||
import org.jetbrains.kotlin.builtins.StandardNames
|
||||
import org.jetbrains.kotlin.fir.types.ConeFlexibleType
|
||||
import org.jetbrains.kotlin.fir.types.ConeIntegerLiteralType
|
||||
import org.jetbrains.kotlin.fir.types.ConeKotlinType
|
||||
import org.jetbrains.kotlin.renderer.replacePrefixesInTypeRepresentations
|
||||
import org.jetbrains.kotlin.renderer.typeStringsDifferOnlyInNullability
|
||||
|
||||
class ConeTypeRendererForReadability(
|
||||
private val idRendererCreator: () -> ConeIdRenderer,
|
||||
) : ConeTypeRenderer() {
|
||||
|
||||
) : ConeTypeRenderer(ConeAttributeRenderer.ForReadability) {
|
||||
constructor(builder: StringBuilder, idRendererCreator: () -> ConeIdRenderer) : this(idRendererCreator) {
|
||||
this.builder = builder
|
||||
this.idRenderer = idRendererCreator()
|
||||
@@ -76,4 +76,8 @@ class ConeTypeRendererForReadability(
|
||||
override fun render(type: ConeIntegerLiteralType) {
|
||||
render(type.getApproximatedType())
|
||||
}
|
||||
|
||||
override fun ConeKotlinType.renderAttributes() {
|
||||
renderNonCompilerAttributes()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,6 +30,7 @@ abstract class ConeAttribute<out T : ConeAttribute<T>> : AnnotationMarker {
|
||||
abstract fun isSubtypeOf(other: @UnsafeVariance T?): Boolean
|
||||
|
||||
abstract override fun toString(): String
|
||||
open fun renderForReadability(): String = toString()
|
||||
|
||||
abstract val key: KClass<out T>
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user