Skip to content

Commit 13314c6

Browse files
committed
add Inlay description
1 parent c9a2eed commit 13314c6

File tree

2 files changed

+49
-35
lines changed

2 files changed

+49
-35
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.github.adrienpessu.sarifviewer.toolWindow
2+
3+
import com.intellij.openapi.editor.EditorCustomElementRenderer
4+
import com.intellij.openapi.editor.Inlay
5+
import com.intellij.openapi.editor.impl.EditorImpl
6+
import com.intellij.openapi.editor.markup.TextAttributes
7+
import java.awt.Font
8+
import java.awt.Graphics
9+
import java.awt.Rectangle
10+
11+
class MyCustomInlayRenderer(private val text: String) : EditorCustomElementRenderer {
12+
13+
private val myFont = Font("Courrier new", Font.ITALIC, 12)
14+
override fun paint(inlay: Inlay<*>, g: Graphics, targetRegion: Rectangle, textAttributes: TextAttributes) {
15+
16+
(inlay.editor as EditorImpl).apply {
17+
g.font = myFont
18+
g.color = colorsScheme.defaultForeground
19+
g.drawString(text, targetRegion.x, targetRegion.y + ascent)
20+
}
21+
}
22+
23+
override fun calcWidthInPixels(inlay: Inlay<*>): Int {
24+
return (inlay.editor as EditorImpl).getFontMetrics(myFont.style).stringWidth(text)
25+
}
26+
}

src/main/kotlin/com/github/adrienpessu/sarifviewer/toolWindow/SarifViewerWindowFactory.kt

Lines changed: 23 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,16 @@ import com.intellij.openapi.actionSystem.ActionManager
1919
import com.intellij.openapi.actionSystem.AnAction
2020
import com.intellij.openapi.components.service
2121
import com.intellij.openapi.diagnostic.thisLogger
22-
import com.intellij.openapi.editor.ScrollType
23-
import com.intellij.openapi.fileEditor.FileDocumentManager
22+
import com.intellij.openapi.editor.Editor
2423
import com.intellij.openapi.fileEditor.FileEditorManager
2524
import com.intellij.openapi.fileEditor.OpenFileDescriptor
2625
import com.intellij.openapi.project.DumbService
2726
import com.intellij.openapi.project.Project
2827
import com.intellij.openapi.ui.ComboBox
29-
import com.intellij.openapi.ui.MessageType
30-
import com.intellij.openapi.ui.popup.Balloon
31-
import com.intellij.openapi.ui.popup.JBPopupFactory
3228
import com.intellij.openapi.vfs.VirtualFileManager
3329
import com.intellij.openapi.wm.ToolWindow
3430
import com.intellij.openapi.wm.ToolWindowFactory
3531
import com.intellij.ui.ScrollPaneFactory
36-
import com.intellij.ui.awt.RelativePoint
3732
import com.intellij.ui.components.JBPanel
3833
import com.intellij.ui.components.JBTabbedPane
3934
import com.intellij.ui.content.ContentFactory
@@ -551,6 +546,7 @@ class SarifViewerWindowFactory : ToolWindowFactory {
551546
leaf.address.split(":")[1].toInt(),
552547
0,
553548
leaf.level,
549+
leaf.ruleId,
554550
leaf.ruleDescription
555551
)
556552

@@ -578,9 +574,16 @@ class SarifViewerWindowFactory : ToolWindowFactory {
578574
lineNumber: Int,
579575
columnNumber: Int = 0,
580576
level: String = "",
581-
ruleDescription: String = ""
577+
rule: String = "",
578+
description: String = ""
582579
) {
583580

581+
val editor: Editor = FileEditorManager.getInstance(project).selectedTextEditor ?: return
582+
val inlayModel = editor.inlayModel
583+
584+
inlayModel.getBlockElementsInRange(0, editor.document.textLength).filter { it.renderer is MyCustomInlayRenderer }
585+
.forEach { it.dispose() }
586+
584587
VirtualFileManager.getInstance().findFileByNioPath(Path.of("${project.basePath}/$path"))
585588
?.let { virtualFile ->
586589
FileEditorManager.getInstance(project).openTextEditor(
@@ -592,36 +595,21 @@ class SarifViewerWindowFactory : ToolWindowFactory {
592595
),
593596
true // request focus to editor
594597
)
595-
FileDocumentManager.getInstance().getDocument(virtualFile)?.let { document ->
596-
if (ruleDescription.isNotEmpty()) {
597-
val lineStartOffset = document.getLineStartOffset(lineNumber - 1)
598-
val lineEndOffset = document.getLineEndOffset(lineNumber - 1)
599-
val editor = FileEditorManager.getInstance(project).selectedTextEditor ?: return
600-
editor.caretModel.moveToOffset(lineStartOffset)
601-
editor.scrollingModel.scrollToCaret(ScrollType.CENTER)
602-
editor.selectionModel.setSelection(lineStartOffset, lineEndOffset)
603-
604-
val messageType = when (level) {
605-
"error" -> MessageType.ERROR
606-
"warning" -> MessageType.WARNING
607-
else -> MessageType.INFO
608-
}
598+
val editor: Editor = FileEditorManager.getInstance(project).selectedTextEditor ?: return
599+
val inlayModel = editor.inlayModel
609600

610-
// add a balloon on the selection
611-
val balloon = JBPopupFactory.getInstance().createHtmlTextBalloonBuilder(
612-
ruleDescription,
613-
messageType,
614-
null
615-
).createBalloon()
616-
balloon.show(
617-
RelativePoint(
618-
editor.contentComponent,
619-
editor.visualPositionToXY(editor.caretModel.visualPosition)
620-
), Balloon.Position.above
621-
)
622-
}
623-
}
601+
val offset = editor.document.getLineStartOffset(lineNumber - 1)
624602

603+
val icon = when (level) {
604+
"error" -> "🛑"
605+
"warning" -> "⚠️"
606+
"note" -> "📝"
607+
else -> ""
608+
}
609+
val description = "$icon $rule: $description"
610+
if (description.isNotEmpty()) {
611+
inlayModel.addBlockElement(offset, true, true, 1, MyCustomInlayRenderer(description))
612+
}
625613
}
626614
}
627615

0 commit comments

Comments
 (0)