FIR LC: handle more kinds of annotation values
This commit is contained in:
committed by
Ilya Kirillov
parent
6ef2dad895
commit
4c8d975ae5
+4
-4
@@ -17,11 +17,11 @@ import org.jetbrains.kotlin.psi.KtElement
|
||||
|
||||
internal class FirAnnotationParameterList(
|
||||
parent: FirLightAbstractAnnotation,
|
||||
private val annotationCall: KtAnnotationCall,
|
||||
private val arguments: List<KtNamedConstantValue>,
|
||||
) : KtLightElementBase(parent), PsiAnnotationParameterList {
|
||||
|
||||
private val _attributes: Array<PsiNameValuePair> by lazyPub {
|
||||
annotationCall.arguments.map {
|
||||
arguments.map {
|
||||
FirNameValuePairForAnnotationArgument(it, this)
|
||||
}.toTypedArray()
|
||||
}
|
||||
@@ -41,7 +41,7 @@ private class FirNameValuePairForAnnotationArgument(
|
||||
override val kotlinOrigin: KtElement? get() = null
|
||||
|
||||
private val _value by lazyPub {
|
||||
(constantValue.expression as? KtSimpleConstantValue<*>)?.createPsiLiteral(this)
|
||||
constantValue.expression.toAnnotationMemberValue(this)
|
||||
}
|
||||
|
||||
override fun setValue(p0: PsiAnnotationMemberValue) = cannotModify()
|
||||
@@ -57,4 +57,4 @@ private class FirNameValuePairForAnnotationArgument(
|
||||
override fun getLiteralValue(): String? = (value as? PsiLiteralExpression)?.value?.toString()
|
||||
|
||||
override fun getName(): String = constantValue.name
|
||||
}
|
||||
}
|
||||
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* 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.light.classes.symbol
|
||||
|
||||
import com.intellij.psi.*
|
||||
import org.jetbrains.kotlin.asJava.elements.KtLightElementBase
|
||||
import org.jetbrains.kotlin.psi.KtElement
|
||||
|
||||
internal class FirPsiArrayInitializerMemberValue(
|
||||
override val kotlinOrigin: KtElement?,
|
||||
private val lightParent: PsiElement,
|
||||
private val arguments: (FirPsiArrayInitializerMemberValue) -> List<PsiAnnotationMemberValue>
|
||||
) : KtLightElementBase(lightParent), PsiArrayInitializerMemberValue {
|
||||
|
||||
override fun getInitializers(): Array<PsiAnnotationMemberValue> = arguments(this).toTypedArray()
|
||||
|
||||
override fun getParent(): PsiElement = lightParent
|
||||
override fun isPhysical(): Boolean = false
|
||||
|
||||
override fun getText(): String = "{" + initializers.joinToString { it.text } + "}"
|
||||
}
|
||||
|
||||
internal abstract class FirPsiAnnotationMemberValue(
|
||||
override val kotlinOrigin: KtElement?,
|
||||
private val lightParent: PsiElement,
|
||||
) : KtLightElementBase(lightParent), PsiAnnotationMemberValue {
|
||||
|
||||
override fun getParent(): PsiElement = lightParent
|
||||
override fun isPhysical(): Boolean = false
|
||||
}
|
||||
|
||||
internal class FirPsiExpression(
|
||||
override val kotlinOrigin: KtElement?,
|
||||
lightParent: PsiElement,
|
||||
private val psiExpression: PsiExpression,
|
||||
) : FirPsiAnnotationMemberValue(kotlinOrigin, lightParent), PsiExpression {
|
||||
override fun getType(): PsiType? = psiExpression.type
|
||||
override fun getText(): String = psiExpression.text
|
||||
}
|
||||
|
||||
internal class FirPsiLiteral(
|
||||
override val kotlinOrigin: KtElement?,
|
||||
lightParent: PsiElement,
|
||||
private val psiLiteral: PsiLiteral,
|
||||
) : FirPsiAnnotationMemberValue(kotlinOrigin, lightParent), PsiLiteral {
|
||||
override fun getValue(): Any? = psiLiteral.value
|
||||
override fun getText(): String = psiLiteral.text
|
||||
}
|
||||
+2
-2
@@ -26,7 +26,7 @@ internal class FirLightAnnotationForAnnotationCall(
|
||||
PsiImplUtil.findDeclaredAttributeValue(this, attributeName)
|
||||
|
||||
private val _parameterList: PsiAnnotationParameterList by lazyPub {
|
||||
FirAnnotationParameterList(this@FirLightAnnotationForAnnotationCall, annotationCall)
|
||||
FirAnnotationParameterList(this@FirLightAnnotationForAnnotationCall, annotationCall.arguments)
|
||||
}
|
||||
|
||||
override fun getParameterList(): PsiAnnotationParameterList = _parameterList
|
||||
@@ -47,4 +47,4 @@ internal class FirLightAnnotationForAnnotationCall(
|
||||
|
||||
override fun isEquivalentTo(another: PsiElement?): Boolean =
|
||||
basicIsEquivalentTo(this, another as? PsiAnnotation)
|
||||
}
|
||||
}
|
||||
|
||||
+18
-4
@@ -6,14 +6,19 @@
|
||||
package org.jetbrains.kotlin.light.classes.symbol
|
||||
|
||||
import com.intellij.psi.PsiAnnotationMemberValue
|
||||
import com.intellij.psi.PsiAnnotationParameterList
|
||||
import com.intellij.psi.PsiElement
|
||||
import com.intellij.psi.impl.PsiImplUtil
|
||||
import org.jetbrains.kotlin.analysis.api.symbols.markers.KtNamedConstantValue
|
||||
import org.jetbrains.kotlin.asJava.classes.lazyPub
|
||||
import org.jetbrains.kotlin.psi.KtCallElement
|
||||
|
||||
internal class FirLightSimpleAnnotation(
|
||||
private val fqName: String?,
|
||||
parent: PsiElement
|
||||
parent: PsiElement,
|
||||
private val arguments: List<KtNamedConstantValue> = listOf(),
|
||||
override val kotlinOrigin: KtCallElement? = null,
|
||||
) : FirLightAbstractAnnotation(parent) {
|
||||
override val kotlinOrigin: KtCallElement? = null
|
||||
|
||||
override fun getQualifiedName(): String? = fqName
|
||||
|
||||
@@ -25,7 +30,16 @@ internal class FirLightSimpleAnnotation(
|
||||
|
||||
override fun hashCode(): Int = fqName.hashCode()
|
||||
|
||||
override fun findAttributeValue(attributeName: String?): PsiAnnotationMemberValue? = null
|
||||
override fun findAttributeValue(attributeName: String?): PsiAnnotationMemberValue? =
|
||||
PsiImplUtil.findAttributeValue(this, attributeName)
|
||||
|
||||
override fun findDeclaredAttributeValue(attributeName: String?) =
|
||||
PsiImplUtil.findDeclaredAttributeValue(this, attributeName)
|
||||
|
||||
private val _parameterList: PsiAnnotationParameterList by lazyPub {
|
||||
FirAnnotationParameterList(this@FirLightSimpleAnnotation, arguments)
|
||||
}
|
||||
|
||||
override fun getParameterList(): PsiAnnotationParameterList = _parameterList
|
||||
|
||||
override fun findDeclaredAttributeValue(attributeName: String?): PsiAnnotationMemberValue? = null
|
||||
}
|
||||
|
||||
+34
-13
@@ -16,10 +16,7 @@ import org.jetbrains.kotlin.analysis.api.KtAnalysisSession
|
||||
import org.jetbrains.kotlin.analysis.api.components.DefaultTypeClassIds
|
||||
import org.jetbrains.kotlin.analysis.api.symbols.KtClassLikeSymbol
|
||||
import org.jetbrains.kotlin.analysis.api.symbols.KtFunctionSymbol
|
||||
import org.jetbrains.kotlin.analysis.api.symbols.markers.KtSimpleConstantValue
|
||||
import org.jetbrains.kotlin.analysis.api.symbols.markers.KtSymbolWithModality
|
||||
import org.jetbrains.kotlin.analysis.api.symbols.markers.KtSymbolWithVisibility
|
||||
import org.jetbrains.kotlin.analysis.api.symbols.markers.KtTypeAndAnnotations
|
||||
import org.jetbrains.kotlin.analysis.api.symbols.markers.*
|
||||
import org.jetbrains.kotlin.analysis.api.types.*
|
||||
import org.jetbrains.kotlin.load.kotlin.TypeMappingMode
|
||||
import org.jetbrains.kotlin.name.ClassId
|
||||
@@ -182,16 +179,41 @@ private fun escapeString(str: String): String = buildString {
|
||||
}
|
||||
}
|
||||
|
||||
private fun KtSimpleConstantValue<*>.asStringForPsiLiteral(): String =
|
||||
when (val value = this.value) {
|
||||
is String -> "\"${escapeString(value)}\""
|
||||
is Long -> "${value}L"
|
||||
is Float -> "${value}f"
|
||||
else -> value?.toString() ?: "null"
|
||||
internal fun KtConstantValue.toAnnotationMemberValue(parent: PsiElement): PsiAnnotationMemberValue? {
|
||||
return when (this) {
|
||||
is KtArrayConstantValue ->
|
||||
FirPsiArrayInitializerMemberValue(kotlinOrigin, parent) { arrayLiteralParent ->
|
||||
values.mapNotNull { element -> element.toAnnotationMemberValue(arrayLiteralParent) }
|
||||
}
|
||||
is KtAnnotationConstantValue ->
|
||||
FirLightSimpleAnnotation(fqName, parent, arguments, kotlinOrigin)
|
||||
else ->
|
||||
createPsiLiteral(parent)?.let {
|
||||
when (it) {
|
||||
is PsiLiteral -> FirPsiLiteral(kotlinOrigin, parent, it)
|
||||
else -> FirPsiExpression(kotlinOrigin, parent, it)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun KtConstantValue.asStringForPsiLiteral(): String? =
|
||||
when (this) {
|
||||
is KtEnumEntryValue ->
|
||||
"${enumEntrySymbol.containingEnumClassIdIfNonLocal?.asSingleFqName()?.asString() ?: ""}.${enumEntrySymbol.name}"
|
||||
is KtSimpleConstantValue<*> -> {
|
||||
when (val value = this.value) {
|
||||
is String -> "\"${escapeString(value)}\""
|
||||
is Long -> "${value}L"
|
||||
is Float -> "${value}f"
|
||||
else -> value?.toString() ?: "null"
|
||||
}
|
||||
}
|
||||
else -> null
|
||||
}
|
||||
|
||||
internal fun KtSimpleConstantValue<*>.createPsiLiteral(parent: PsiElement): PsiExpression? {
|
||||
val asString = asStringForPsiLiteral()
|
||||
internal fun KtConstantValue.createPsiLiteral(parent: PsiElement): PsiExpression? {
|
||||
val asString = asStringForPsiLiteral() ?: return null
|
||||
val instance = PsiElementFactory.getInstance(parent.project)
|
||||
return try {
|
||||
instance.createExpressionFromText(asString, parent)
|
||||
@@ -200,5 +222,4 @@ internal fun KtSimpleConstantValue<*>.createPsiLiteral(parent: PsiElement): PsiE
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
internal fun BitSet.copy(): BitSet = clone() as BitSet
|
||||
|
||||
Reference in New Issue
Block a user