How do i make a InlayHint clickable
Answered
Hello everyone,
I am currently developing a JetBrains plugin in Kotlin, and I want to implement Inlay Hints for content keys in HTML files. I can successfully display the hints, but I need assistance in making them clickable.
following is my InlayHintProvider
import com.intellij.codeInsight.hints.declarative.*
import com.intellij.openapi.editor.Editor
import com.intellij.psi.*
class ContentKeyInlayHintsProvider : InlayHintsProvider {
companion object {
const val PROVIDER_ID: String = "content.key.hints"
}
override fun createCollector(file: PsiFile, editor: Editor): InlayHintsCollector {
return Collector()
}
private class Collector : SharedBypassCollector {
override fun collectFromElement(element: PsiElement, sink: InlayTreeSink) {
if (element is PsiLiteralValue && element.value is String) {
val text = element.value as? String ?: return
if (isValidContentKey(text)) {
val position = InlineInlayPosition(element.textRange.endOffset, relatedToPrevious = true)
sink.addPresentation(position, hasBackground = true) {
text(text)
}
}
}
}
private fun isValidContentKey(key: String): Boolean {
return key.matches(Regex("^[a-zA-Z0-9]+(\\.[a-zA-Z0-9]+)+\$"))
}
}
}
Thats how it looks in action
I am looking for similar functionality to the versions controlle inlay hint, i.e. clickable etc.
Please sign in to leave a comment.
Hello,
you can extend
com.intellij.codeInsight.hints.declarative.InlayActionHandler
and register it in yourplugin.xml
. Then, create ancom.intellij.codeInsight.hints.declarative.InlayActionData
object with the ID of your handler and pass that as an argument tocom.intellij.codeInsight.hints.declarative.PresentationTreeBuilder#text
or#clickHandlerScope
method when building your inlay hint. This will make that part of your inlay hint Ctrl/Cmd+Left-clickable. (Note that plain left-click is reserved for collapsing/expanding collapsible lists – seePresentationTreeBuilder#collapsibleList
)com.intellij.codeInsight.hints.declarative.PsiPointerInlayActionNavigationHandler
is an example of such a handler. If navigation is what you're trying to do, you can use it. E.g.Another option you have is to add an action to the menu available by right-clicking the inlay hint. For that, you create an ordinary action and add it to the “InlayMenu” group when registering. See
com.intellij.codeInsight.hints.declarative.impl.DisableDeclarativeInlayAction
for an example.