Extract base interface for Kotlin light elements

This commit is contained in:
Alexey Sedunov
2014-01-22 20:07:41 +04:00
parent f694b658ef
commit 9eac148555
13 changed files with 80 additions and 38 deletions
@@ -22,6 +22,8 @@ import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiManager;
import com.intellij.psi.impl.light.AbstractLightClass;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.psi.JetClassOrObject;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.lang.resolve.java.jetAsJava.JetJavaMirrorMarker;
import org.jetbrains.jet.lang.resolve.name.FqName;
@@ -46,6 +48,12 @@ import org.jetbrains.jet.lang.resolve.name.FqName;
this.file = file;
}
@Nullable
@Override
public JetClassOrObject getOrigin() {
return null;
}
@Override
public PsiFile getContainingFile() {
return file;
@@ -19,11 +19,9 @@ package org.jetbrains.jet.asJava;
import com.intellij.psi.PsiClass;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.resolve.name.FqName;
import org.jetbrains.jet.lang.resolve.java.jetAsJava.KotlinLightElement
import org.jetbrains.jet.lang.psi.JetClassOrObject
public interface KotlinLightClass extends PsiClass {
@NotNull
FqName getFqName();
@NotNull
PsiClass getDelegate();
public trait KotlinLightClass: PsiClass, KotlinLightElement<JetClassOrObject, PsiClass> {
val fqName: FqName
}
@@ -198,8 +198,9 @@ public class KotlinLightClassForExplicitDeclaration extends KotlinWrappingLightC
this.classOrObject = classOrObject;
}
@Override
@NotNull
public JetClassOrObject getJetClassOrObject() {
public JetClassOrObject getOrigin() {
return classOrObject;
}
@@ -30,6 +30,7 @@ import com.intellij.psi.util.CachedValuesManager;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.psi.JetClassOrObject;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.lang.resolve.java.PackageClassUtils;
import org.jetbrains.jet.lang.resolve.java.jetAsJava.JetJavaMirrorMarker;
@@ -91,6 +92,12 @@ public class KotlinLightClassForPackage extends KotlinWrappingLightClass impleme
return true;
}
@Nullable
@Override
public JetClassOrObject getOrigin() {
return null;
}
@Nullable
@Override
public PsiModifierList getModifierList() {
@@ -39,15 +39,16 @@ import org.jetbrains.jet.lang.psi.psiUtil.getParentByType
import org.jetbrains.jet.lang.psi.JetProperty
import com.intellij.psi.PsiTypeParameter
public class KotlinLightMethodForDeclaration(manager: PsiManager, val method: PsiMethod, val jetDeclaration: JetDeclaration, containingClass: PsiClass):
LightMethod(manager, method, containingClass), KotlinLightMethod {
public class KotlinLightMethodForDeclaration(
manager: PsiManager, override val delegate: PsiMethod, override val origin: JetDeclaration, containingClass: PsiClass
): LightMethod(manager, delegate, containingClass), KotlinLightMethod {
private val paramsList: CachedValue<PsiParameterList> by Delegates.blockingLazy {
val cacheManager = CachedValuesManager.getManager(method.getProject())
val cacheManager = CachedValuesManager.getManager(delegate.getProject())
cacheManager.createCachedValue<PsiParameterList>({
val parameterBuilder = LightParameterListBuilder(getManager(), JetLanguage.INSTANCE)
for ((index, parameter) in method.getParameterList().getParameters().withIndices()) {
for ((index, parameter) in delegate.getParameterList().getParameters().withIndices()) {
val lightParameter = LightParameter(parameter.getName() ?: "p$index", parameter.getType(), this, JetLanguage.INSTANCE)
parameterBuilder.addParameter(lightParameter)
}
@@ -57,36 +58,33 @@ public class KotlinLightMethodForDeclaration(manager: PsiManager, val method: Ps
}
private val typeParamsList: CachedValue<PsiTypeParameterList> by Delegates.blockingLazy {
val cacheManager = CachedValuesManager.getManager(method.getProject())
val cacheManager = CachedValuesManager.getManager(delegate.getProject())
cacheManager.createCachedValue<PsiTypeParameterList>({
val declaration =
if (jetDeclaration is JetPropertyAccessor) jetDeclaration.getParentByType(javaClass<JetProperty>()) else jetDeclaration
val list = LightClassUtil.buildLightTypeParameterList(this@KotlinLightMethodForDeclaration, jetDeclaration)
if (origin is JetPropertyAccessor) origin.getParentByType(javaClass<JetProperty>()) else origin
val list = LightClassUtil.buildLightTypeParameterList(this@KotlinLightMethodForDeclaration, origin)
CachedValueProvider.Result.create(list, PsiModificationTracker.OUT_OF_CODE_BLOCK_MODIFICATION_COUNT)
}, false)
}
override fun getDelegate(): PsiMethod = method
override fun getNavigationElement() : PsiElement = jetDeclaration
override fun getOriginalElement() : PsiElement = jetDeclaration
override fun getOrigin(): JetDeclaration? = jetDeclaration
override fun getNavigationElement() : PsiElement = origin
override fun getOriginalElement() : PsiElement = origin
override fun getParent(): PsiElement? = getContainingClass()
override fun setName(name: String): PsiElement? {
(jetDeclaration as PsiNamedElement).setName(name)
(origin as PsiNamedElement).setName(name)
return this
}
public override fun delete() {
if (jetDeclaration.isValid()) {
jetDeclaration.delete()
if (origin.isValid()) {
origin.delete()
}
}
override fun isEquivalentTo(another: PsiElement?): Boolean {
if (another is KotlinLightMethod && getOrigin() == another.getOrigin()) {
if (another is KotlinLightMethod && origin == another.origin) {
return true
}
@@ -100,6 +98,6 @@ public class KotlinLightMethodForDeclaration(manager: PsiManager, val method: Ps
getTypeParameterList()?.let { it.getTypeParameters() } ?: PsiTypeParameter.EMPTY_ARRAY
override fun copy(): PsiElement {
return KotlinLightMethodForDeclaration(getManager()!!, method, jetDeclaration.copy() as JetDeclaration, getContainingClass()!!)
return KotlinLightMethodForDeclaration(getManager()!!, delegate, origin.copy() as JetDeclaration, getContainingClass()!!)
}
}
@@ -20,12 +20,15 @@ import com.intellij.psi.*;
import com.intellij.psi.impl.light.AbstractLightClass;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.psi.JetTypeParameter;
import org.jetbrains.jet.lang.psi.JetClass;
import org.jetbrains.jet.lang.psi.JetTypeParameterListOwner;
import org.jetbrains.jet.lang.resolve.java.jetAsJava.KotlinLightElement;
import org.jetbrains.jet.lang.resolve.java.jetAsJava.KotlinLightMethod;
import org.jetbrains.jet.plugin.JetLanguage;
public class KotlinLightTypeParameter extends AbstractLightClass implements PsiTypeParameter {
public class KotlinLightTypeParameter
extends AbstractLightClass implements PsiTypeParameter, KotlinLightElement<JetTypeParameter, PsiTypeParameter> {
private final PsiTypeParameterListOwner owner;
private final int index;
private final String name;
@@ -46,6 +49,12 @@ public class KotlinLightTypeParameter extends AbstractLightClass implements PsiT
return getOwnerDelegate().getTypeParameters()[index];
}
@NotNull
@Override
public JetTypeParameter getOrigin() {
return ((JetTypeParameterListOwner) owner.getNavigationElement()).getTypeParameters().get(index);
}
@NotNull
private PsiTypeParameterListOwner getOwnerDelegate() {
if (owner instanceof KotlinLightClass) return ((KotlinLightClass) owner).getDelegate();
@@ -116,6 +125,6 @@ public class KotlinLightTypeParameter extends AbstractLightClass implements PsiT
@NotNull
@Override
public PsiElement getNavigationElement() {
return ((JetTypeParameterListOwner) owner.getNavigationElement()).getTypeParameters().get(index);
return getOrigin();
}
}