mirror of
https://github.com/chylex/IntelliJ-Inspection-Lens.git
synced 2025-06-04 02:34:03 +02:00
Refactor to organize responsibilities and clarify code
This commit is contained in:
parent
edef5787b4
commit
4d46af3224
src/main/kotlin/com/chylex/intellij/inspectionlens
@ -26,13 +26,9 @@ class EditorInlayLensManager private constructor(private val editor: Editor) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun updateRenderer(renderer: LensRenderer, info: HighlightInfo) {
|
private fun getInlayHintOffset(info: HighlightInfo): Int {
|
||||||
renderer.text = info.description.takeIf(String::isNotBlank)?.let(::addMissingPeriod) ?: " "
|
// Ensures a highlight at the end of a line does not overflow to the next line.
|
||||||
renderer.severity = LensSeverity.from(info.severity)
|
return info.actualEndOffset - 1
|
||||||
}
|
|
||||||
|
|
||||||
private fun addMissingPeriod(text: String): String {
|
|
||||||
return if (text.endsWith('.')) text else "$text."
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -41,12 +37,12 @@ class EditorInlayLensManager private constructor(private val editor: Editor) {
|
|||||||
fun show(highlighter: RangeHighlighter, info: HighlightInfo) {
|
fun show(highlighter: RangeHighlighter, info: HighlightInfo) {
|
||||||
val currentInlay = inlays[highlighter]
|
val currentInlay = inlays[highlighter]
|
||||||
if (currentInlay != null && currentInlay.isValid) {
|
if (currentInlay != null && currentInlay.isValid) {
|
||||||
updateRenderer(currentInlay.renderer, info)
|
currentInlay.renderer.setPropertiesFrom(info)
|
||||||
currentInlay.update()
|
currentInlay.update()
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
val offset = info.actualEndOffset - 1
|
val offset = getInlayHintOffset(info)
|
||||||
val renderer = LensRenderer().also { updateRenderer(it, info) }
|
val renderer = LensRenderer(info)
|
||||||
val properties = InlayProperties().relatesToPrecedingText(true).priority(-offset)
|
val properties = InlayProperties().relatesToPrecedingText(true).priority(-offset)
|
||||||
|
|
||||||
editor.inlayModel.addAfterLineEndElement(offset, properties, renderer)?.let {
|
editor.inlayModel.addAfterLineEndElement(offset, properties, renderer)?.let {
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package com.chylex.intellij.inspectionlens
|
package com.chylex.intellij.inspectionlens
|
||||||
|
|
||||||
|
import com.intellij.codeInsight.daemon.impl.HighlightInfo
|
||||||
import com.intellij.codeInsight.daemon.impl.HintRenderer
|
import com.intellij.codeInsight.daemon.impl.HintRenderer
|
||||||
import com.intellij.openapi.editor.Editor
|
import com.intellij.openapi.editor.Editor
|
||||||
import com.intellij.openapi.editor.Inlay
|
import com.intellij.openapi.editor.Inlay
|
||||||
@ -11,23 +12,44 @@ import java.awt.Rectangle
|
|||||||
/**
|
/**
|
||||||
* Renders the text of an inspection lens.
|
* Renders the text of an inspection lens.
|
||||||
*/
|
*/
|
||||||
class LensRenderer : HintRenderer(null) {
|
class LensRenderer(info: HighlightInfo) : HintRenderer(null) {
|
||||||
private companion object {
|
private lateinit var severity: LensSeverity
|
||||||
private val ATTRIBUTES = TextAttributes(null, null, null, null, Font.ITALIC)
|
|
||||||
|
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) {
|
override fun paint(inlay: Inlay<*>, g: Graphics, r: Rectangle, textAttributes: TextAttributes) {
|
||||||
r.y += 1
|
fixBaselineForTextRendering(r)
|
||||||
super.paint(inlay, g, r, textAttributes)
|
super.paint(inlay, g, r, textAttributes)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getTextAttributes(editor: Editor): 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 {
|
override fun useEditorFont(): Boolean {
|
||||||
return true
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user