From 9e061555afa2bc2740452f8ebb565f6cb442c77c Mon Sep 17 00:00:00 2001 From: Nikolay Krasko Date: Tue, 6 Mar 2018 12:41:14 +0300 Subject: [PATCH] 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 --- .../KotlinStructureViewElement.kt | 24 ++++++++++++------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/structureView/KotlinStructureViewElement.kt b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/structureView/KotlinStructureViewElement.kt index c513bb9d73a..54e660ef86d 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/structureView/KotlinStructureViewElement.kt +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/structureView/KotlinStructureViewElement.kt @@ -35,7 +35,7 @@ import kotlin.properties.ReadWriteProperty import kotlin.reflect.KProperty class KotlinStructureViewElement( - val element: NavigatablePsiElement, + element: NavigatablePsiElement, private val isInherited: Boolean = false ) : PsiTreeElementBase(element), Queryable { @@ -74,6 +74,8 @@ class KotlinStructureViewElement( } override fun getChildrenBase(): Collection { + val element = element + val children = when (element) { is KtFile -> element.declarations is KtClass -> element.getStructureDeclarations() @@ -104,14 +106,18 @@ class KotlinStructureViewElement( private fun isPublic(descriptor: DeclarationDescriptor?) = (descriptor as? DeclarationDescriptorWithVisibility)?.visibility == Visibilities.PUBLIC - private fun countDescriptor(): DeclarationDescriptor? = when { - !element.isValid -> null - element !is KtDeclaration -> null - element is KtAnonymousInitializer -> null - else -> runReadAction { - if (!DumbService.isDumb(element.getProject())) { - element.resolveToDescriptorIfAny() - } else null + private fun countDescriptor(): DeclarationDescriptor? { + val element = element + return when { + element == null -> null + !element.isValid -> null + element !is KtDeclaration -> null + element is KtAnonymousInitializer -> null + else -> runReadAction { + if (!DumbService.isDumb(element.getProject())) { + element.resolveToDescriptorIfAny() + } else null + } } } }