From 75f67d5336d67109202f0bd9bcaff06a134c787b Mon Sep 17 00:00:00 2001 From: Alexey Sedunov Date: Thu, 16 Jan 2014 17:10:50 +0400 Subject: [PATCH] Add utility functions for conversion to light elements --- .../jet/lang/psi/JetParameterList.java | 1 + .../jet/lang/psi/JetTypeParameterList.java | 1 + .../jet/lang/psi/psiUtil/jetPsiUtil.kt | 14 ++++ .../jetbrains/jet/asJava/lightClassUtils.kt | 64 ++++++++++++++++++- 4 files changed, 79 insertions(+), 1 deletion(-) diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetParameterList.java b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetParameterList.java index fd78e36ff13..aca875915a1 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetParameterList.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetParameterList.java @@ -50,6 +50,7 @@ public class JetParameterList extends JetElementImplStub getParameters() { return Arrays.asList(getStubOrPsiChildren(JetStubElementTypes.VALUE_PARAMETER, JetParameter.ARRAY_FACTORY)); } diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetTypeParameterList.java b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetTypeParameterList.java index 9f9021b7efc..a45302790c9 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetTypeParameterList.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetTypeParameterList.java @@ -34,6 +34,7 @@ public class JetTypeParameterList extends JetElementImplStub getParameters() { return Arrays.asList(getStubOrPsiChildren(JetStubElementTypes.TYPE_PARAMETER, JetTypeParameter.ARRAY_FACTORY)); } diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/psi/psiUtil/jetPsiUtil.kt b/compiler/frontend/src/org/jetbrains/jet/lang/psi/psiUtil/jetPsiUtil.kt index 3475e964fea..ba092c3912f 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/psi/psiUtil/jetPsiUtil.kt +++ b/compiler/frontend/src/org/jetbrains/jet/lang/psi/psiUtil/jetPsiUtil.kt @@ -42,6 +42,10 @@ import org.jetbrains.jet.lang.psi.JetParameterList import org.jetbrains.jet.lang.psi.JetNamedDeclaration import com.intellij.psi.PsiNamedElement import org.jetbrains.jet.lang.psi.JetObjectDeclaration +import org.jetbrains.jet.lang.psi.JetNamedFunction +import org.jetbrains.jet.lang.psi.JetProperty +import org.jetbrains.jet.lang.psi.JetCallableDeclaration +import org.jetbrains.jet.lang.psi.JetPropertyAccessor fun PsiElement.getParentByTypesAndPredicate( strict : Boolean = false, vararg parentClasses : Class, predicate: (T) -> Boolean @@ -178,4 +182,14 @@ fun JetDeclaration.isOverridable(): Boolean { val PsiElement.namedNavigationElement: PsiNamedElement? get() = getNavigationElement()?.getParentByType(javaClass()) +fun PsiElement.isExtensionDeclaration(): Boolean { + val callable: JetCallableDeclaration? = when (this) { + is JetNamedFunction, is JetProperty -> this as JetCallableDeclaration + is JetPropertyAccessor -> getParentByType(javaClass()) + else -> null + } + + return callable?.getReceiverTypeRef() != null +} + fun PsiElement.isObjectLiteral(): Boolean = this is JetObjectDeclaration && isObjectLiteral() \ No newline at end of file diff --git a/compiler/jet.as.java.psi/src/org/jetbrains/jet/asJava/lightClassUtils.kt b/compiler/jet.as.java.psi/src/org/jetbrains/jet/asJava/lightClassUtils.kt index 1289b164c4b..3715cd8ca95 100644 --- a/compiler/jet.as.java.psi/src/org/jetbrains/jet/asJava/lightClassUtils.kt +++ b/compiler/jet.as.java.psi/src/org/jetbrains/jet/asJava/lightClassUtils.kt @@ -23,6 +23,36 @@ import org.jetbrains.jet.lang.psi.JetProperty import org.jetbrains.jet.lang.psi.JetParameter import java.util.Collections import org.jetbrains.jet.lang.psi.JetPropertyAccessor +import com.intellij.psi.PsiParameter +import org.jetbrains.jet.lang.psi.psiUtil.getParentByType +import org.jetbrains.jet.lang.psi.JetParameterList +import org.jetbrains.jet.lang.psi.JetClass +import org.jetbrains.jet.lang.psi.JetTypeParameter +import org.jetbrains.jet.lang.psi.JetDeclaration +import org.jetbrains.jet.lang.psi.JetClassOrObject +import com.intellij.psi.PsiTypeParameter +import java.util.ArrayList +import org.jetbrains.jet.lang.psi.JetTypeParameterList +import com.intellij.psi.PsiTypeParameterListOwner +import com.intellij.psi.PsiNamedElement +import org.jetbrains.jet.lang.psi.JetCallableDeclaration +import org.jetbrains.jet.lang.psi.psiUtil.isExtensionDeclaration + +fun JetDeclaration.toLightElements(): List = + when (this) { + is JetClassOrObject -> Collections.singletonList(LightClassUtil.getPsiClass(this)) + is JetNamedFunction -> Collections.singletonList(LightClassUtil.getLightClassMethod(this)) + is JetProperty -> LightClassUtil.getLightClassPropertyMethods(this).toList() + is JetPropertyAccessor -> Collections.singletonList(LightClassUtil.getLightClassAccessorMethod(this)) + is JetParameter -> ArrayList().let { elements -> + toPsiParameter()?.let { psiParameter -> elements.add(psiParameter) } + LightClassUtil.getLightClassPropertyMethods(this).toCollection(elements) + + elements + } + is JetTypeParameter -> toPsiTypeParameters() + else -> Collections.emptyList() + } fun PsiElement.toLightMethods(): List = when (this) { @@ -42,4 +72,36 @@ fun PsiElement.getRepresentativeLightMethod(): PsiMethod? = is JetPropertyAccessor -> LightClassUtil.getLightClassAccessorMethod(this) is PsiMethod -> this else -> null - } \ No newline at end of file + } + +fun JetParameter.toPsiParameter(): PsiParameter? { + val paramList = getParentByType(javaClass()) + if (paramList == null) return null + + val paramIndex = paramList.getParameters().indexOf(this) + val owner = paramList.getParent() + val lightParamIndex = if (owner != null && owner.isExtensionDeclaration()) paramIndex + 1 else paramIndex + + val method: PsiMethod? = when (owner) { + is JetNamedFunction -> LightClassUtil.getLightClassMethod(owner) + is JetPropertyAccessor -> LightClassUtil.getLightClassAccessorMethod(owner) + is JetClass -> LightClassUtil.getPsiClass(owner)?.getConstructors()?.let { constructors -> + if (constructors.isNotEmpty()) constructors[0] else null + } + else -> null + } + if (method == null) return null + + return method.getParameterList().getParameters()[lightParamIndex] +} + +fun JetTypeParameter.toPsiTypeParameters(): List { + val paramList = getParentByType(javaClass()) + if (paramList == null) return Collections.emptyList() + + val paramIndex = paramList.getParameters().indexOf(this) + val lightOwners = paramList.getParentByType(javaClass())?.toLightElements() + + return lightOwners?.map { lightOwner -> (lightOwner as PsiTypeParameterListOwner).getTypeParameters()[paramIndex] } + ?: Collections.emptyList() +} \ No newline at end of file