Analysis API: fix annotation rendering for types

This commit is contained in:
Ilya Kirillov
2021-11-18 19:09:21 +01:00
parent 3a5e503f29
commit 193df3e3c4
18 changed files with 262 additions and 75 deletions
@@ -39,19 +39,13 @@ internal class ConeTypeIdeRenderer(
if (message != null) append(" <$message>")
}
private var filterExtensionFunctionType: Boolean = false
private fun StringBuilder.renderAnnotationList(annotations: List<FirAnnotation>?) {
if (annotations != null) {
val filteredExtensionIfNeeded = annotations.applyIf(filterExtensionFunctionType) {
annotations.filterNot { it.toAnnotationClassId() == StandardClassIds.Annotations.ExtensionFunctionType }
}
renderAnnotations(this@ConeTypeIdeRenderer, filteredExtensionIfNeeded, session)
private fun StringBuilder.renderAnnotationList(type: ConeKotlinType) {
if (options.renderTypeAnnotations) {
renderAnnotations(this@ConeTypeIdeRenderer, type.customAnnotations, session)
}
}
fun renderType(type: ConeTypeProjection, annotations: List<FirAnnotation>? = null): String = buildString {
fun renderType(type: ConeTypeProjection): String = buildString {
when (type) {
is ConeKotlinErrorType -> {
renderErrorType(type)
@@ -59,39 +53,36 @@ internal class ConeTypeIdeRenderer(
//is Dynamic??? -> append("dynamic")
is ConeClassLikeType -> {
if (options.renderFunctionType && shouldRenderAsPrettyFunctionType(type)) {
val oldFilterExtensionFunctionType = filterExtensionFunctionType
filterExtensionFunctionType = true
renderAnnotationList(annotations)
renderAnnotationList(type)
renderFunctionType(type)
filterExtensionFunctionType = oldFilterExtensionFunctionType
} else {
renderAnnotationList(annotations)
renderAnnotationList(type)
renderTypeConstructorAndArguments(type)
}
}
is ConeTypeParameterType -> {
renderAnnotationList(annotations)
renderAnnotationList(type)
append(type.lookupTag.name.asString())
renderNullability(type.type)
}
is ConeIntersectionType -> {
renderAnnotationList(annotations)
renderAnnotationList(type)
type.intersectedTypes.joinTo(this, "&", prefix = "(", postfix = ")") {
renderType(it)
}
renderNullability(type.type)
}
is ConeFlexibleType -> {
renderAnnotationList(annotations)
renderAnnotationList(type)
append(renderFlexibleType(renderType(type.lowerBound), renderType(type.upperBound)))
}
is ConeCapturedType -> {
renderAnnotationList(annotations)
renderAnnotationList(type)
append(type.render())
renderNullability(type.type)
}
is ConeDefinitelyNotNullType -> {
renderAnnotationList(annotations)
renderAnnotationList(type)
append(renderType(type.original))
append("!!")
}
@@ -214,7 +205,9 @@ internal class ConeTypeIdeRenderer(
else -> error("Invalid declaration ${declaration.renderWithType()}")
} ?: return listOf(declaration)
return if(containingClass.isLocal) { containingClass.collectForLocal().reversed() } else null
return if (containingClass.isLocal) {
containingClass.collectForLocal().reversed()
} else null
}
private fun StringBuilder.renderTypeConstructorAndArguments(type: ConeClassLikeType) {
@@ -6,6 +6,10 @@
package org.jetbrains.kotlin.analysis.api.fir.renderer
import org.jetbrains.kotlin.analysis.api.fir.annotations.mapAnnotationParameters
import org.jetbrains.kotlin.analysis.api.fir.evaluate.FirCompileTimeConstantEvaluator
import org.jetbrains.kotlin.analysis.api.fir.evaluate.KtFirConstantValueConverter
import org.jetbrains.kotlin.analysis.api.symbols.markers.KtConstantValueRenderer
import org.jetbrains.kotlin.analysis.api.symbols.markers.KtUnsupportedConstantValue
import org.jetbrains.kotlin.builtins.StandardNames
import org.jetbrains.kotlin.fir.FirSession
import org.jetbrains.kotlin.fir.declarations.toAnnotationClassId
@@ -47,14 +51,15 @@ private fun renderAnnotation(annotation: FirAnnotation, coneTypeIdeRenderer: Con
private fun renderAndSortAnnotationArguments(descriptor: FirAnnotation, session: FirSession): List<String> {
val argumentList = mapAnnotationParameters(descriptor, session).entries.map { (name, value) ->
"$name = ${renderConstant(value)}"
"$name = ${renderConstant(value, session)}"
}
return argumentList.sorted()
}
private fun renderConstant(value: FirExpression): String {
return when (value) {
is FirConstExpression<*> -> value.toString()
else -> "NOT_CONST_EXPRESSION"
}
private fun renderConstant(value: FirExpression, useSiteSession: FirSession): String {
val evaluated = FirCompileTimeConstantEvaluator.evaluate(value)
val constantValue = KtFirConstantValueConverter.toConstantValue(evaluated ?: value, useSiteSession)
?: KtUnsupportedConstantValue
return KtConstantValueRenderer.render(constantValue)
}
@@ -43,8 +43,8 @@ internal class FirIdeRenderer private constructor(
}
}
private fun renderType(type: ConeTypeProjection, annotations: List<FirAnnotation>? = null): String =
typeIdeRenderer.renderType(type, annotations)
private fun renderType(type: ConeTypeProjection): String =
typeIdeRenderer.renderType(type)
private fun renderType(firRef: FirTypeRef, approximate: Boolean = false): String {
require(firRef is FirResolvedTypeRef)
@@ -52,12 +52,7 @@ internal class FirIdeRenderer private constructor(
val approximatedIfNeeded = approximate.ifTrue {
PublicTypeApproximator.approximateTypeToPublicDenotable(firRef.coneType, session, approximateLocalTypes = true)
} ?: firRef.coneType
val annotations = if (RendererModifier.ANNOTATIONS in options.modifiers) {
firRef.annotations
} else {
null
}
return renderType(approximatedIfNeeded, annotations)
return renderType(approximatedIfNeeded)
}
private fun StringBuilder.renderName(declaration: FirDeclaration) {
@@ -653,7 +648,7 @@ internal class FirIdeRenderer private constructor(
append(": ")
val parameterType = typeToRender.coneType
if (isVarArg) {
append(renderType(parameterType.arrayElementType() ?: parameterType, typeToRender.annotations))
append(renderType(parameterType.arrayElementType() ?: parameterType))
} else {
append(renderType(typeToRender))
}
@@ -185,4 +185,26 @@ public class FirRendererTestGenerated extends AbstractFirRendererTest {
public void testWhere() throws Exception {
runTest("analysis/analysis-api/testData/components/symbolDeclarationRenderer/renderDeclaration/where.kt");
}
@Nested
@TestMetadata("analysis/analysis-api/testData/components/symbolDeclarationRenderer/renderDeclaration/types")
@TestDataPath("$PROJECT_ROOT")
public class Types {
@Test
public void testAllFilesPresentInTypes() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("analysis/analysis-api/testData/components/symbolDeclarationRenderer/renderDeclaration/types"), Pattern.compile("^(.+)\\.kt$"), null, true);
}
@Test
@TestMetadata("annotaionOnTypes.kt")
public void testAnnotaionOnTypes() throws Exception {
runTest("analysis/analysis-api/testData/components/symbolDeclarationRenderer/renderDeclaration/types/annotaionOnTypes.kt");
}
@Test
@TestMetadata("annotaionOnTypesWithComplexExpression.kt")
public void testAnnotaionOnTypesWithComplexExpression() throws Exception {
runTest("analysis/analysis-api/testData/components/symbolDeclarationRenderer/renderDeclaration/types/annotaionOnTypesWithComplexExpression.kt");
}
}
}