[light classes] move getParentForLocalDeclaration to light-classes-base
^KT-53097
This commit is contained in:
+78
@@ -5,9 +5,87 @@
|
|||||||
|
|
||||||
package org.jetbrains.kotlin.asJava.classes
|
package org.jetbrains.kotlin.asJava.classes
|
||||||
|
|
||||||
|
import com.intellij.psi.PsiClass
|
||||||
import com.intellij.psi.PsiElement
|
import com.intellij.psi.PsiElement
|
||||||
|
import com.intellij.psi.PsiMethod
|
||||||
import com.intellij.psi.augment.PsiAugmentProvider
|
import com.intellij.psi.augment.PsiAugmentProvider
|
||||||
|
import com.intellij.psi.impl.light.LightClass
|
||||||
|
import com.intellij.psi.impl.light.LightMethod
|
||||||
|
import org.jetbrains.kotlin.asJava.LightClassUtil
|
||||||
|
import org.jetbrains.kotlin.asJava.toLightClass
|
||||||
|
import org.jetbrains.kotlin.idea.KotlinLanguage
|
||||||
|
import org.jetbrains.kotlin.psi.*
|
||||||
|
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
|
||||||
|
|
||||||
internal fun <Psi : PsiElement> collectAugments(element: PsiElement, type: Class<out Psi>): List<Psi> {
|
internal fun <Psi : PsiElement> collectAugments(element: PsiElement, type: Class<out Psi>): List<Psi> {
|
||||||
return PsiAugmentProvider.collectAugments(element, type, null)
|
return PsiAugmentProvider.collectAugments(element, type, null)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun getParentForLocalDeclaration(classOrObject: KtClassOrObject): PsiElement? {
|
||||||
|
fun getParentByPsiMethod(method: PsiMethod?, name: String?, forceMethodWrapping: Boolean): PsiElement? {
|
||||||
|
if (method == null || name == null) return null
|
||||||
|
|
||||||
|
var containingClass: PsiClass? = method.containingClass ?: return null
|
||||||
|
|
||||||
|
val currentFileName = classOrObject.containingFile.name
|
||||||
|
|
||||||
|
var createWrapper = forceMethodWrapping
|
||||||
|
// Use PsiClass wrapper instead of package light class to avoid names like "FooPackage" in Type Hierarchy and related views
|
||||||
|
if (containingClass is KtLightClassForFacade) {
|
||||||
|
containingClass = object : LightClass(containingClass as KtLightClassForFacade, KotlinLanguage.INSTANCE) {
|
||||||
|
override fun getName(): String = currentFileName
|
||||||
|
}
|
||||||
|
createWrapper = true
|
||||||
|
}
|
||||||
|
|
||||||
|
if (createWrapper) {
|
||||||
|
return object : LightMethod(classOrObject.manager, method, containingClass!!, KotlinLanguage.INSTANCE) {
|
||||||
|
override fun getParent(): PsiElement = getContainingClass()
|
||||||
|
override fun getName(): String = name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return method
|
||||||
|
}
|
||||||
|
|
||||||
|
var declaration: PsiElement? = KtPsiUtil.getTopmostParentOfTypes(
|
||||||
|
classOrObject,
|
||||||
|
KtNamedFunction::class.java,
|
||||||
|
KtConstructor::class.java,
|
||||||
|
KtProperty::class.java,
|
||||||
|
KtAnonymousInitializer::class.java,
|
||||||
|
KtParameter::class.java
|
||||||
|
)
|
||||||
|
|
||||||
|
if (declaration is KtParameter) {
|
||||||
|
declaration = declaration.getStrictParentOfType<KtNamedDeclaration>()
|
||||||
|
}
|
||||||
|
|
||||||
|
if (declaration is KtFunction) {
|
||||||
|
return getParentByPsiMethod(
|
||||||
|
LightClassUtil.getLightClassMethod(declaration),
|
||||||
|
declaration.name,
|
||||||
|
forceMethodWrapping = false
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Represent the property as a fake method with the same name
|
||||||
|
if (declaration is KtProperty) {
|
||||||
|
return getParentByPsiMethod(
|
||||||
|
LightClassUtil.getLightClassPropertyMethods(declaration).getter,
|
||||||
|
declaration.name,
|
||||||
|
forceMethodWrapping = true
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (declaration is KtAnonymousInitializer) {
|
||||||
|
val parent = declaration.parent
|
||||||
|
val grandparent = parent.parent
|
||||||
|
|
||||||
|
if (parent is KtClassBody && grandparent is KtClassOrObject) {
|
||||||
|
return grandparent.toLightClass()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return if (declaration is KtClass) declaration.toLightClass() else null
|
||||||
|
}
|
||||||
+1
-78
@@ -5,16 +5,8 @@
|
|||||||
|
|
||||||
package org.jetbrains.kotlin.asJava.classes
|
package org.jetbrains.kotlin.asJava.classes
|
||||||
|
|
||||||
import com.intellij.psi.PsiClass
|
|
||||||
import com.intellij.psi.PsiElement
|
import com.intellij.psi.PsiElement
|
||||||
import com.intellij.psi.PsiMethod
|
import org.jetbrains.kotlin.psi.KtClassOrObject
|
||||||
import com.intellij.psi.impl.light.LightClass
|
|
||||||
import com.intellij.psi.impl.light.LightMethod
|
|
||||||
import org.jetbrains.kotlin.asJava.LightClassUtil
|
|
||||||
import org.jetbrains.kotlin.asJava.toLightClass
|
|
||||||
import org.jetbrains.kotlin.idea.KotlinLanguage
|
|
||||||
import org.jetbrains.kotlin.psi.*
|
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
|
|
||||||
|
|
||||||
open class KtUltraLightClassForLocalDeclaration(
|
open class KtUltraLightClassForLocalDeclaration(
|
||||||
classOrObject: KtClassOrObject,
|
classOrObject: KtClassOrObject,
|
||||||
@@ -29,72 +21,3 @@ open class KtUltraLightClassForLocalDeclaration(
|
|||||||
|
|
||||||
override fun getParent() = _parent
|
override fun getParent() = _parent
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getParentForLocalDeclaration(classOrObject: KtClassOrObject): PsiElement? {
|
|
||||||
fun getParentByPsiMethod(method: PsiMethod?, name: String?, forceMethodWrapping: Boolean): PsiElement? {
|
|
||||||
if (method == null || name == null) return null
|
|
||||||
|
|
||||||
var containingClass: PsiClass? = method.containingClass ?: return null
|
|
||||||
|
|
||||||
val currentFileName = classOrObject.containingFile.name
|
|
||||||
|
|
||||||
var createWrapper = forceMethodWrapping
|
|
||||||
// Use PsiClass wrapper instead of package light class to avoid names like "FooPackage" in Type Hierarchy and related views
|
|
||||||
if (containingClass is KtLightClassForFacade) {
|
|
||||||
containingClass = object : LightClass(containingClass as KtLightClassForFacade, KotlinLanguage.INSTANCE) {
|
|
||||||
override fun getName(): String = currentFileName
|
|
||||||
}
|
|
||||||
createWrapper = true
|
|
||||||
}
|
|
||||||
|
|
||||||
if (createWrapper) {
|
|
||||||
return object : LightMethod(classOrObject.manager, method, containingClass!!, KotlinLanguage.INSTANCE) {
|
|
||||||
override fun getParent(): PsiElement = getContainingClass()
|
|
||||||
override fun getName(): String = name
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return method
|
|
||||||
}
|
|
||||||
|
|
||||||
var declaration: PsiElement? = KtPsiUtil.getTopmostParentOfTypes(
|
|
||||||
classOrObject,
|
|
||||||
KtNamedFunction::class.java,
|
|
||||||
KtConstructor::class.java,
|
|
||||||
KtProperty::class.java,
|
|
||||||
KtAnonymousInitializer::class.java,
|
|
||||||
KtParameter::class.java
|
|
||||||
)
|
|
||||||
|
|
||||||
if (declaration is KtParameter) {
|
|
||||||
declaration = declaration.getStrictParentOfType<KtNamedDeclaration>()
|
|
||||||
}
|
|
||||||
|
|
||||||
if (declaration is KtFunction) {
|
|
||||||
return getParentByPsiMethod(
|
|
||||||
LightClassUtil.getLightClassMethod(declaration),
|
|
||||||
declaration.name,
|
|
||||||
forceMethodWrapping = false
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Represent the property as a fake method with the same name
|
|
||||||
if (declaration is KtProperty) {
|
|
||||||
return getParentByPsiMethod(
|
|
||||||
LightClassUtil.getLightClassPropertyMethods(declaration).getter,
|
|
||||||
declaration.name,
|
|
||||||
forceMethodWrapping = true
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
if (declaration is KtAnonymousInitializer) {
|
|
||||||
val parent = declaration.parent
|
|
||||||
val grandparent = parent.parent
|
|
||||||
|
|
||||||
if (parent is KtClassBody && grandparent is KtClassOrObject) {
|
|
||||||
return grandparent.toLightClass()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return if (declaration is KtClass) declaration.toLightClass() else null
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user