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

Limit description length to 120 characters

Closes 
This commit is contained in:
chylex 2024-12-26 06:27:28 +01:00
parent 816440a150
commit 4c80573375
Signed by: chylex
SSH Key Fingerprint: SHA256:WqM8X/1DDn11LbYM0H5wsqZUjbcKxVsic37L+ERcF4o

View File

@ -28,6 +28,7 @@ class LensRenderer(private var info: HighlightInfo, settings: LensSettingsState)
private val useEditorFont = settings.useEditorFont private val useEditorFont = settings.useEditorFont
private lateinit var inlay: Inlay<*> private lateinit var inlay: Inlay<*>
private lateinit var attributes: LensSeverityTextAttributes private lateinit var attributes: LensSeverityTextAttributes
private var extraRightPadding = 0
private var hovered = false private var hovered = false
init { init {
@ -41,9 +42,11 @@ class LensRenderer(private var info: HighlightInfo, settings: LensSettingsState)
fun setPropertiesFrom(info: HighlightInfo) { fun setPropertiesFrom(info: HighlightInfo) {
this.info = info this.info = info
val description = getValidDescriptionText(info.description)
text = getValidDescriptionText(info.description) text = description
attributes = LensSeverity.from(info.severity).textAttributes attributes = LensSeverity.from(info.severity).textAttributes
extraRightPadding = if (description.lastOrNull() == '.') 2 else 0
} }
override fun paint(inlay: Inlay<*>, g: Graphics, r: Rectangle, textAttributes: TextAttributes) { override fun paint(inlay: Inlay<*>, g: Graphics, r: Rectangle, textAttributes: TextAttributes) {
@ -64,7 +67,7 @@ class LensRenderer(private var info: HighlightInfo, settings: LensSettingsState)
val font = editor.colorsScheme.getFont(EditorFontType.PLAIN) val font = editor.colorsScheme.getFont(EditorFontType.PLAIN)
val x = r.x + TEXT_HORIZONTAL_PADDING val x = r.x + TEXT_HORIZONTAL_PADDING
val y = r.y + editor.ascent + 1 val y = r.y + editor.ascent + 1
val w = inlay.widthInPixels - UNDERLINE_WIDTH_REDUCTION val w = inlay.widthInPixels - UNDERLINE_WIDTH_REDUCTION - extraRightPadding
val h = editor.descent val h = editor.descent
g.color = attributes.foregroundColor g.color = attributes.foregroundColor
@ -113,9 +116,9 @@ class LensRenderer(private var info: HighlightInfo, settings: LensSettingsState)
} }
private fun isHoveringText(point: Point): Boolean { private fun isHoveringText(point: Point): Boolean {
return point.x >= HOVER_PADDING_LEFT return point.x >= HOVER_HORIZONTAL_PADDING
&& point.y >= 4 && point.y >= 4
&& point.x < inlay.widthInPixels - HOVER_PADDING_RIGHT && point.x < inlay.widthInPixels - HOVER_HORIZONTAL_PADDING - extraRightPadding
&& point.y < inlay.heightInPixels - 1 && point.y < inlay.heightInPixels - 1
} }
@ -124,15 +127,10 @@ class LensRenderer(private var info: HighlightInfo, settings: LensSettingsState)
* [HintRenderer.paintHint] renders padding around text, but not around effects. * [HintRenderer.paintHint] renders padding around text, but not around effects.
*/ */
private const val TEXT_HORIZONTAL_PADDING = 7 private const val TEXT_HORIZONTAL_PADDING = 7
private const val HOVER_HORIZONTAL_PADDING = TEXT_HORIZONTAL_PADDING - 2
private const val UNDERLINE_WIDTH_REDUCTION = (TEXT_HORIZONTAL_PADDING * 2) - 1
/** private const val MAX_DESCRIPTION_LENGTH = 120
* The last character is always a period, which does not take up the full width, so the underline and the hover region are shrunk by an additional pixel.
*/
private const val EXTRA_RIGHT_SIDE_PADDING = 1
private const val UNDERLINE_WIDTH_REDUCTION = (TEXT_HORIZONTAL_PADDING * 2) + EXTRA_RIGHT_SIDE_PADDING
private const val HOVER_PADDING_LEFT = TEXT_HORIZONTAL_PADDING - 2
private const val HOVER_PADDING_RIGHT = HOVER_PADDING_LEFT + EXTRA_RIGHT_SIDE_PADDING
/** /**
* Kotlin compiler inspections have an `[UPPERCASE_TAG]` at the beginning. * Kotlin compiler inspections have an `[UPPERCASE_TAG]` at the beginning.
@ -140,7 +138,7 @@ class LensRenderer(private var info: HighlightInfo, settings: LensSettingsState)
private val UPPERCASE_TAG_REGEX = Pattern.compile("^\\[[A-Z_]+] ") private val UPPERCASE_TAG_REGEX = Pattern.compile("^\\[[A-Z_]+] ")
private fun getValidDescriptionText(text: String?): String { private fun getValidDescriptionText(text: String?): String {
return if (text.isNullOrBlank()) " " else addMissingPeriod(unescapeHtmlEntities(stripUppercaseTag(text))) return if (text.isNullOrBlank()) " " else addEllipsisOrMissingPeriod(unescapeHtmlEntities(stripUppercaseTag(text)))
} }
private fun stripUppercaseTag(text: String): String { private fun stripUppercaseTag(text: String): String {
@ -158,8 +156,12 @@ class LensRenderer(private var info: HighlightInfo, settings: LensSettingsState)
return if (text.contains('&')) StringUtil.unescapeXmlEntities(text) else text return if (text.contains('&')) StringUtil.unescapeXmlEntities(text) else text
} }
private fun addMissingPeriod(text: String): String { private fun addEllipsisOrMissingPeriod(text: String): String {
return if (text.endsWith('.')) text else "$text." return when {
text.length > MAX_DESCRIPTION_LENGTH -> text.take(MAX_DESCRIPTION_LENGTH).trimEnd { it.isWhitespace() || it == '.' } + ""
!text.endsWith('.') -> "$text."
else -> text
}
} }
private fun fixBaselineForTextRendering(rect: Rectangle) { private fun fixBaselineForTextRendering(rect: Rectangle) {