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

0
4 comments
Avatar
Permanently deleted user

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.

0
Avatar
Permanently deleted user
g.drawString(text, pos.x, targetRegion.y)

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.

 

 

0

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

 

0
Avatar
Permanently deleted user

Solved it! Thank you so much!

0

Please sign in to leave a comment.