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
@@ -0,0 +1,25 @@
/*
* Copyright 2010-2014 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.lang.resolve.java.jetAsJava
import com.intellij.psi.PsiElement
import org.jetbrains.jet.lang.psi.JetDeclaration
trait KotlinLightElement<T: JetDeclaration, D: PsiElement> {
val origin: T?
val delegate: D
}
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2013 JetBrains s.r.o.
* Copyright 2010-2014 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,7 +20,4 @@ import com.intellij.psi.PsiMethod;
import org.jetbrains.jet.lang.psi.JetDeclaration;
/** Java method created for Kotlin declaration to make it resolvable in Java */
public interface KotlinLightMethod extends PsiMethod {
JetDeclaration getOrigin();
PsiMethod getDelegate();
}
public trait KotlinLightMethod: PsiMethod, KotlinLightElement<JetDeclaration, PsiMethod>
@@ -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();
}
}
@@ -84,7 +84,7 @@ public class KotlinFindClassUsagesDialog extends FindClassUsagesDialog {
public void configureLabelComponent(@NotNull SimpleColoredComponent coloredComponent) {
PsiClass klass = (PsiClass) getPsiElement();
if (klass instanceof KotlinLightClassForExplicitDeclaration) {
coloredComponent.append(JetRefactoringUtil.formatClass(((KotlinLightClassForExplicitDeclaration) klass).getJetClassOrObject()));
coloredComponent.append(JetRefactoringUtil.formatClass(((KotlinLightClassForExplicitDeclaration) klass).getOrigin()));
}
}
@@ -27,7 +27,6 @@ import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.asJava.KotlinLightClass;
import org.jetbrains.jet.asJava.KotlinLightClassForExplicitDeclaration;
import org.jetbrains.jet.asJava.KotlinLightClassForPackage;
import org.jetbrains.jet.lang.psi.JetClass;
import org.jetbrains.jet.lang.psi.JetClassOrObject;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.plugin.JetBundle;
@@ -67,7 +66,7 @@ public class RenameJetClassProcessor extends RenamePsiElementProcessor {
private static JetClassOrObject getJetClassOrObject(@Nullable PsiElement element, boolean showErrors, @Nullable Editor editor) {
if (element instanceof KotlinLightClass) {
if (element instanceof KotlinLightClassForExplicitDeclaration) {
return ((KotlinLightClassForExplicitDeclaration) element).getJetClassOrObject();
return ((KotlinLightClassForExplicitDeclaration) element).getOrigin();
}
else if (element instanceof KotlinLightClassForPackage) {
if (showErrors) {
@@ -45,7 +45,7 @@ public class RenameKotlinFunctionProcessor : RenamePsiElementProcessor() {
}
override fun canProcessElement(element: PsiElement): Boolean {
return element is JetNamedFunction || (element is KotlinLightMethod && element.getOrigin() is JetNamedFunction)
return element is JetNamedFunction || (element is KotlinLightMethod && element.origin is JetNamedFunction)
}
override fun substituteElementToRename(element: PsiElement?, editor: Editor?): PsiElement? {
@@ -59,7 +59,7 @@ public class RenameKotlinFunctionProcessor : RenamePsiElementProcessor() {
val subtitudedJavaElement = javaMethodProcessorInstance.substituteElementToRename(wrappedMethod, editor)
return when (subtitudedJavaElement) {
is KotlinLightMethod -> subtitudedJavaElement.getOrigin() as? JetNamedFunction
is KotlinLightMethod -> subtitudedJavaElement.origin as? JetNamedFunction
else -> subtitudedJavaElement
}
}
@@ -163,7 +163,7 @@ fun PsiReference.isPropertyReadOnlyUsage(): Boolean {
val refTarget = resolve()
if (refTarget is KotlinLightMethod) {
val origin = refTarget.getOrigin()
val origin = refTarget.origin
val declaration: JetNamedDeclaration? = when (origin) {
is JetPropertyAccessor -> origin.getParentByType(javaClass<JetProperty>())
is JetProperty, is JetParameter -> origin as JetNamedDeclaration
@@ -40,7 +40,7 @@ public class JetPsiHeuristicsUtil {
KotlinLightClassForExplicitDeclaration lightClass = (KotlinLightClassForExplicitDeclaration) member;
// It is a Kotlin class already, we need to properly check visibility?
JetClassOrObject classOrObject = lightClass.getJetClassOrObject();
JetClassOrObject classOrObject = lightClass.getOrigin();
if (isTopLevelDeclaration(classOrObject) && classOrObject.hasModifier(JetTokens.PRIVATE_KEYWORD)) {
// The class is declared private in the targetPackage