Don't duplicate UAST parent calculation logic

This commit is contained in:
Dmitry Jemerov
2017-09-13 13:38:27 +02:00
parent b638febc41
commit a5a5b37e1e
6 changed files with 12 additions and 31 deletions
@@ -13,10 +13,8 @@ import org.jetbrains.uast.*
class KotlinUAnnotation( class KotlinUAnnotation(
override val psi: KtAnnotationEntry, override val psi: KtAnnotationEntry,
private val givenParent: UElement? givenParent: UElement?
) : UAnnotation { ) : KotlinAbstractUElement(givenParent), UAnnotation {
override val uastParent: UElement? by lz { convertParent(givenParent) }
private val resolvedAnnotation: AnnotationDescriptor? by lz { psi.analyze()[BindingContext.ANNOTATION, psi] } private val resolvedAnnotation: AnnotationDescriptor? by lz { psi.analyze()[BindingContext.ANNOTATION, psi] }
private val resolvedCall: ResolvedCall<*>? by lz { psi.getResolvedCall(psi.analyze()) } private val resolvedCall: ResolvedCall<*>? by lz { psi.getResolvedCall(psi.analyze()) }
@@ -28,10 +28,8 @@ import org.jetbrains.uast.USimpleNameReferenceExpression
class KotlinUImportStatement( class KotlinUImportStatement(
override val psi: KtImportDirective, override val psi: KtImportDirective,
private val givenParent: UElement? givenParent: UElement?
) : UImportStatement { ) : KotlinAbstractUElement(givenParent), UImportStatement {
override val uastParent: UElement? by lz { convertParent(givenParent) }
override val isOnDemand: Boolean override val isOnDemand: Boolean
get() = psi.isAllUnder get() = psi.isAllUnder
@@ -30,12 +30,10 @@ import org.jetbrains.uast.kotlin.*
open class KotlinUMethod( open class KotlinUMethod(
psi: KtLightMethod, psi: KtLightMethod,
private val givenParent: UElement? givenParent: UElement?
) : UAnnotationMethod, JavaUElementWithComments, PsiMethod by psi { ) : KotlinAbstractUElement(givenParent), UAnnotationMethod, JavaUElementWithComments, PsiMethod by psi {
override val psi: KtLightMethod = unwrap<UMethod, KtLightMethod>(psi) override val psi: KtLightMethod = unwrap<UMethod, KtLightMethod>(psi)
override val uastParent: UElement? by lz { convertParent(givenParent) }
override val uastDefaultValue by lz { override val uastDefaultValue by lz {
val annotationParameter = psi.kotlinOrigin as? KtParameter ?: return@lz null val annotationParameter = psi.kotlinOrigin as? KtParameter ?: return@lz null
val defaultValue = annotationParameter.defaultValue ?: return@lz null val defaultValue = annotationParameter.defaultValue ?: return@lz null
@@ -85,8 +83,6 @@ open class KotlinUMethod(
override fun equals(other: Any?) = other is KotlinUMethod && psi == other.psi override fun equals(other: Any?) = other is KotlinUMethod && psi == other.psi
override fun hashCode() = psi.hashCode()
companion object { companion object {
fun create(psi: KtLightMethod, containingElement: UElement?) = KotlinUMethod(psi, containingElement) fun create(psi: KtLightMethod, containingElement: UElement?) = KotlinUMethod(psi, containingElement)
} }
@@ -33,10 +33,8 @@ import org.jetbrains.uast.kotlin.psi.UastKotlinPsiParameter
import org.jetbrains.uast.kotlin.psi.UastKotlinPsiVariable import org.jetbrains.uast.kotlin.psi.UastKotlinPsiVariable
import org.jetbrains.uast.visitor.UastVisitor import org.jetbrains.uast.visitor.UastVisitor
abstract class AbstractKotlinUVariable(private val givenParent: UElement?) abstract class AbstractKotlinUVariable(givenParent: UElement?)
: PsiVariable, UVariable, KotlinUElementWithComments { : KotlinAbstractUElement(givenParent), PsiVariable, UVariable, KotlinUElementWithComments {
override val uastParent: UElement? by lz { convertParent(givenParent) }
override val uastInitializer: UExpression? override val uastInitializer: UExpression?
get() { get() {
@@ -83,8 +81,6 @@ abstract class AbstractKotlinUVariable(private val givenParent: UElement?)
get() = UIdentifier(nameIdentifier, this) get() = UIdentifier(nameIdentifier, this)
override fun equals(other: Any?) = other is AbstractKotlinUVariable && psi == other.psi override fun equals(other: Any?) = other is AbstractKotlinUVariable && psi == other.psi
override fun hashCode() = psi.hashCode()
} }
class KotlinUVariable( class KotlinUVariable(
@@ -23,17 +23,15 @@ import org.jetbrains.uast.*
class KotlinUNamedExpression private constructor( class KotlinUNamedExpression private constructor(
override val name: String?, override val name: String?,
private val givenParent: UElement?, givenParent: UElement?,
expressionProducer: (UElement) -> UExpression expressionProducer: (UElement) -> UExpression
) : UNamedExpression { ) : KotlinAbstractUElement(givenParent), UNamedExpression {
override val expression: UExpression by lz { expressionProducer(this) } override val expression: UExpression by lz { expressionProducer(this) }
override val annotations: List<UAnnotation> = emptyList() override val annotations: List<UAnnotation> = emptyList()
override val psi: PsiElement? = null override val psi: PsiElement? = null
override val uastParent: UElement? by lz { convertParent(givenParent) }
companion object { companion object {
internal fun create(name: String?, valueArgument: ValueArgument, uastParent: UElement?): UNamedExpression { internal fun create(name: String?, valueArgument: ValueArgument, uastParent: UElement?): UNamedExpression {
val expression = valueArgument.getArgumentExpression() val expression = valueArgument.getArgumentExpression()
@@ -12,19 +12,14 @@ import org.jetbrains.uast.kotlin.psi.UastKotlinPsiVariable
private class KotlinLocalFunctionUVariable( private class KotlinLocalFunctionUVariable(
val function: KtFunction, val function: KtFunction,
override val psi: PsiVariable, override val psi: PsiVariable,
private val givenParent: UElement? givenParent: UElement?
) : UVariable, PsiVariable by psi { ) : KotlinAbstractUElement(givenParent), UVariable, PsiVariable by psi {
override val uastParent: UElement? by lz { convertParent(givenParent) }
override val uastInitializer: UExpression? by lz { override val uastInitializer: UExpression? by lz {
createLocalFunctionLambdaExpression(function, this) createLocalFunctionLambdaExpression(function, this)
} }
override val typeReference: UTypeReferenceExpression? = null override val typeReference: UTypeReferenceExpression? = null
override val uastAnchor: UElement? = null override val uastAnchor: UElement? = null
override val annotations: List<UAnnotation> = emptyList() override val annotations: List<UAnnotation> = emptyList()
override fun equals(other: Any?) = other is KotlinLocalFunctionUVariable && psi == other.psi
override fun hashCode() = psi.hashCode()
} }