Uast: a workaround for getting parent for KotlinUAnonymousClass
when it is used in field initialisation
This commit is contained in:
@@ -43,7 +43,7 @@ abstract class KotlinAbstractUElement(private val givenParent: UElement?) : Kotl
|
|||||||
protected open fun convertParent(): UElement? {
|
protected open fun convertParent(): UElement? {
|
||||||
@Suppress("DEPRECATION")
|
@Suppress("DEPRECATION")
|
||||||
val psi = psi //TODO: `psi` is deprecated but it seems that it couldn't be simply replaced for this case
|
val psi = psi //TODO: `psi` is deprecated but it seems that it couldn't be simply replaced for this case
|
||||||
var parent = psi?.parent ?: psi?.containingFile
|
var parent = psi?.parent ?: sourcePsi?.parent ?: psi?.containingFile
|
||||||
|
|
||||||
if (psi is KtLightClassForLocalDeclaration) {
|
if (psi is KtLightClassForLocalDeclaration) {
|
||||||
val originParent = psi.kotlinOrigin.parent
|
val originParent = psi.kotlinOrigin.parent
|
||||||
|
|||||||
@@ -43,7 +43,7 @@ abstract class KotlinAbstractUElement(private val givenParent: UElement?) : Kotl
|
|||||||
protected open fun convertParent(): UElement? {
|
protected open fun convertParent(): UElement? {
|
||||||
@Suppress("DEPRECATION")
|
@Suppress("DEPRECATION")
|
||||||
val psi = psi //TODO: `psi` is deprecated but it seems that it couldn't be simply replaced for this case
|
val psi = psi //TODO: `psi` is deprecated but it seems that it couldn't be simply replaced for this case
|
||||||
var parent = psi?.parent ?: psi?.containingFile
|
var parent = psi?.parent ?: sourcePsi?.parent ?: psi?.containingFile
|
||||||
|
|
||||||
if (psi is KtLightClassForLocalDeclaration) {
|
if (psi is KtLightClassForLocalDeclaration) {
|
||||||
val originParent = psi.kotlinOrigin.parent
|
val originParent = psi.kotlinOrigin.parent
|
||||||
|
|||||||
@@ -133,6 +133,20 @@ class KotlinDetachedUastTest : KotlinLightCodeInsightFixtureTestCase() {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun testAnonymousInnerClassWithIDELightClasses() {
|
||||||
|
|
||||||
|
val detachedClass = myFixture.configureByText("MyClass.kt","""
|
||||||
|
class MyClass() {
|
||||||
|
private val obj = object : MyClass() {}
|
||||||
|
}
|
||||||
|
""")
|
||||||
|
|
||||||
|
val anonymousClass = detachedClass.findUElementByTextFromPsi<UObjectLiteralExpression>("object : MyClass() {}")
|
||||||
|
.let { uObjectLiteralExpression -> uObjectLiteralExpression.declaration }
|
||||||
|
TestCase.assertEquals("UClass (name = null), UObjectLiteralExpression, UField (name = obj), UClass (name = MyClass), UFile (package = )", generateSequence<UElement>(anonymousClass, { it.uastParent }).joinToString { it.asLogString() })
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun <T> T?.orFail(msg: String): T = this ?: error(msg)
|
fun <T> T?.orFail(msg: String): T = this ?: error(msg)
|
||||||
Reference in New Issue
Block a user