idea-plugin / arrow.meta.ide.dsl.editor.icon / IconProviderSyntax / addIcon

addIcon

open fun <A : PsiElement> MetaIde.addIcon(icon: Icon, transform: (psiElement: PsiElement, flag: Int) -> A? = Noop.nullable2()): ExtensionPhase

registers an IconProvider. One minimal example from KotlinIconProvider, may look like this:

import arrow.meta.ide.MetaIde
import arrow.meta.ide.IdePlugin
import arrow.meta.ide.invoke
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.idea.KotlinIcons
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi.KtObjectDeclaration
import org.jetbrains.kotlin.utils.addToStdlib.safeAs

val MetaIde.fileAndStructureViewIcons: IdePlugin
  get() = "File- and StructureViewIcons" {
    meta(
      addIcon(KotlinIcons.GRADLE_SCRIPT) { psi: PsiElement, _: Int ->
        psi.safeAs<KtFile>()?.takeIf { it.isScript() && it.name.endsWith(".gradle.kts") }
      },
      addIcon(KotlinIcons.OBJECT) { psi, _ ->
        psi.safeAs<KtObjectDeclaration>()
      }
    )
  }

This implementation creates 2 IconProviders. The first registers the KotlinIcons.GRADLE_SCRIPT Icon to any Kotlin ScriptFile, which ends with .gradle.kts. The other registers KotlinIcons.OBJECT Icon to any KtObjectDeclaration, so that it appears in the StructureView. The advantage of registering multiple IconProviders than one, which orchestrate all possible Icons, is that it is easier to Debug and Test each Icon specifically. The parameter flag from transform is used to compose more complex transform functions considering Iconable.ICON_FLAG_VISIBILITY, Iconable.ICON_FLAG_IGNORE_MASK or Iconable.ICON_FLAG_READ_STATUS.

Do you like Arrow?

Arrow Org
<