Use anchor for element to avoid working with invalid element (KT-22631)

Element in constructor can be invalidated when dumb mode begins/ends.
Working with invalid elements can produce nodes without icons and bad
presentation.

 #KT-22631 Fixed
This commit is contained in:
Nikolay Krasko
2018-03-06 12:41:14 +03:00
parent bb9933a8f9
commit 9e061555af
@@ -35,7 +35,7 @@ import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty import kotlin.reflect.KProperty
class KotlinStructureViewElement( class KotlinStructureViewElement(
val element: NavigatablePsiElement, element: NavigatablePsiElement,
private val isInherited: Boolean = false private val isInherited: Boolean = false
) : PsiTreeElementBase<NavigatablePsiElement>(element), Queryable { ) : PsiTreeElementBase<NavigatablePsiElement>(element), Queryable {
@@ -74,6 +74,8 @@ class KotlinStructureViewElement(
} }
override fun getChildrenBase(): Collection<StructureViewTreeElement> { override fun getChildrenBase(): Collection<StructureViewTreeElement> {
val element = element
val children = when (element) { val children = when (element) {
is KtFile -> element.declarations is KtFile -> element.declarations
is KtClass -> element.getStructureDeclarations() is KtClass -> element.getStructureDeclarations()
@@ -104,7 +106,10 @@ class KotlinStructureViewElement(
private fun isPublic(descriptor: DeclarationDescriptor?) = private fun isPublic(descriptor: DeclarationDescriptor?) =
(descriptor as? DeclarationDescriptorWithVisibility)?.visibility == Visibilities.PUBLIC (descriptor as? DeclarationDescriptorWithVisibility)?.visibility == Visibilities.PUBLIC
private fun countDescriptor(): DeclarationDescriptor? = when { private fun countDescriptor(): DeclarationDescriptor? {
val element = element
return when {
element == null -> null
!element.isValid -> null !element.isValid -> null
element !is KtDeclaration -> null element !is KtDeclaration -> null
element is KtAnonymousInitializer -> null element is KtAnonymousInitializer -> null
@@ -114,6 +119,7 @@ class KotlinStructureViewElement(
} else null } else null
} }
} }
}
} }
private class AssignableLazyProperty<in R, T : Any>(val init: () -> T) : ReadWriteProperty<R, T> { private class AssignableLazyProperty<in R, T : Any>(val init: () -> T) : ReadWriteProperty<R, T> {