1
0
mirror of https://github.com/chylex/IntelliJ-Inspection-Lens.git synced 2025-05-07 08:34:06 +02:00

Refactor to organize responsibilities and clarify code

This commit is contained in:
chylex 2022-07-08 00:18:44 +02:00
parent edef5787b4
commit 4d46af3224
Signed by: chylex
GPG Key ID: 4DE42C8F19A80548
2 changed files with 34 additions and 16 deletions
src/main/kotlin/com/chylex/intellij/inspectionlens

View File

@ -26,13 +26,9 @@ class EditorInlayLensManager private constructor(private val editor: Editor) {
}
}
private fun updateRenderer(renderer: LensRenderer, info: HighlightInfo) {
renderer.text = info.description.takeIf(String::isNotBlank)?.let(::addMissingPeriod) ?: " "
renderer.severity = LensSeverity.from(info.severity)
}
private fun addMissingPeriod(text: String): String {
return if (text.endsWith('.')) text else "$text."
private fun getInlayHintOffset(info: HighlightInfo): Int {
// Ensures a highlight at the end of a line does not overflow to the next line.
return info.actualEndOffset - 1
}
}
@ -41,12 +37,12 @@ class EditorInlayLensManager private constructor(private val editor: Editor) {
fun show(highlighter: RangeHighlighter, info: HighlightInfo) {
val currentInlay = inlays[highlighter]
if (currentInlay != null && currentInlay.isValid) {
updateRenderer(currentInlay.renderer, info)
currentInlay.renderer.setPropertiesFrom(info)
currentInlay.update()
}
else {
val offset = info.actualEndOffset - 1
val renderer = LensRenderer().also { updateRenderer(it, info) }
val offset = getInlayHintOffset(info)
val renderer = LensRenderer(info)
val properties = InlayProperties().relatesToPrecedingText(true).priority(-offset)
editor.inlayModel.addAfterLineEndElement(offset, properties, renderer)?.let {

View File

@ -1,5 +1,6 @@
package com.chylex.intellij.inspectionlens
import com.intellij.codeInsight.daemon.impl.HighlightInfo
import com.intellij.codeInsight.daemon.impl.HintRenderer
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.editor.Inlay
@ -11,23 +12,44 @@ import java.awt.Rectangle
/**
* Renders the text of an inspection lens.
*/
class LensRenderer : HintRenderer(null) {
private companion object {
private val ATTRIBUTES = TextAttributes(null, null, null, null, Font.ITALIC)
class LensRenderer(info: HighlightInfo) : HintRenderer(null) {
private lateinit var severity: LensSeverity
init {
setPropertiesFrom(info)
}
var severity = LensSeverity.OTHER
fun setPropertiesFrom(info: HighlightInfo) {
text = getValidDescriptionText(info.description)
severity = LensSeverity.from(info.severity)
}
override fun paint(inlay: Inlay<*>, g: Graphics, r: Rectangle, textAttributes: TextAttributes) {
r.y += 1
fixBaselineForTextRendering(r)
super.paint(inlay, g, r, textAttributes)
}
override fun getTextAttributes(editor: Editor): TextAttributes {
return ATTRIBUTES.also { it.foregroundColor = severity.getColor(editor) }
return ATTRIBUTES_SINGLETON.also { it.foregroundColor = severity.getColor(editor) }
}
override fun useEditorFont(): Boolean {
return true
}
private companion object {
private val ATTRIBUTES_SINGLETON = TextAttributes(null, null, null, null, Font.ITALIC)
private fun getValidDescriptionText(text: String?): String {
return if (text.isNullOrBlank()) " " else addMissingPeriod(text)
}
private fun addMissingPeriod(text: String): String {
return if (text.endsWith('.')) text else "$text."
}
private fun fixBaselineForTextRendering(rect: Rectangle) {
rect.y += 1
}
}
}