[Analysis API] rework renderer

^KTIJ-23268 fixed
This commit is contained in:
Ilya Kirillov
2022-10-28 10:35:23 +02:00
parent f8ed993307
commit d1eb7c51f1
110 changed files with 3814 additions and 181 deletions
@@ -10,6 +10,7 @@ import org.jetbrains.kotlin.analysis.api.KtAnalysisSession
import org.jetbrains.kotlin.analysis.api.components.*
import org.jetbrains.kotlin.analysis.api.descriptors.components.*
import org.jetbrains.kotlin.analysis.api.impl.base.components.KtAnalysisScopeProviderImpl
import org.jetbrains.kotlin.analysis.api.impl.base.components.KtRendererProviderImpl
import org.jetbrains.kotlin.analysis.api.lifetime.KtLifetimeToken
import org.jetbrains.kotlin.analysis.api.lifetime.withValidityAssertion
import org.jetbrains.kotlin.analysis.api.symbols.KtSymbolProvider
@@ -41,7 +42,7 @@ class KtFe10AnalysisSession(
override val symbolDeclarationOverridesProviderImpl: KtSymbolDeclarationOverridesProvider =
KtFe10SymbolDeclarationOverridesProvider(this)
override val referenceShortenerImpl: KtReferenceShortener = KtFe10ReferenceShortener(this)
override val symbolDeclarationRendererProviderImpl: KtSymbolDeclarationRendererProvider = KtFe10SymbolDeclarationRendererProvider(this)
override val symbolDeclarationRendererProviderImpl: KtSymbolDeclarationRendererProvider = KtRendererProviderImpl(this, token)
override val expressionTypeProviderImpl: KtExpressionTypeProvider = KtFe10ExpressionTypeProvider(this)
override val psiTypeProviderImpl: KtPsiTypeProvider = KtFe10PsiTypeProvider(this)
override val typeProviderImpl: KtTypeProvider = KtFe10TypeProvider(this)
@@ -1,43 +0,0 @@
/*
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.analysis.api.descriptors.components
import org.jetbrains.kotlin.analysis.api.components.KtDeclarationRendererOptions
import org.jetbrains.kotlin.analysis.api.components.KtSymbolDeclarationRendererProvider
import org.jetbrains.kotlin.analysis.api.components.KtTypeRendererOptions
import org.jetbrains.kotlin.analysis.api.descriptors.KtFe10AnalysisSession
import org.jetbrains.kotlin.analysis.api.descriptors.components.base.Fe10KtAnalysisSessionComponent
import org.jetbrains.kotlin.analysis.api.descriptors.symbols.descriptorBased.base.getSymbolDescriptor
import org.jetbrains.kotlin.analysis.api.descriptors.symbols.descriptorBased.base.render
import org.jetbrains.kotlin.analysis.api.descriptors.types.base.KtFe10Type
import org.jetbrains.kotlin.analysis.api.descriptors.utils.KtFe10TypeRenderer
import org.jetbrains.kotlin.analysis.api.symbols.KtDeclarationSymbol
import org.jetbrains.kotlin.analysis.api.lifetime.KtLifetimeToken
import org.jetbrains.kotlin.analysis.api.signatures.KtCallableSignature
import org.jetbrains.kotlin.analysis.api.types.KtType
import org.jetbrains.kotlin.analysis.utils.printer.prettyPrint
internal class KtFe10SymbolDeclarationRendererProvider(
override val analysisSession: KtFe10AnalysisSession
) : KtSymbolDeclarationRendererProvider(), Fe10KtAnalysisSessionComponent {
override val token: KtLifetimeToken
get() = analysisSession.token
override fun renderDeclaration(symbol: KtDeclarationSymbol, options: KtDeclarationRendererOptions): String {
val descriptor = getSymbolDescriptor(symbol)
if (descriptor != null) {
return descriptor.render(analysisContext, options)
}
// Rendering for unresolved symbols is not implemented
return ""
}
override fun render(type: KtType, options: KtTypeRendererOptions): String {
require(type is KtFe10Type)
return prettyPrint { KtFe10TypeRenderer(options).render(type.type, this) }
}
}
@@ -67,9 +67,14 @@ internal class KtFe10TypeProvider(
override val builtinTypes: KtBuiltinTypes by lazy(LazyThreadSafetyMode.PUBLICATION) { KtFe10BuiltinTypes(analysisContext) }
override fun approximateToSuperPublicDenotableType(type: KtType): KtType? {
override fun approximateToSuperPublicDenotableType(type: KtType, approximateLocalTypes: Boolean): KtType? {
require(type is KtFe10Type)
return typeApproximator.approximateToSuperType(type.type, PublicApproximatorConfiguration)?.toKtType(analysisContext)
return typeApproximator.approximateToSuperType(type.type, PublicApproximatorConfiguration(approximateLocalTypes))?.toKtType(analysisContext)
}
override fun approximateToSubPublicDenotableType(type: KtType, approximateLocalTypes: Boolean): KtType? {
require(type is KtFe10Type)
return typeApproximator.approximateToSubType(type.type, PublicApproximatorConfiguration(approximateLocalTypes))?.toKtType(analysisContext)
}
override fun buildSelfClassType(symbol: KtNamedClassOrObjectSymbol): KtType {
@@ -73,7 +73,7 @@ internal class KtFe10Renderer(
private fun KtFe10RendererConsumer.renderType(type: KotlinType, shouldApproximate: Boolean = false) {
if (shouldApproximate) {
val approximatedType = typeApproximator.approximateToSuperType(type.unwrap(), PublicApproximatorConfiguration)
val approximatedType = typeApproximator.approximateToSuperType(type.unwrap(), PublicApproximatorConfiguration(localTypes = true))
?: type.takeIf { it.constructor.declarationDescriptor?.name != SpecialNames.NO_NAME_PROVIDED }
?: analysisContext.builtIns.anyType
@@ -8,11 +8,10 @@ package org.jetbrains.kotlin.analysis.api.descriptors.utils
import org.jetbrains.kotlin.types.TypeApproximatorConfiguration
@Suppress("SpellCheckingInspection")
internal object PublicApproximatorConfiguration : TypeApproximatorConfiguration.AllFlexibleSameValue() {
internal class PublicApproximatorConfiguration(override val localTypes: Boolean) : TypeApproximatorConfiguration.AllFlexibleSameValue() {
override val allFlexible: Boolean get() = false
override val errorType: Boolean get() = true
override val definitelyNotNullType: Boolean get() = false
override val integerLiteralConstantType: Boolean get() = true
override val intersectionTypesInContravariantPositions: Boolean get() = true
override val localTypes: Boolean get() = true
}