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

Debug tools

This commit is contained in:
chylex 2024-12-29 17:28:09 +01:00
parent 8936f0e5be
commit 83ba551f82
Signed by: chylex
SSH Key Fingerprint: SHA256:WqM8X/1DDn11LbYM0H5wsqZUjbcKxVsic37L+ERcF4o
7 changed files with 431 additions and 16 deletions
src/main

View File

@ -0,0 +1,274 @@
package com.chylex.intellij.inspectionlens.debug
import com.chylex.intellij.inspectionlens.editor.lens.ColorGenerator
import com.chylex.intellij.inspectionlens.editor.lens.ColorMode
import com.chylex.intellij.inspectionlens.editor.lens.EditorLens
import com.chylex.intellij.inspectionlens.editor.lens.LensSeverity
import com.intellij.codeInsight.daemon.impl.HighlightInfo
import com.intellij.codeInsight.daemon.impl.HighlightInfoType
import com.intellij.grazie.ide.TextProblemSeverities
import com.intellij.lang.annotation.HighlightSeverity
import com.intellij.openapi.Disposable
import com.intellij.openapi.actionSystem.ActionUpdateThread
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.components.service
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.editor.EditorFactory
import com.intellij.openapi.editor.colors.EditorColorsManager
import com.intellij.openapi.editor.colors.EditorColorsScheme
import com.intellij.openapi.editor.event.CaretEvent
import com.intellij.openapi.editor.event.CaretListener
import com.intellij.openapi.editor.event.SelectionEvent
import com.intellij.openapi.editor.event.SelectionListener
import com.intellij.openapi.editor.ex.EditorEx
import com.intellij.openapi.project.Project
import com.intellij.openapi.ui.DialogPanel
import com.intellij.openapi.ui.DialogWrapper
import com.intellij.openapi.util.Disposer
import com.intellij.openapi.util.Key
import com.intellij.spellchecker.SpellCheckerSeveritiesProvider
import com.intellij.ui.ColorUtil
import com.intellij.ui.dsl.builder.Align
import com.intellij.ui.dsl.builder.Cell
import com.intellij.ui.dsl.builder.LabelPosition
import com.intellij.ui.dsl.builder.Row
import com.intellij.ui.dsl.builder.panel
import com.intellij.ui.dsl.builder.selected
import javax.swing.Action
import javax.swing.JSlider
class ShowColorEditorAction : AnAction() {
override fun getActionUpdateThread(): ActionUpdateThread {
return ActionUpdateThread.BGT
}
override fun actionPerformed(e: AnActionEvent) {
Dialog(e.project).show()
}
private class Dialog(project: Project?) : DialogWrapper(project) {
private val editorSyncer = EditorSyncer(disposable)
init {
title = "Inspection Lens - Color Editor"
init()
}
@Suppress("DuplicatedCode")
override fun createCenterPanel(): DialogPanel {
return panel {
row {
for (scheme in EditorColorsManager.getInstance().allSchemes.sortedWith(compareBy({ ColorUtil.isDark(it.defaultBackground) }, { it.displayName }))) {
createEditor(scheme)
}
}.resizableRow()
row {
val generateColors = checkBox("Generate colors")
.selected(ColorGenerator.useGenerator)
.onChanged {
ColorGenerator.useGenerator = it.isSelected
refreshColors()
}
slider(0, 15, 1, 5)
.label("Light theme desaturation", LabelPosition.TOP)
.enabledIf(generateColors.selected)
.bindColorValue(ColorGenerator::lightThemeDesaturation) { ColorGenerator.lightThemeDesaturation = it }
slider(0, 15, 1, 5)
.label("Dark theme desaturation", LabelPosition.TOP)
.enabledIf(generateColors.selected)
.bindColorValue(ColorGenerator::darkThemeDesaturation) { ColorGenerator.darkThemeDesaturation = it }
slider(0, 100, 5, 25)
.label("Light theme threshold", LabelPosition.TOP)
.enabledIf(generateColors.selected)
.bindColorValue(ColorGenerator::lightThemeThreshold) { ColorGenerator.lightThemeThreshold = it }
slider(0, 100, 5, 25)
.label("Dark theme threshold", LabelPosition.TOP)
.enabledIf(generateColors.selected)
.bindColorValue(ColorGenerator::darkThemeThreshold) { ColorGenerator.darkThemeThreshold = it }
slider(0, 25, 1, 5)
.label("Light theme line alpha", LabelPosition.TOP)
.enabledIf(generateColors.selected)
.bindColorValue(ColorGenerator::lightThemeLineAlpha) { ColorGenerator.lightThemeLineAlpha = it }
slider(0, 25, 1, 5)
.label("Dark theme line alpha", LabelPosition.TOP)
.enabledIf(generateColors.selected)
.bindColorValue(ColorGenerator::darkThemeLineAlpha) { ColorGenerator.darkThemeLineAlpha = it }
}
}
}
private fun Row.createEditor(scheme: EditorColorsScheme) {
val editor = createPreviewEditor(scheme, disposable)
editorSyncer.add(editor)
panel {
row {
cell(editor.component)
.label(scheme.displayName, LabelPosition.TOP)
.align(Align.FILL)
.resizableColumn()
}.resizableRow()
}.resizableColumn()
}
private fun Cell<JSlider>.bindColorValue(getter: () -> Int, setter: (Int) -> Unit): Cell<JSlider> {
return applyToComponent {
value = getter()
toolTipText = value.toString()
addChangeListener {
toolTipText = value.toString()
setter(value)
refreshColors()
}
}
}
private fun refreshColors() {
for (severity in LensSeverity.values()) {
severity.refreshColors()
}
for (editor in editorSyncer.editors) {
recreateLenses(editor)
}
}
override fun createActions(): Array<Action> {
return arrayOf(okAction)
}
override fun getDimensionServiceKey(): String {
return this::class.java.name
}
}
private class EditorSyncer(private val disposable: Disposable) {
val editors = mutableListOf<Editor>()
fun add(editor: Editor) {
editors.add(editor)
editor.caretModel.addCaretListener(object : CaretListener {
override fun caretPositionChanged(event: CaretEvent) {
syncCarets(editor)
}
override fun caretAdded(event: CaretEvent) {
syncCarets(editor)
}
override fun caretRemoved(event: CaretEvent) {
syncCarets(editor)
}
}, disposable)
editor.selectionModel.addSelectionListener(object : SelectionListener {
override fun selectionChanged(e: SelectionEvent) {
syncCarets(editor)
}
}, disposable)
}
private var isSyncing = false
private fun syncCarets(mainEditor: Editor) {
val caretsAndSelections = mainEditor.caretModel.caretsAndSelections
syncEditors(mainEditor) {
it.caretModel.caretsAndSelections = caretsAndSelections
}
}
private inline fun syncEditors(mainEditor: Editor, action: (Editor) -> Unit) {
if (isSyncing) {
return
}
isSyncing = true
try {
for (editor in editors) {
if (editor !== mainEditor) {
action(editor)
}
}
} finally {
isSyncing = false
}
}
}
private companion object {
private val SEVERITIES = listOf(
HighlightSeverity.ERROR,
HighlightSeverity.WARNING,
HighlightSeverity.WEAK_WARNING,
HighlightSeverity.GENERIC_SERVER_ERROR_OR_WARNING,
TextProblemSeverities.STYLE_SUGGESTION,
SpellCheckerSeveritiesProvider.TYPO,
HighlightSeverity("Other", 50)
)
private val LENSES_KEY = Key.create<MutableList<EditorLens>>("InspectionLens.ShowColorEditorAction.Lenses")
private fun createPreviewEditor(scheme: EditorColorsScheme, disposable: Disposable): Editor {
val editorFactory = EditorFactory.getInstance()
val document = editorFactory.createDocument(('A'..'Z').take(SEVERITIES.size * 3).joinToString(separator = "\n", postfix = "\n\n") { "$it = 0;" })
val editor = editorFactory.createViewer(document) as EditorEx
editor.colorsScheme = scheme
editor.caretModel.moveToOffset(editor.document.textLength)
with(editor.settings) {
additionalColumnsCount = 0
additionalLinesCount = 0
isIndentGuidesShown = false
isLineMarkerAreaShown = false
isLineNumbersShown = true
isRightMarginShown = false
isWhitespacesShown = true
setGutterIconsShown(false)
}
ColorMode.setForEditor(editor, if (ColorUtil.isDark(scheme.defaultBackground)) ColorMode.ALWAYS_DARK else ColorMode.ALWAYS_LIGHT)
recreateLenses(editor)
Disposer.register(disposable) { editorFactory.releaseEditor(editor) }
return editor
}
private fun recreateLenses(editor: Editor) {
LENSES_KEY.get(editor)?.forEach(EditorLens::hide)
val lenses = mutableListOf<EditorLens>()
LENSES_KEY.set(editor, lenses)
for ((index, severity) in SEVERITIES.withIndex()) {
addLens(editor, severity, index, lenses)
addLens(editor, severity, (index * 2) + 1 + SEVERITIES.size, lenses)
}
}
private fun addLens(editor: Editor, severity: HighlightSeverity, line: Int, list: MutableList<EditorLens>) {
val startOffset = editor.document.getLineStartOffset(line)
val endOffset = editor.document.getLineEndOffset(line)
val highlightInfo = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR)
.severity(severity)
.range(startOffset, endOffset)
.descriptionAndTooltip("Example - ${severity.displayCapitalizedName}")
.createUnconditionally()
EditorLens.show(editor, highlightInfo, service())?.let(list::add)
}
}
}

View File

@ -0,0 +1,80 @@
package com.chylex.intellij.inspectionlens.editor.lens
import com.intellij.ui.ColorUtil
import java.awt.Color
import java.util.Locale
import kotlin.math.abs
import kotlin.math.pow
internal object ColorGenerator {
private const val HACK_BRIGHTNESS_STEP = 1.025F
var useGenerator = false
var lightThemeDesaturation = 1
var lightThemeThreshold = 32
var lightThemeLineAlpha = 10
var darkThemeDesaturation = 2
var darkThemeThreshold = 62
var darkThemeLineAlpha = 13
data class Theme(
val lightThemeTextColor: Color,
val lightThemeLineColor: Color,
val darkThemeTextColor: Color,
val darkThemeLineColor: Color,
)
fun generate(name: String, baseColor: Color): Theme {
val lightThemeTextColor: Color = findColorWithPerceivedBrightness("$name / Light", baseColor, lightThemeDesaturation, 50 downTo -50) { it <= lightThemeThreshold }
val darkThemeTextColor: Color = findColorWithPerceivedBrightness("$name / Dark", baseColor, darkThemeDesaturation, -50..50) { it >= darkThemeThreshold }
val lightThemeLineColor = ColorUtil.toAlpha(lightThemeTextColor, lightThemeLineAlpha)
val darkThemeLineColor = ColorUtil.toAlpha(darkThemeTextColor, darkThemeLineAlpha)
return Theme(lightThemeTextColor, lightThemeLineColor, darkThemeTextColor, darkThemeLineColor)
}
private fun findColorWithPerceivedBrightness(name: String, baseColor: Color, desaturation: Int, steps: IntProgression, testPerceivedBrightness: (Double) -> Boolean): Color {
var finalColor = baseColor
var finalBrightening = 0
var finalPerceivedBrightness = 0.0
for (brightening in steps) {
finalColor = ColorUtil.desaturate(ColorUtil.hackBrightness(baseColor, abs(brightening), if (brightening < 0) 1F / HACK_BRIGHTNESS_STEP else HACK_BRIGHTNESS_STEP), desaturation)
finalBrightening = brightening
finalPerceivedBrightness = getPerceivedBrightness(finalColor)
if (testPerceivedBrightness(finalPerceivedBrightness)) {
break
}
}
println("$name - ${baseColor.red},${baseColor.green},${baseColor.blue} --> step $finalBrightening; brightness ${String.format(Locale.ROOT, "%.2f", finalPerceivedBrightness)}; color ${finalColor.red},${finalColor.green},${finalColor.blue}")
return finalColor
}
private fun getLuminance(r: Double, g: Double, b: Double): Double {
return 0.2126 * srgbToLinearRgb(r) + 0.7152 * srgbToLinearRgb(g) + 0.0722 * srgbToLinearRgb(b)
}
private fun srgbToLinearRgb(channel: Double): Double {
return if (channel <= 0.04045)
channel / 12.92
else
((channel + 0.055) / 1.055).pow(2.4)
}
private fun getPerceivedBrightness(luminance: Double): Double {
return if (luminance <= (216.0 / 24389.0))
luminance * (24389.0 / 27.0)
else
luminance.pow(1.0 / 3.0) * 116 - 16
}
private fun getPerceivedBrightness(color: Color): Double {
val r = color.red / 255.0
val g = color.green / 255.0
val b = color.blue / 255.0
return getPerceivedBrightness(getLuminance(r, g, b))
}
}

View File

@ -0,0 +1,35 @@
package com.chylex.intellij.inspectionlens.editor.lens
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.util.Key
import com.intellij.ui.JBColor
import java.awt.Color
import java.util.EnumMap
internal enum class ColorMode {
AUTO,
ALWAYS_LIGHT,
ALWAYS_DARK;
companion object {
private val KEY = Key.create<ColorMode>("InspectionLens.ColorMode")
fun getFromEditor(editor: Editor): ColorMode {
return KEY.get(editor, AUTO)
}
fun setForEditor(editor: Editor, colorMode: ColorMode) {
KEY.set(editor, colorMode)
}
inline fun createColorAttributes(lightThemeColor: Color, darkThemeColor: Color, attributesFactory: (JBColor) -> LensSeverityTextAttributes): EnumMap<ColorMode, LensSeverityTextAttributes> {
val result = EnumMap<ColorMode, LensSeverityTextAttributes>(ColorMode::class.java)
result[AUTO] = attributesFactory(JBColor(lightThemeColor, darkThemeColor))
result[ALWAYS_LIGHT] = attributesFactory(JBColor(lightThemeColor, lightThemeColor))
result[ALWAYS_DARK] = attributesFactory(JBColor(darkThemeColor, darkThemeColor))
return result
}
}
}

View File

@ -43,7 +43,7 @@ internal value class EditorLensLineBackground(private val highlighter: RangeHigh
return if (editor.foldingModel.let { it.isOffsetCollapsed(startOffset) || it.isOffsetCollapsed(endOffset) })
null
else
severity.lineAttributes
severity.getLineAttributes(ColorMode.getFromEditor(editor))
}
}
}

View File

@ -28,7 +28,7 @@ import javax.swing.SwingUtilities
class LensRenderer(private var info: HighlightInfo, settings: LensSettingsState) : HintRenderer(null), InputHandler {
private val useEditorFont = settings.useEditorFont
private lateinit var inlay: Inlay<*>
private lateinit var attributes: LensSeverityTextAttributes
private lateinit var severity: LensSeverity
private var extraRightPadding = 0
private var hovered = false
@ -46,7 +46,7 @@ class LensRenderer(private var info: HighlightInfo, settings: LensSettingsState)
val description = getValidDescriptionText(info.description)
text = description
attributes = LensSeverity.from(info.severity).textAttributes
severity = LensSeverity.from(info.severity)
extraRightPadding = if (description.lastOrNull() == '.') 2 else 0
}
@ -71,12 +71,12 @@ class LensRenderer(private var info: HighlightInfo, settings: LensSettingsState)
val w = inlay.widthInPixels - UNDERLINE_WIDTH_REDUCTION - extraRightPadding
val h = editor.descent
g.color = attributes.foregroundColor
g.color = getTextAttributes(editor).foregroundColor
EffectPainter.LINE_UNDERSCORE.paint(g as Graphics2D, x, y, w, h, font)
}
override fun getTextAttributes(editor: Editor): TextAttributes {
return attributes
return severity.getTextAttributes(ColorMode.getFromEditor(editor))
}
override fun useEditorFont(): Boolean {

View File

@ -2,19 +2,20 @@ package com.chylex.intellij.inspectionlens.editor.lens
import com.chylex.intellij.inspectionlens.InspectionLens
import com.chylex.intellij.inspectionlens.compatibility.SpellCheckerSupport
import com.chylex.intellij.inspectionlens.editor.lens.ColorMode.Companion.createColorAttributes
import com.intellij.lang.annotation.HighlightSeverity
import com.intellij.ui.ColorUtil
import com.intellij.ui.ColorUtil.toAlpha
import com.intellij.ui.JBColor
import java.awt.Color
import java.awt.Font
import java.util.Collections
import java.util.EnumMap
/**
* Determines properties of inspection lenses based on severity.
*/
@Suppress("UseJBColor", "InspectionUsingGrayColors")
enum class LensSeverity(baseColor: Color, lightThemeDarkening: Int, darkThemeBrightening: Int) {
enum class LensSeverity(private val baseColor: Color, private val lightThemeDarkening: Int, private val darkThemeBrightening: Int) {
ERROR (Color(158, 41, 39), lightThemeDarkening = 2, darkThemeBrightening = 4),
WARNING (Color(190, 145, 23), lightThemeDarkening = 5, darkThemeBrightening = 1),
WEAK_WARNING (Color(117, 109, 86), lightThemeDarkening = 4, darkThemeBrightening = 4),
@ -23,18 +24,37 @@ enum class LensSeverity(baseColor: Color, lightThemeDarkening: Int, darkThemeBri
TYPO (Color( 73, 156, 84), lightThemeDarkening = 4, darkThemeBrightening = 1),
OTHER (Color(128, 128, 128), lightThemeDarkening = 2, darkThemeBrightening = 2);
val textAttributes: LensSeverityTextAttributes
val lineAttributes: LensSeverityTextAttributes
private lateinit var textAttributes: EnumMap<ColorMode, LensSeverityTextAttributes>
private lateinit var lineAttributes: EnumMap<ColorMode, LensSeverityTextAttributes>
init {
val lightThemeColor = ColorUtil.saturate(ColorUtil.darker(baseColor, lightThemeDarkening), 1)
val darkThemeColor = ColorUtil.desaturate(ColorUtil.brighter(baseColor, darkThemeBrightening), 2)
refreshColors()
}
internal fun refreshColors() {
val theme = if (ColorGenerator.useGenerator) {
ColorGenerator.generate(name, baseColor)
}
else {
val lightThemeTextColor = ColorUtil.saturate(ColorUtil.darker(baseColor, lightThemeDarkening), 1)
val darkThemeTextColor = ColorUtil.desaturate(ColorUtil.brighter(baseColor, darkThemeBrightening), 2)
val lightThemeLineColor = toAlpha(lightThemeTextColor, 10)
val darkThemeLineColor = toAlpha(darkThemeTextColor, 13)
ColorGenerator.Theme(lightThemeTextColor, lightThemeLineColor, darkThemeTextColor, darkThemeLineColor)
}
val textColor = JBColor(lightThemeColor, darkThemeColor)
val lineColor = JBColor(toAlpha(lightThemeColor, 10), toAlpha(darkThemeColor, 13))
textAttributes = LensSeverityTextAttributes(foregroundColor = textColor, fontStyle = Font.ITALIC)
lineAttributes = LensSeverityTextAttributes(backgroundColor = lineColor)
textAttributes = createColorAttributes(theme.lightThemeTextColor, theme.darkThemeTextColor) { LensSeverityTextAttributes(foregroundColor = it, fontStyle = Font.ITALIC) }
lineAttributes = createColorAttributes(theme.lightThemeLineColor, theme.darkThemeLineColor) { LensSeverityTextAttributes(backgroundColor = it) }
}
internal fun getTextAttributes(colorMode: ColorMode): LensSeverityTextAttributes {
return textAttributes.getValue(colorMode)
}
internal fun getLineAttributes(colorMode: ColorMode): LensSeverityTextAttributes {
return lineAttributes.getValue(colorMode)
}
companion object {

View File

@ -92,4 +92,10 @@
<projectListeners>
<listener class="com.chylex.intellij.inspectionlens.InspectionLensFileOpenedListener" topic="com.intellij.openapi.fileEditor.FileOpenedSyncListener" />
</projectListeners>
<actions>
<action id="com.chylex.intellij.inspectionlens.debug.ShowColorEditorAction" class="com.chylex.intellij.inspectionlens.debug.ShowColorEditorAction" text="Inspection Lens - Show Color Editor">
<add-to-group group-id="ToolsMenu" anchor="last" />
</action>
</actions>
</idea-plugin>