Fix structure view auto-update (KT-21733, KT-19519)

- Fixed by subclassing PsiTreeElementBase instead of StructureViewTreeElement
- Warnings cleanup

 #KT-19519 Fixed
 #KT-21733 Fixed
This commit is contained in:
Nikolay Krasko
2017-12-13 18:53:29 +03:00
parent b133189309
commit c28a7d8f6d
3 changed files with 44 additions and 39 deletions
+1
View File
@@ -12,6 +12,7 @@
<w>memoize</w>
<w>memoized</w>
<w>multiline</w>
<w>navigatable</w>
<w>preload</w>
<w>preloader</w>
<w>preloading</w>
@@ -55,6 +55,8 @@ class KotlinInheritedMembersNodeProvider: InheritedMembersNodeProvider<TreeEleme
children.add(KotlinStructureViewElement(superTypeMember, memberDescriptor, true))
}
}
CallableMemberDescriptor.Kind.DECLARATION -> Unit /* Don't show */
CallableMemberDescriptor.Kind.SYNTHESIZED -> Unit /* Don't show */
}
}
@@ -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<NavigatablePsiElement>(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<TreeElement> =
childrenDeclarations.map { KotlinStructureViewElement(it, false) }.toTypedArray()
@TestOnly
override fun putInfo(info: MutableMap<String, String?>) {
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<String, String?>) {
// 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<StructureViewTreeElement> {
return childrenDeclarations.map { KotlinStructureViewElement(it, false) }
}
private val childrenDeclarations: List<KtDeclaration>
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