ElementColorProvider - Determining class of PSIElement or reference
Answered
I am adding color previews based on a custom framework we have. I have the logic to determine the color working and the GutterIcons are applied correctly, but I am new the Intellij SDK and could use some guidance on how I can check if a PsiElement meets at least one of two criteria:
1. The element is the class or a derivative of a given type (for instance class Bar in Kotlin)
2. Is calling a variable that is the class or a derivative of a given type (for instance class Bar in Kotlin)
It is a lot like finding usages.
Thanks in advance!
internal class FooColorProvider : ElementColorProvider {
override fun getColorFrom(element: PsiElement): Color? {
if (element !is LeafPsiElement && element.firstChild != null) {
return null
}
if (element.elementType.toString() != IDENTIFIER) {
return null
}
//todo@patches - Figure out how to determine if this is a reference to the specified class or the specified class itself
return getColorForElement(element)
}
override fun setColorTo(element: PsiElement, color: Color) {
if (element !is LeafPsiElement && element.firstChild != null) {
return
}
NavigationGutterIconBuilder.create(ColorIcon(GUTTER_ICON_SIZE, color))
.setTarget(element)
}
/*
* Private functions
*/
private fun getColorForElement(element: PsiElement): Color? {
// Omitted for brevity
return null
}
}
Please sign in to leave a comment.
You can inspect PSI structure of Kotlin code using PSI Viewer plugin https://plugins.jetbrains.com/plugin/227-psiviewer
Type check can be done on expose PsiType, inheritance check can be achieved using com.intellij.psi.util.InheritanceUtil#isInheritor(com.intellij.psi.PsiClass, java.lang.String)