InlayModel addAfterLineEndElement
Answered
I want to add some text after the caret. I found that InlayModel.addAfterLineEndElement may be the API I need. I wrote the below code, but the position of added text is strange.
val caret = editor.caretModel
val lineEnd = caret.visualLineEnd
val inlayModel = editor.inlayModel
inlayModel.addAfterLineEndElement(lineEnd, true, object : EditorCustomElementRenderer {
override fun getContextMenuGroupId(inlay: Inlay<*>): String? {
return null
}
override fun calcWidthInPixels(inlay: Inlay<*>): Int {
return (JBUI.pixScale(inlay.editor.component) * 11).roundToInt()
}
override fun paint(inlay: Inlay<*>, g: Graphics, targetRegion: Rectangle, textAttributes: TextAttributes) {
g.color = JBColor.GRAY
g.drawString("Hello world", targetRegion.x,
targetRegion.y)
}
})
Below is the running example. The `Hello world` is added after I typed I.
I tried different positon for the g.drawString function but no one works.
So what is the coordinate system of the `g` parameter in the paint function? And how should I get the x and y
for the g.drawString function so that I can add the `Hello world` after `I`?
Please sign in to leave a comment.
Finally I used the x computed from editor.visualPositionToXY and y from targetRegion to make it work. Though I am not sure whether it is the right way.
Actually the y has no effect. I tried:
targetRegion.y,
0,
pos.y
-font.size
But the viewer shows the same text at the same position.
Take a look at existing renderers, e.g. com.intellij.xdebugger.impl.XDebuggerInlayUtil.MyRenderer - you'll need to use FontMetrics
Solved it! Thank you so much!