Uast: Migrate to UAnnotation

This commit is contained in:
Yan Zhulanow
2016-10-17 19:14:17 +03:00
committed by Yan Zhulanow
parent fee54d9b86
commit 6ff29f473b
73 changed files with 402 additions and 81 deletions
@@ -16,8 +16,9 @@
package org.jetbrains.uast.kotlin
import org.jetbrains.kotlin.psi.KtElement
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.psi.KtAnnotatedExpression
import org.jetbrains.kotlin.psi.KtExpression
import org.jetbrains.uast.UAnnotation
import org.jetbrains.uast.UElement
import org.jetbrains.uast.UExpression
import org.jetbrains.uast.psi.PsiElementBacked
@@ -32,4 +33,11 @@ abstract class KotlinAbstractUElement : UElement {
}
}
abstract class KotlinAbstractUExpression : KotlinAbstractUElement(), UExpression
abstract class KotlinAbstractUExpression : KotlinAbstractUElement(), UExpression {
override val annotations: List<UAnnotation>
get() {
val psi = (this as? PsiElementBacked)?.psi as? KtExpression ?: return emptyList()
val annotatedExpression = psi.parent as? KtAnnotatedExpression ?: return emptyList()
return annotatedExpression.annotationEntries.map { KotlinUAnnotation(it, this) }
}
}
@@ -18,7 +18,9 @@ package org.jetbrains.uast.kotlin
import com.intellij.lang.Language
import com.intellij.openapi.project.Project
import com.intellij.psi.*
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile
import com.intellij.psi.PsiVariable
import com.intellij.psi.impl.source.tree.LeafPsiElement
import org.jetbrains.kotlin.asJava.LightClassUtil
import org.jetbrains.kotlin.asJava.classes.KtLightClass
@@ -0,0 +1,40 @@
package org.jetbrains.uast.kotlin
import com.intellij.psi.PsiClass
import org.jetbrains.kotlin.psi.KtAnnotationEntry
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
import org.jetbrains.uast.*
class KotlinUAnnotation(
override val psi: KtAnnotationEntry,
override val containingElement: UElement?
) : UAnnotation {
private val resolvedAnnotation by lz { psi.analyze()[BindingContext.ANNOTATION, psi] }
override val qualifiedName: String?
get() = resolvedAnnotation?.type?.constructor?.declarationDescriptor?.fqNameSafe?.asString()
override val attributeValues by lz {
val context = getUastContext()
psi.valueArguments.map { arg ->
val name = arg.getArgumentName()?.asName?.asString() ?: ""
UNamedExpression(name, this).apply {
val value = arg.getArgumentExpression()?.let { context.convertElement(it, this) } as? UExpression
expression = value ?: UastEmptyExpression
}
}
}
override fun resolve(): PsiClass? {
val descriptor = resolvedAnnotation?.type?.constructor?.declarationDescriptor ?: return null
return descriptor.toSource()?.getMaybeLightElement(this) as? PsiClass
}
//TODO
override fun findAttributeValue(name: String?) = findDeclaredAttributeValue(name)
override fun findDeclaredAttributeValue(name: String?): UNamedExpression? {
return attributeValues.firstOrNull { it.matchesName(name ?: "value") }
}
}
@@ -52,6 +52,9 @@ class KotlinUClass private constructor(
override val containingElement: UElement?
get() = containingMethod
override val annotations: List<UAnnotation>
get() = emptyList()
override val expressions by lz {
initializers.map {
getLanguagePlugin().convertOpt<UExpression>(it.body, this) ?: UastEmptyExpression
@@ -26,6 +26,9 @@ class KotlinUFile(override val psi: KtFile, override val languagePlugin: UastLan
override val packageName: String
get() = psi.packageFqName.asString()
override val annotations: List<UAnnotation>
get() = psi.annotationEntries.map { KotlinUAnnotation(it, this) }
override val allCommentsInFile by lz {
val comments = ArrayList<UComment>(0)
psi.accept(object : PsiRecursiveElementWalkingVisitor() {
@@ -16,7 +16,6 @@
package org.jetbrains.uast.kotlin
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.idea.references.mainReference
import org.jetbrains.kotlin.psi.KtExpression
import org.jetbrains.kotlin.psi.KtImportDirective
@@ -19,6 +19,8 @@ package org.jetbrains.uast.kotlin.declarations
import org.jetbrains.kotlin.asJava.elements.KtLightElement
import org.jetbrains.kotlin.asJava.elements.KtLightMethod
import org.jetbrains.kotlin.asJava.elements.KtLightMethodImpl
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.KtCallableDeclaration
import org.jetbrains.kotlin.psi.KtFunction
import org.jetbrains.kotlin.psi.KtParameter
import org.jetbrains.uast.*
@@ -37,7 +39,10 @@ open class KotlinUMethod(
val bodyExpression = (kotlinOrigin as? KtFunction)?.bodyExpression ?: return@lz null
getLanguagePlugin().convertElement(bodyExpression, this) as? UExpression
}
override val isOverride: Boolean
get() = (kotlinOrigin as? KtCallableDeclaration)?.hasModifier(KtTokens.OVERRIDE_KEYWORD) ?: false
companion object {
fun create(psi: KtLightMethod, containingElement: UElement?) = when (psi) {
is KtLightMethodImpl.KtLightAnnotationMethod -> KotlinUAnnotationMethod(psi, containingElement)
@@ -24,6 +24,7 @@ import org.jetbrains.uast.expressions.UReferenceExpression
import org.jetbrains.uast.expressions.UTypeReferenceExpression
import org.jetbrains.uast.java.AbstractJavaUVariable
import org.jetbrains.uast.java.JavaAbstractUExpression
import org.jetbrains.uast.java.JavaUAnnotation
import org.jetbrains.uast.java.annotations
import org.jetbrains.uast.kotlin.psi.UastKotlinPsiParameter
import org.jetbrains.uast.kotlin.psi.UastKotlinPsiVariable
@@ -49,7 +50,7 @@ class KotlinUVariable(
) : AbstractKotlinUVariable(), UVariable, PsiVariable by psi {
override val psi = unwrap<UVariable, PsiVariable>(psi)
override val uastAnnotations by lz { psi.annotations.map { SimpleUAnnotation(it, this) } }
override val annotations by lz { psi.annotations.map { JavaUAnnotation(it, this) } }
override val typeReference by lz { getLanguagePlugin().convertOpt<UTypeReferenceExpression>(psi.typeElement, this) }
companion object {
@@ -18,6 +18,7 @@ package org.jetbrains.uast.kotlin
import org.jetbrains.kotlin.psi.KtLabeledExpression
import org.jetbrains.uast.UElement
import org.jetbrains.uast.UIdentifier
import org.jetbrains.uast.ULabeledExpression
import org.jetbrains.uast.psi.PsiElementBacked
@@ -28,5 +29,8 @@ class KotlinULabeledExpression(
override val label: String
get() = psi.getLabelName().orAnonymous("label")
override val labelIdentifier: UIdentifier?
get() = psi.getTargetLabel()?.let { UIdentifier(it, this) }
override val expression by lz { KotlinConverter.convertOrEmpty(psi.baseExpression, this) }
}
@@ -63,6 +63,9 @@ class KotlinUObjectLiteralExpression(
) : KotlinAbstractUElement(), USimpleNameReferenceExpression, PsiElementBacked {
override fun resolve() = (psi.resolveCallToDeclaration(this) as? PsiMethod)?.containingClass
override val annotations: List<UAnnotation>
get() = emptyList()
override val resolvedName: String?
get() = identifier
@@ -109,6 +109,9 @@ open class KotlinUSimpleReferenceExpression(
null
}
override val annotations: List<UAnnotation>
get() = emptyList()
override val receiverType by lz {
val type = (resolvedCall.dispatchReceiver ?: resolvedCall.extensionReceiver)?.type ?: return@lz null
type.toPsiType(this, psi, boxed = true)
@@ -17,11 +17,21 @@
package org.jetbrains.uast.kotlin
import org.jetbrains.kotlin.psi.KtSuperExpression
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.uast.UElement
import org.jetbrains.uast.UIdentifier
import org.jetbrains.uast.USuperExpression
import org.jetbrains.uast.psi.PsiElementBacked
class KotlinUSuperExpression(
override val psi: KtSuperExpression,
override val containingElement: UElement?
) : KotlinAbstractUExpression(), USuperExpression, PsiElementBacked, KotlinUElementWithType, KotlinEvaluatableUElement
) : KotlinAbstractUExpression(), USuperExpression, PsiElementBacked, KotlinUElementWithType, KotlinEvaluatableUElement {
override val label: String?
get() = psi.getLabelName()
override val labelIdentifier: UIdentifier?
get() = psi.getTargetLabel()?.let { UIdentifier(it, this) }
override fun resolve() = psi.analyze()[BindingContext.LABEL_TARGET, psi.getTargetLabel()]
}
@@ -95,6 +95,8 @@ class KotlinUSwitchEntry(
get() = null
override val containingElement: UElement?
get() = this@KotlinUSwitchEntry
override val annotations: List<UAnnotation>
get() = emptyList()
}
}
}
@@ -17,11 +17,21 @@
package org.jetbrains.uast.kotlin
import org.jetbrains.kotlin.psi.KtThisExpression
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.uast.UElement
import org.jetbrains.uast.UIdentifier
import org.jetbrains.uast.UThisExpression
import org.jetbrains.uast.psi.PsiElementBacked
class KotlinUThisExpression(
override val psi: KtThisExpression,
override val containingElement: UElement?
) : KotlinAbstractUExpression(), UThisExpression, PsiElementBacked, KotlinUElementWithType, KotlinEvaluatableUElement
) : KotlinAbstractUExpression(), UThisExpression, PsiElementBacked, KotlinUElementWithType, KotlinEvaluatableUElement {
override val label: String?
get() = psi.getLabelName()
override val labelIdentifier: UIdentifier?
get() = psi.getTargetLabel()?.let { UIdentifier(it, this) }
override fun resolve() = psi.analyze()[BindingContext.LABEL_TARGET, psi.getTargetLabel()]
}