[ULC] Fix annotations invalid parents

Fixed #KT-45287
This commit is contained in:
Igor Yakovlev
2021-03-04 18:29:17 +01:00
parent 179cf303da
commit b744f41c0d
3 changed files with 11 additions and 1 deletions
@@ -23,12 +23,16 @@ import com.intellij.psi.impl.light.LightElement
import org.jetbrains.kotlin.idea.KotlinLanguage
import org.jetbrains.kotlin.psi.KtElement
abstract class KtLightElementBase(private val parent: PsiElement) : LightElement(parent.manager, KotlinLanguage.INSTANCE) {
abstract class KtLightElementBase(private var parent: PsiElement) : LightElement(parent.manager, KotlinLanguage.INSTANCE) {
override fun toString() = "${this.javaClass.simpleName} of $parent"
override fun getParent(): PsiElement = parent
abstract val kotlinOrigin: KtElement?
internal fun setParent(newParent: PsiElement) {
parent = newParent
}
override fun getText() = kotlinOrigin?.text ?: ""
override fun getTextRange() = kotlinOrigin?.textRange ?: TextRange.EMPTY_RANGE
override fun getTextOffset() = kotlinOrigin?.textOffset ?: 0
@@ -61,6 +61,10 @@ abstract class KtLightModifierList<out T : KtLightElement<KtModifierListOwner, P
private fun computeAnnotations(): List<KtLightAbstractAnnotation> {
val annotationsForEntries = owner.givenAnnotations ?: lightAnnotationsForEntries(this)
//TODO: Hacky way to update wrong parents for annotations
annotationsForEntries.forEach {
it.parent = this
}
val modifierListOwner = parent
if (modifierListOwner is KtLightClassForSourceDeclaration && modifierListOwner.isAnnotationType) {
@@ -61,3 +61,5 @@ class Foo @Anno constructor(dependency: MyDependency) {
class Example(@field:Ann val foo: String, // annotate Java field
@get:Ann val bar: String, // annotate Java getter
@param:Ann val quux: String) // annotate Java constructor parameter
class CtorAnnotations(@Anno val x: String, @param:Anno val y: String, val z: String)