InlayModel addAfterLineEndElement


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,

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`?

Comment actions Permalink

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.

Comment actions Permalink
g.drawString(text, pos.x, targetRegion.y)

Actually the y has no effect. I tried:





But the viewer shows the same text at the same position.



Comment actions Permalink

Take a look at existing renderers, e.g. com.intellij.xdebugger.impl.XDebuggerInlayUtil.MyRenderer - you'll need to use FontMetrics


Comment actions Permalink

Solved it! Thank you so much!


Please sign in to leave a comment.