1
0
mirror of https://github.com/chylex/IntelliJ-Inspection-Lens.git synced 2025-01-29 19:45:59 +01:00

Compare commits

...

2 Commits

Author SHA1 Message Date
4c80573375
Limit description length to 120 characters
Closes #18
2024-12-26 06:27:28 +01:00
816440a150
Strip uppercase tags from the beginning of inspection description
Meant for Kotlin compiler inspections, but could potentially affect others.
2024-12-26 01:54:16 +01:00

View File

@ -18,6 +18,7 @@ import java.awt.Graphics2D
import java.awt.Point
import java.awt.Rectangle
import java.awt.event.MouseEvent
import java.util.regex.Pattern
import javax.swing.SwingUtilities
/**
@ -27,6 +28,7 @@ class LensRenderer(private var info: HighlightInfo, settings: LensSettingsState)
private val useEditorFont = settings.useEditorFont
private lateinit var inlay: Inlay<*>
private lateinit var attributes: LensSeverityTextAttributes
private var extraRightPadding = 0
private var hovered = false
init {
@ -40,9 +42,11 @@ class LensRenderer(private var info: HighlightInfo, settings: LensSettingsState)
fun setPropertiesFrom(info: HighlightInfo) {
this.info = info
val description = getValidDescriptionText(info.description)
text = getValidDescriptionText(info.description)
text = description
attributes = LensSeverity.from(info.severity).textAttributes
extraRightPadding = if (description.lastOrNull() == '.') 2 else 0
}
override fun paint(inlay: Inlay<*>, g: Graphics, r: Rectangle, textAttributes: TextAttributes) {
@ -63,7 +67,7 @@ class LensRenderer(private var info: HighlightInfo, settings: LensSettingsState)
val font = editor.colorsScheme.getFont(EditorFontType.PLAIN)
val x = r.x + TEXT_HORIZONTAL_PADDING
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
g.color = attributes.foregroundColor
@ -112,9 +116,9 @@ class LensRenderer(private var info: HighlightInfo, settings: LensSettingsState)
}
private fun isHoveringText(point: Point): Boolean {
return point.x >= HOVER_PADDING_LEFT
return point.x >= HOVER_HORIZONTAL_PADDING
&& point.y >= 4
&& point.x < inlay.widthInPixels - HOVER_PADDING_RIGHT
&& point.x < inlay.widthInPixels - HOVER_HORIZONTAL_PADDING - extraRightPadding
&& point.y < inlay.heightInPixels - 1
}
@ -123,30 +127,41 @@ class LensRenderer(private var info: HighlightInfo, settings: LensSettingsState)
* [HintRenderer.paintHint] renders padding around text, but not around effects.
*/
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.
* Kotlin compiler inspections have an `[UPPERCASE_TAG]` at the beginning.
*/
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
private val UPPERCASE_TAG_REGEX = Pattern.compile("^\\[[A-Z_]+] ")
private fun getValidDescriptionText(text: String?): String {
return if (text.isNullOrBlank()) " " else addMissingPeriod(unescapeHtmlEntities(text))
return if (text.isNullOrBlank()) " " else addEllipsisOrMissingPeriod(unescapeHtmlEntities(stripUppercaseTag(text)))
}
private fun unescapeHtmlEntities(potentialHtml: String): String {
return potentialHtml.ifContains('&', StringUtil::unescapeXmlEntities)
private fun stripUppercaseTag(text: String): String {
if (text.startsWith('[')) {
val matcher = UPPERCASE_TAG_REGEX.matcher(text)
if (matcher.find()) {
return text.substring(matcher.end())
}
}
return text
}
private fun addMissingPeriod(text: String): String {
return if (text.endsWith('.')) text else "$text."
private fun unescapeHtmlEntities(text: String): String {
return if (text.contains('&')) StringUtil.unescapeXmlEntities(text) else text
}
private inline fun String.ifContains(charToTest: Char, action: (String) -> String): String {
return if (this.contains(charToTest)) action(this) else this
private fun addEllipsisOrMissingPeriod(text: String): String {
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) {