LightElements: avoid computation of LightMemberOrigin.originalElement on common api calls

This commit is contained in:
Pavel V. Talanov
2016-03-29 19:21:01 +03:00
parent f40a04c5a2
commit f9fa365059
3 changed files with 26 additions and 20 deletions
@@ -123,7 +123,7 @@ sealed class KtLightFieldImpl(
fun create(origin: LightMemberOrigin?, delegate: PsiField, containingClass: KtLightClass): KtLightField { fun create(origin: LightMemberOrigin?, delegate: PsiField, containingClass: KtLightClass): KtLightField {
when (delegate) { when (delegate) {
is PsiEnumConstant -> { is PsiEnumConstant -> {
val kotlinEnumEntry = origin?.originalElement as? KtEnumEntry val kotlinEnumEntry = (origin as? LightMemberOriginForDeclaration)?.originalElement as? KtEnumEntry
val initializingClass = if (kotlinEnumEntry != null && kotlinEnumEntry.declarations.isNotEmpty()) { val initializingClass = if (kotlinEnumEntry != null && kotlinEnumEntry.declarations.isNotEmpty()) {
val enumConstantFqName = FqName(containingClass.getFqName().asString() + "." + kotlinEnumEntry.name) val enumConstantFqName = FqName(containingClass.getFqName().asString() + "." + kotlinEnumEntry.name)
KtLightClassForEnumEntry(enumConstantFqName, kotlinEnumEntry, delegate) KtLightClassForEnumEntry(enumConstantFqName, kotlinEnumEntry, delegate)
@@ -28,15 +28,16 @@ import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.resolve.jvm.diagnostics.JvmDeclarationOriginKind import org.jetbrains.kotlin.resolve.jvm.diagnostics.JvmDeclarationOriginKind
interface KtLightMethod : PsiMethod, KtLightDeclaration<KtDeclaration, PsiMethod> { interface KtLightMethod : PsiMethod, KtLightDeclaration<KtDeclaration, PsiMethod> {
val lightMethodOrigin: LightMemberOrigin?
val isDelegated: Boolean val isDelegated: Boolean
} }
sealed class KtLightMethodImpl( sealed class KtLightMethodImpl(
override val clsDelegate: PsiMethod, override val clsDelegate: PsiMethod,
private val lightMethodOrigin: LightMemberOrigin?, override val lightMethodOrigin: LightMemberOrigin?,
containingClass: KtLightClass containingClass: KtLightClass
) : LightMethod(clsDelegate.manager, clsDelegate, containingClass), KtLightMethod { ) : LightMethod(clsDelegate.manager, clsDelegate, containingClass), KtLightMethod {
override val kotlinOrigin: KtDeclaration? = lightMethodOrigin?.originalElement as? KtDeclaration override val kotlinOrigin: KtDeclaration? get() = lightMethodOrigin?.originalElement as? KtDeclaration
private val lightIdentifier by lazy { KtLightIdentifier(this, kotlinOrigin as? KtNamedDeclaration) } private val lightIdentifier by lazy { KtLightIdentifier(this, kotlinOrigin as? KtNamedDeclaration) }
@@ -58,22 +59,23 @@ sealed class KtLightMethodImpl(
private val typeParamsList: CachedValue<PsiTypeParameterList> by lazy { private val typeParamsList: CachedValue<PsiTypeParameterList> by lazy {
val cacheManager = CachedValuesManager.getManager(clsDelegate.project) val cacheManager = CachedValuesManager.getManager(clsDelegate.project)
cacheManager.createCachedValue<PsiTypeParameterList>({ cacheManager.createCachedValue<PsiTypeParameterList>({
val origin = kotlinOrigin val origin = (lightMethodOrigin as? LightMemberOriginForDeclaration)?.originalElement
val list = if (origin is KtClassOrObject) { val list = if (origin != null) {
KotlinLightTypeParameterListBuilder(manager) if (origin is KtClassOrObject) {
} KotlinLightTypeParameterListBuilder(manager)
else if (origin == null) { }
clsDelegate.typeParameterList else {
} LightClassUtil.buildLightTypeParameterList(this@KtLightMethodImpl, origin)
else { }
LightClassUtil.buildLightTypeParameterList(this@KtLightMethodImpl, origin) }
} else {
CachedValueProvider.Result.create(list, PsiModificationTracker.OUT_OF_CODE_BLOCK_MODIFICATION_COUNT) clsDelegate.typeParameterList
}, false) }
CachedValueProvider.Result.create(list, PsiModificationTracker.OUT_OF_CODE_BLOCK_MODIFICATION_COUNT)
}, false)
} }
override fun getNavigationElement(): PsiElement = kotlinOrigin?.navigationElement ?: super.getNavigationElement() override fun getNavigationElement(): PsiElement = kotlinOrigin?.navigationElement ?: super.getNavigationElement()
override fun getOriginalElement(): PsiElement = kotlinOrigin ?: super.getOriginalElement()
override fun getParent(): PsiElement? = containingClass override fun getParent(): PsiElement? = containingClass
override fun getText() = kotlinOrigin?.text ?: "" override fun getText() = kotlinOrigin?.text ?: ""
override fun getTextRange() = kotlinOrigin?.textRange ?: TextRange.EMPTY_RANGE override fun getTextRange() = kotlinOrigin?.textRange ?: TextRange.EMPTY_RANGE
@@ -152,11 +154,11 @@ sealed class KtLightMethodImpl(
override fun equals(other: Any?): Boolean = override fun equals(other: Any?): Boolean =
other is KtLightMethod && other is KtLightMethod &&
name == other.name && name == other.name &&
kotlinOrigin == other.kotlinOrigin && lightMethodOrigin == other.lightMethodOrigin &&
containingClass == other.containingClass && containingClass == other.containingClass &&
clsDelegate == other.clsDelegate clsDelegate == other.clsDelegate
override fun hashCode(): Int = ((name.hashCode() * 31 + (kotlinOrigin?.hashCode() ?: 0)) * 31 + containingClass.hashCode()) * 31 + clsDelegate.hashCode() override fun hashCode(): Int = ((name.hashCode() * 31 + (lightMethodOrigin?.hashCode() ?: 0)) * 31 + containingClass.hashCode()) * 31 + clsDelegate.hashCode()
override fun toString(): String = "${this.javaClass.simpleName}:$name" override fun toString(): String = "${this.javaClass.simpleName}:$name"
@@ -105,6 +105,11 @@ public class KtLightParameter extends LightParameter implements KtLightDeclarati
return origin != null ? origin : super.getNavigationElement(); return origin != null ? origin : super.getNavigationElement();
} }
@Override
public boolean isValid() {
return method.isValid();
}
@Override @Override
public PsiElement setName(@NonNls @NotNull String name) throws IncorrectOperationException { public PsiElement setName(@NonNls @NotNull String name) throws IncorrectOperationException {
KtParameter origin = getKotlinOrigin(); KtParameter origin = getKotlinOrigin();
@@ -116,8 +121,7 @@ public class KtLightParameter extends LightParameter implements KtLightDeclarati
@Override @Override
public PsiFile getContainingFile() { public PsiFile getContainingFile() {
KtDeclaration declaration = method.getKotlinOrigin(); return method.getContainingFile();
return declaration != null ? declaration.getContainingFile() : super.getContainingFile();
} }
@NotNull @NotNull