InlayHintsProvider is not aligned

Answered

Hi, I added clickable text to Psielement using the Inlay Hints Provider, but it wasn't aligned. Does anyone know what the problem is?

 

There is no alignment here, which is incorrect

This is correct.

How can I solve this problem?

this is my code: 


class DartCodeAIInlay : InlayHintsProvider<DartAISetting> {

override val key: SettingsKey<DartAISetting>
get() = SettingsKey(name)

override val name: String
get() = "Open-AI-Setting"
override val previewText: String
get() {
return """
class TestClass {

void test() {

}
}
""".trimIndent()
}

override fun createSettings(): DartAISetting {
return DartAISetting.getInstance()
}

override fun getCollectorFor(
file: PsiFile,
editor: Editor,
settings: DartAISetting,
sink: InlayHintsSink
): InlayHintsCollector {
return object : FactoryInlayHintsCollector(editor) {
override fun collect(element: PsiElement, editor: Editor, sink: InlayHintsSink): Boolean {
val inlays = HintsInlayPresentationFactory(factory)
if (element is DartMethodDeclarationImpl) {
val text = inlays.simpleText("ASK OPENAI","", handle = object : InlayPresentationClickHandle{
override fun invoke(p1: MouseEvent, p2: Point) {
println("")
}
})
sink.addBlockElement(element.textRange.startOffset,true,true,-1,CustomRender(text))
}
return true
}
}
}

override fun createConfigurable(settings: DartAISetting): ImmediateConfigurable {
return AISettingPanel()
}

}

class CustomRender(val text: InlayPresentation) : InlayPresentation {


override val height: Int
get() = text.height
override val width: Int
get() = text.width

override fun addListener(listener: PresentationListener) {
text.addListener(listener)
}

override fun fireContentChanged(area: Rectangle) {
text.fireContentChanged(area)
}

override fun fireSizeChanged(previous: Dimension, current: Dimension) {
text.fireSizeChanged(previous, current)
}

override fun paint(g: Graphics2D, attributes: TextAttributes) {
text.paint(g, attributes)
}

override fun removeListener(listener: PresentationListener) {
text.removeListener(listener)
}

override fun toString(): String {
return "11"
}

}
0
2 comments

https://plugins.jetbrains.com/docs/intellij/inlay-hints.html
Please try and find similar implementations as described on this page. It looks like e.g. org.jetbrains.kotlin.idea.codeInsight.codevision.KotlinCodeVisionHintsCollector#prepareBlockElements does something similar

0

Thank you for your answer. I found the answer there and it worked well

override fun collect(element: PsiElement, editor: Editor, sink: InlayHintsSink): Boolean {
val inlays = HintsInlayPresentationFactory(factory)


val offset = element.textRange.startOffset
val line = editor.document.getLineNumber(offset)
val lineStart = editor.document.getLineStartOffset(line)
val indent = offset - lineStart //缩进
val indentText = StringUtil.repeat(" ",indent)
if (element is DartMethodDeclarationImpl) {
val text = factory.text("$indentText ")

val ai = inlays.simpleText("ASK OPENAI","", handle = object : InlayPresentationClickHandle{
override fun invoke(p1: MouseEvent, p2: Point) {
println("")
}
})
val newF = factory.seq(text,ai)
sink.addBlockElement(lineStart,true,true,0,CustomRender(newF))
}
return true
}
0

Please sign in to leave a comment.