Skip to content

Commit

Permalink
feat: make structure view works
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Sep 29, 2022
1 parent 0825062 commit a1d1b47
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/main/kotlin/com/feakin/intellij/FkFile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ class FkFile(viewProvider: FileViewProvider) : PsiFileBase(viewProvider, FkLangu
override fun toString(): String {
return "Feakin"
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.feakin.intellij.structure

import com.feakin.intellij.FkFile
import com.feakin.intellij.FkIcons
import com.feakin.intellij.psi.FeakinContextDeclaration
import com.feakin.intellij.psi.FeakinContextMapDeclaration
import com.feakin.intellij.psi.FeakinDeclaration
import com.intellij.ide.projectView.PresentationData
import com.intellij.ide.structureView.StructureViewTreeElement
import com.intellij.ide.util.treeView.TreeAnchorizer
Expand All @@ -13,9 +16,8 @@ import com.intellij.openapi.editor.DefaultLanguageHighlighterColors
import com.intellij.pom.Navigatable
import com.intellij.psi.NavigatablePsiElement
import com.intellij.psi.PsiElement
import com.intellij.psi.util.PsiTreeUtil

class FkStructureViewElement(private val element: NavigatablePsiElement) : StructureViewTreeElement {
class FkStructureViewElement(private val element: PsiElement) : StructureViewTreeElement {
companion object {
private val log: Logger = logger<FkStructureViewElement>()
}
Expand All @@ -37,12 +39,11 @@ class FkStructureViewElement(private val element: NavigatablePsiElement) : Struc
override fun getPresentation(): ItemPresentation {
val struct = PresentationData()
struct.setIcon(null)
log.warn(psi?.javaClass?.name)

when (val psi = psi) {
is FeakinContextMapDeclaration -> {
val structNameDeclaration = psi.contextMapName
struct.setAttributesKey(DefaultLanguageHighlighterColors.KEYWORD)
struct.setAttributesKey(DefaultLanguageHighlighterColors.IDENTIFIER)
struct.presentableText = structNameDeclaration.text
return struct
}
Expand All @@ -54,27 +55,54 @@ class FkStructureViewElement(private val element: NavigatablePsiElement) : Struc
return struct
}

is FkFile -> {
struct.setAttributesKey(DefaultLanguageHighlighterColors.KEYWORD)
struct.setIcon(FkIcons.FILE)
struct.presentableText = psi.originalFile.name
return struct
}

else -> {
val presentation = element.presentation
return presentation ?: PresentationData()
return PresentationData()
}
}
}

override fun getChildren(): Array<TreeElement> {
log.warn(psi?.javaClass?.name)
return when (val psi = psi) {
val treeElements = ArrayList<TreeElement>()

psi?.children?.forEach { child ->
val nodes = findContentNode(child).filterNotNull()
nodes.forEach {
treeElements.add(FkStructureViewElement(it))
}
}

return treeElements.toTypedArray()
}

// todo: make it better.
private fun findContentNode(psi: PsiElement?): List<PsiElement?> {
when (psi) {
is FeakinDeclaration -> {
val res = ArrayList<PsiElement?>()
psi.children.forEach {
res.addAll(findContentNode(it).filterNotNull())
}
return res
}

is FeakinContextMapDeclaration -> {
PsiTreeUtil.getChildrenOfTypeAsList(psi, FeakinContextMapDeclaration::class.java)
return listOf(psi)
}

is FeakinContextDeclaration -> {
PsiTreeUtil.getChildrenOfTypeAsList(psi, FeakinContextDeclaration::class.java)
return listOf(psi)
}

else -> {
emptyList()
return emptyList()
}
}.map(::FkStructureViewElement).toTypedArray()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,9 @@ class FkStructureViewModel(editor: Editor?, psiFile: FkFile) :
is FeakinContextMapDeclaration,
is FeakinContextDeclaration,
is FeakinAggregateDeclaration,

is FeakinContextName,
is FeakinContextMapName,
is FeakinAggregateName,
-> true

else -> false
}
}


}

0 comments on commit a1d1b47

Please sign in to comment.