Add utility functions for conversion to light elements
This commit is contained in:
@@ -50,6 +50,7 @@ public class JetParameterList extends JetElementImplStub<PsiJetParameterListStub
|
|||||||
return visitor.visitParameterList(this, data);
|
return visitor.visitParameterList(this, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
public List<JetParameter> getParameters() {
|
public List<JetParameter> getParameters() {
|
||||||
return Arrays.asList(getStubOrPsiChildren(JetStubElementTypes.VALUE_PARAMETER, JetParameter.ARRAY_FACTORY));
|
return Arrays.asList(getStubOrPsiChildren(JetStubElementTypes.VALUE_PARAMETER, JetParameter.ARRAY_FACTORY));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,6 +34,7 @@ public class JetTypeParameterList extends JetElementImplStub<PsiJetTypeParameter
|
|||||||
super(stub, nodeType);
|
super(stub, nodeType);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
public List<JetTypeParameter> getParameters() {
|
public List<JetTypeParameter> getParameters() {
|
||||||
return Arrays.asList(getStubOrPsiChildren(JetStubElementTypes.TYPE_PARAMETER, JetTypeParameter.ARRAY_FACTORY));
|
return Arrays.asList(getStubOrPsiChildren(JetStubElementTypes.TYPE_PARAMETER, JetTypeParameter.ARRAY_FACTORY));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -42,6 +42,10 @@ import org.jetbrains.jet.lang.psi.JetParameterList
|
|||||||
import org.jetbrains.jet.lang.psi.JetNamedDeclaration
|
import org.jetbrains.jet.lang.psi.JetNamedDeclaration
|
||||||
import com.intellij.psi.PsiNamedElement
|
import com.intellij.psi.PsiNamedElement
|
||||||
import org.jetbrains.jet.lang.psi.JetObjectDeclaration
|
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<T: PsiElement>(
|
fun PsiElement.getParentByTypesAndPredicate<T: PsiElement>(
|
||||||
strict : Boolean = false, vararg parentClasses : Class<T>, predicate: (T) -> Boolean
|
strict : Boolean = false, vararg parentClasses : Class<T>, predicate: (T) -> Boolean
|
||||||
@@ -178,4 +182,14 @@ fun JetDeclaration.isOverridable(): Boolean {
|
|||||||
val PsiElement.namedNavigationElement: PsiNamedElement?
|
val PsiElement.namedNavigationElement: PsiNamedElement?
|
||||||
get() = getNavigationElement()?.getParentByType(javaClass<PsiNamedElement>())
|
get() = getNavigationElement()?.getParentByType(javaClass<PsiNamedElement>())
|
||||||
|
|
||||||
|
fun PsiElement.isExtensionDeclaration(): Boolean {
|
||||||
|
val callable: JetCallableDeclaration? = when (this) {
|
||||||
|
is JetNamedFunction, is JetProperty -> this as JetCallableDeclaration
|
||||||
|
is JetPropertyAccessor -> getParentByType(javaClass<JetProperty>())
|
||||||
|
else -> null
|
||||||
|
}
|
||||||
|
|
||||||
|
return callable?.getReceiverTypeRef() != null
|
||||||
|
}
|
||||||
|
|
||||||
fun PsiElement.isObjectLiteral(): Boolean = this is JetObjectDeclaration && isObjectLiteral()
|
fun PsiElement.isObjectLiteral(): Boolean = this is JetObjectDeclaration && isObjectLiteral()
|
||||||
@@ -23,6 +23,36 @@ import org.jetbrains.jet.lang.psi.JetProperty
|
|||||||
import org.jetbrains.jet.lang.psi.JetParameter
|
import org.jetbrains.jet.lang.psi.JetParameter
|
||||||
import java.util.Collections
|
import java.util.Collections
|
||||||
import org.jetbrains.jet.lang.psi.JetPropertyAccessor
|
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<PsiElement> =
|
||||||
|
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<PsiElement>().let { elements ->
|
||||||
|
toPsiParameter()?.let { psiParameter -> elements.add(psiParameter) }
|
||||||
|
LightClassUtil.getLightClassPropertyMethods(this).toCollection(elements)
|
||||||
|
|
||||||
|
elements
|
||||||
|
}
|
||||||
|
is JetTypeParameter -> toPsiTypeParameters()
|
||||||
|
else -> Collections.emptyList()
|
||||||
|
}
|
||||||
|
|
||||||
fun PsiElement.toLightMethods(): List<PsiMethod> =
|
fun PsiElement.toLightMethods(): List<PsiMethod> =
|
||||||
when (this) {
|
when (this) {
|
||||||
@@ -42,4 +72,36 @@ fun PsiElement.getRepresentativeLightMethod(): PsiMethod? =
|
|||||||
is JetPropertyAccessor -> LightClassUtil.getLightClassAccessorMethod(this)
|
is JetPropertyAccessor -> LightClassUtil.getLightClassAccessorMethod(this)
|
||||||
is PsiMethod -> this
|
is PsiMethod -> this
|
||||||
else -> null
|
else -> null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun JetParameter.toPsiParameter(): PsiParameter? {
|
||||||
|
val paramList = getParentByType(javaClass<JetParameterList>())
|
||||||
|
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<PsiTypeParameter> {
|
||||||
|
val paramList = getParentByType(javaClass<JetTypeParameterList>())
|
||||||
|
if (paramList == null) return Collections.emptyList()
|
||||||
|
|
||||||
|
val paramIndex = paramList.getParameters().indexOf(this)
|
||||||
|
val lightOwners = paramList.getParentByType(javaClass<JetDeclaration>())?.toLightElements()
|
||||||
|
|
||||||
|
return lightOwners?.map { lightOwner -> (lightOwner as PsiTypeParameterListOwner).getTypeParameters()[paramIndex] }
|
||||||
|
?: Collections.emptyList()
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user