|
| 1 | +package com.wavesplatform.rideplugin.editor |
| 2 | + |
| 3 | +import com.intellij.ide.structureView.* |
| 4 | +import com.intellij.ide.structureView.StructureViewModel.ElementInfoProvider |
| 5 | +import com.intellij.ide.structureView.impl.common.PsiTreeElementBase |
| 6 | +import com.intellij.ide.util.treeView.smartTree.Sorter |
| 7 | +import com.intellij.lang.PsiStructureViewFactory |
| 8 | +import com.intellij.openapi.editor.Editor |
| 9 | +import com.intellij.psi.PsiElement |
| 10 | +import com.intellij.psi.PsiFile |
| 11 | +import com.intellij.psi.util.PsiTreeUtil |
| 12 | +import com.wavesplatform.rideplugin.RideFile |
| 13 | +import com.wavesplatform.rideplugin.icons.RideIcons |
| 14 | +import com.wavesplatform.rideplugin.psi.* |
| 15 | +import javax.swing.Icon |
| 16 | + |
| 17 | +//show only first level definition |
| 18 | +class RideStructureViewFactory : PsiStructureViewFactory { |
| 19 | + override fun getStructureViewBuilder(psiFile: PsiFile): StructureViewBuilder { |
| 20 | + return object : TreeBasedStructureViewBuilder() { |
| 21 | + override fun createStructureViewModel(editor: Editor?): StructureViewModel { |
| 22 | + return RideModel(psiFile, editor) |
| 23 | + } |
| 24 | + |
| 25 | + override fun isRootNodeShown(): Boolean { |
| 26 | + return false |
| 27 | + } |
| 28 | + } |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +class RideModel(psiFile: PsiFile, editor: Editor?) : |
| 33 | + StructureViewModelBase(psiFile, RideStructureViewElement(psiFile)), ElementInfoProvider { |
| 34 | + init { |
| 35 | + withSuitableClasses( |
| 36 | + RideFile::class.java, |
| 37 | + RideNamedElement::class.java, |
| 38 | + ) |
| 39 | + .withSorters(Sorter.ALPHA_SORTER) |
| 40 | + |
| 41 | + } |
| 42 | + |
| 43 | + override fun isAlwaysShowsPlus(element: StructureViewTreeElement?): Boolean { |
| 44 | + return false |
| 45 | + } |
| 46 | + |
| 47 | + override fun isAlwaysLeaf(element: StructureViewTreeElement?): Boolean { |
| 48 | + return element?.value is RideSimpleRefExpr |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +class RideStructureViewElement(e: PsiElement) : PsiTreeElementBase<PsiElement?>(e) { |
| 53 | + override fun getPresentableText(): String { |
| 54 | + val currentElement = element |
| 55 | + if (currentElement is RideFile) { |
| 56 | + return "File:" + currentElement.name |
| 57 | + } |
| 58 | + if (currentElement is RideNamedElement) { |
| 59 | + //todo fix due to lack of flexibility |
| 60 | + return "Function:" + currentElement.name |
| 61 | + } |
| 62 | + return "Undefined" |
| 63 | + } |
| 64 | + |
| 65 | + override fun getChildrenBase(): Collection<StructureViewTreeElement> { |
| 66 | + val result: MutableList<StructureViewTreeElement> = arrayListOf() |
| 67 | + val element = element |
| 68 | + if (element is RideFile) { |
| 69 | + val rideAll: RideAll = PsiTreeUtil.findChildOfType(element, RideAll::class.java) ?: return result |
| 70 | + for (next: RideStatement in rideAll.statementList) { |
| 71 | + val child = next.firstChild |
| 72 | + if (child is RideFuncExpr) { |
| 73 | + child.functionDefinition?.let { |
| 74 | + result.add(RideStructureViewElement(it)) |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + return result |
| 80 | + } |
| 81 | + |
| 82 | + override fun getIcon(open: Boolean): Icon { |
| 83 | + return RideIcons.FUNCTION |
| 84 | + } |
| 85 | +} |
0 commit comments