Analysis API FIR: fix rendering FirField

This commit is contained in:
Ilya Kirillov
2021-11-30 13:07:43 +01:00
parent 68e3c72a64
commit d5f6809f76
2 changed files with 20 additions and 19 deletions
@@ -13,6 +13,7 @@ import org.jetbrains.kotlin.analysis.api.fir.symbols.KtFirSymbol
import org.jetbrains.kotlin.analysis.api.fir.types.KtFirType import org.jetbrains.kotlin.analysis.api.fir.types.KtFirType
import org.jetbrains.kotlin.analysis.api.fir.types.PublicTypeApproximator import org.jetbrains.kotlin.analysis.api.fir.types.PublicTypeApproximator
import org.jetbrains.kotlin.analysis.api.fir.utils.toConeNullability import org.jetbrains.kotlin.analysis.api.fir.utils.toConeNullability
import org.jetbrains.kotlin.analysis.api.symbols.KtCallableSymbol
import org.jetbrains.kotlin.analysis.api.symbols.KtNamedClassOrObjectSymbol import org.jetbrains.kotlin.analysis.api.symbols.KtNamedClassOrObjectSymbol
import org.jetbrains.kotlin.analysis.api.symbols.markers.KtPossibleMemberSymbol import org.jetbrains.kotlin.analysis.api.symbols.markers.KtPossibleMemberSymbol
import org.jetbrains.kotlin.analysis.api.tokens.ValidityToken import org.jetbrains.kotlin.analysis.api.tokens.ValidityToken
@@ -187,7 +188,7 @@ internal class KtFirTypeProvider(
.mapTo(mutableListOf()) { it.asKtType() } .mapTo(mutableListOf()) { it.asKtType() }
} }
override fun getDispatchReceiverType(symbol: KtPossibleMemberSymbol): KtType? { override fun getDispatchReceiverType(symbol: KtCallableSymbol): KtType? {
require(symbol is KtFirSymbol<*>) require(symbol is KtFirSymbol<*>)
return symbol.firRef.withFir { declaration -> return symbol.firRef.withFir { declaration ->
@@ -27,7 +27,7 @@ internal class FirIdeRenderer private constructor(
options: KtDeclarationRendererOptions, options: KtDeclarationRendererOptions,
session: FirSession, session: FirSession,
) : FirIdeRendererBase(options, session) { ) : FirIdeRendererBase(options, session) {
fun PrettyPrinter.renderMemberDeclaration(declaration: FirMemberDeclaration) { fun PrettyPrinter.renderMemberDeclaration(declaration: FirDeclaration) {
when (declaration) { when (declaration) {
is FirAnonymousObject -> renderAnonymousObject(declaration) is FirAnonymousObject -> renderAnonymousObject(declaration)
is FirRegularClass -> renderRegularClass(declaration) is FirRegularClass -> renderRegularClass(declaration)
@@ -37,7 +37,7 @@ internal class FirIdeRenderer private constructor(
is FirSimpleFunction -> renderSimpleFunction(declaration) is FirSimpleFunction -> renderSimpleFunction(declaration)
is FirBackingField -> renderBackingField() is FirBackingField -> renderBackingField()
is FirEnumEntry -> renderEnumEntry(declaration) is FirEnumEntry -> renderEnumEntry(declaration)
is FirProperty -> renderProperty(declaration) is FirProperty -> renderPropertyOrField(declaration)
is FirValueParameter -> renderValueParameter(declaration) is FirValueParameter -> renderValueParameter(declaration)
is FirField -> renderPropertyOrField(declaration) is FirField -> renderPropertyOrField(declaration)
is FirErrorFunction -> error("FirErrorFunction should not be rendered") is FirErrorFunction -> error("FirErrorFunction should not be rendered")
@@ -46,9 +46,6 @@ internal class FirIdeRenderer private constructor(
is FirFile -> error("FirFile should not be rendered") is FirFile -> error("FirFile should not be rendered")
is FirTypeParameter -> renderTypeParameter(declaration) is FirTypeParameter -> renderTypeParameter(declaration)
is FirAnonymousFunction -> TODO() is FirAnonymousFunction -> TODO()
is FirErrorFunction -> Unit
is FirErrorProperty -> Unit
is FirBackingField -> Unit
} }
} }
@@ -56,30 +53,33 @@ internal class FirIdeRenderer private constructor(
append("field") append("field")
} }
private fun PrettyPrinter.renderProperty(property: FirProperty) { private fun PrettyPrinter.renderPropertyOrField(variable: FirVariable) {
renderAnnotationsAndModifiers(property) check(variable is FirProperty || variable is FirField) {
renderValVarPrefix(property) "Required either FirProperty or FirField but was ${variable::class.simpleName}"
renderTypeParameters(property) }
renderReceiver(property) renderAnnotationsAndModifiers(variable)
renderName(property) renderValVarPrefix(variable)
renderTypeParameters(variable)
renderReceiver(variable)
renderName(variable)
append(": ") append(": ")
renderType(property.returnTypeRef, approximate = options.approximateTypes) renderType(variable.returnTypeRef, approximate = options.approximateTypes)
renderWhereSuffix(property) renderWhereSuffix(variable)
fun FirPropertyAccessor?.needToRender() = this != null && (annotations.isNotEmpty() || visibility != property.visibility) fun FirPropertyAccessor?.needToRender() = this != null && (annotations.isNotEmpty() || visibility != variable.visibility)
val needToRenderAccessors = options.renderClassMembers && val needToRenderAccessors = options.renderClassMembers &&
(property.getter.needToRender() || (property.isVar && property.setter.needToRender())) (variable.getter.needToRender() || (variable.isVar && variable.setter.needToRender()))
if (needToRenderAccessors) { if (needToRenderAccessors) {
withIndent { withIndent {
property.getter?.let { getter -> variable.getter?.let { getter ->
if (getter.needToRender()) { if (getter.needToRender()) {
appendLine() appendLine()
renderPropertyAccessor(getter) renderPropertyAccessor(getter)
} }
} }
property.setter?.let { setter -> variable.setter?.let { setter ->
if (setter.needToRender()) { if (setter.needToRender()) {
appendLine() appendLine()
renderPropertyAccessor(setter) renderPropertyAccessor(setter)
@@ -341,7 +341,7 @@ internal class FirIdeRenderer private constructor(
companion object { companion object {
fun render( fun render(
firDeclaration: FirMemberDeclaration, firDeclaration: FirDeclaration,
options: KtDeclarationRendererOptions, options: KtDeclarationRendererOptions,
session: FirSession session: FirSession
): String { ): String {