From c28a7d8f6d3eae1aa41db5627ff153ade9e850d8 Mon Sep 17 00:00:00 2001 From: Nikolay Krasko Date: Wed, 13 Dec 2017 18:53:29 +0300 Subject: [PATCH] Fix structure view auto-update (KT-21733, KT-19519) - Fixed by subclassing PsiTreeElementBase instead of StructureViewTreeElement - Warnings cleanup #KT-19519 Fixed #KT-21733 Fixed --- .idea/dictionaries/Nikolay_Krasko.xml | 1 + .../KotlinInheritedMembersNodeProvider.kt | 2 + .../KotlinStructureViewElement.kt | 80 ++++++++++--------- 3 files changed, 44 insertions(+), 39 deletions(-) diff --git a/.idea/dictionaries/Nikolay_Krasko.xml b/.idea/dictionaries/Nikolay_Krasko.xml index 45a4572a3bb..83417f16864 100644 --- a/.idea/dictionaries/Nikolay_Krasko.xml +++ b/.idea/dictionaries/Nikolay_Krasko.xml @@ -12,6 +12,7 @@ memoize memoized multiline + navigatable preload preloader preloading diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/structureView/KotlinInheritedMembersNodeProvider.kt b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/structureView/KotlinInheritedMembersNodeProvider.kt index ca23b179c3b..39e894b6d31 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/structureView/KotlinInheritedMembersNodeProvider.kt +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/structureView/KotlinInheritedMembersNodeProvider.kt @@ -55,6 +55,8 @@ class KotlinInheritedMembersNodeProvider: InheritedMembersNodeProvider Unit /* Don't show */ + CallableMemberDescriptor.Kind.SYNTHESIZED -> Unit /* Don't show */ } } 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 868de382c33..0498e77d88c 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 @@ -17,7 +17,7 @@ package org.jetbrains.kotlin.idea.structureView import com.intellij.ide.structureView.StructureViewTreeElement -import com.intellij.ide.util.treeView.smartTree.TreeElement +import com.intellij.ide.structureView.impl.common.PsiTreeElementBase import com.intellij.navigation.ItemPresentation import com.intellij.openapi.project.DumbService import com.intellij.openapi.ui.Queryable @@ -30,46 +30,20 @@ import org.jetbrains.kotlin.descriptors.Visibilities import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny import org.jetbrains.kotlin.idea.util.application.runReadAction import org.jetbrains.kotlin.psi.* +import javax.swing.Icon class KotlinStructureViewElement(val element: NavigatablePsiElement, - val isInherited: Boolean = false) : StructureViewTreeElement, Queryable { - private var presentation: KotlinStructureElementPresentation? = null + private val isInherited: Boolean = false) : PsiTreeElementBase(element), Queryable { + private var _presentation: KotlinStructureElementPresentation? = null + get() { + if (field == null) { + field = KotlinStructureElementPresentation(isInherited, element, _descriptor) + } - constructor(element: NavigatablePsiElement, descriptor: DeclarationDescriptor, isInherited: Boolean) : this(element, isInherited){ - if (element !is KtElement) { - // Avoid storing descriptor in fields - presentation = KotlinStructureElementPresentation(isInherited, element, descriptor) - } - } - - override fun getValue() = element - - override fun navigate(requestFocus: Boolean) { - element.navigate(requestFocus) - } - - override fun canNavigate() = element.canNavigate() - - override fun canNavigateToSource() = element.canNavigateToSource() - - override fun getPresentation(): ItemPresentation { - if (presentation == null) { - presentation = KotlinStructureElementPresentation(isInherited, element, descriptor) + return field } - return presentation!! - } - - override fun getChildren(): Array = - childrenDeclarations.map { KotlinStructureViewElement(it, false) }.toTypedArray() - - @TestOnly - override fun putInfo(info: MutableMap) { - info.put("text", getPresentation().presentableText) - info.put("location", getPresentation().locationString) - } - - private val descriptor: DeclarationDescriptor? + private val _descriptor: DeclarationDescriptor? get() { if (!(element.isValid && element is KtDeclaration)) { return null @@ -87,6 +61,37 @@ class KotlinStructureViewElement(val element: NavigatablePsiElement, } } + val isPublic: Boolean + get() { + return (_descriptor as? DeclarationDescriptorWithVisibility)?.visibility == Visibilities.PUBLIC + } + + constructor(element: NavigatablePsiElement, descriptor: DeclarationDescriptor, isInherited: Boolean) : this(element, isInherited){ + if (element !is KtElement) { + // Avoid storing descriptor in fields + _presentation = KotlinStructureElementPresentation(isInherited, element, descriptor) + } + } + + override fun getPresentation(): ItemPresentation = _presentation!! + override fun getLocationString(): String? = _presentation!!.locationString + override fun getIcon(open: Boolean): Icon? = _presentation!!.getIcon(open) + override fun getPresentableText(): String? = _presentation!!.presentableText + + @TestOnly + override fun putInfo(info: MutableMap) { + // Sanity check for API consistency + assert(presentation.presentableText == presentableText) { "Two different ways of getting presentableText" } + assert(presentation.locationString == locationString) { "Two different ways of getting locationString" } + + info["text"] = presentableText + info["location"] = locationString + } + + override fun getChildrenBase(): Collection { + return childrenDeclarations.map { KotlinStructureViewElement(it, false) } + } + private val childrenDeclarations: List get() = when (element) { is KtFile -> element.declarations @@ -111,9 +116,6 @@ class KotlinStructureViewElement(val element: NavigatablePsiElement, return result } - - val isPublic: Boolean - get() = (descriptor as? DeclarationDescriptorWithVisibility)?.visibility == Visibilities.PUBLIC } fun KtClassOrObject.getStructureDeclarations() = primaryConstructorParameters.filter { it.hasValOrVar() } + declarations