1
0
mirror of https://github.com/chylex/IntelliJ-Inspection-Lens.git synced 2024-11-24 22:42:52 +01:00

Compare commits

...

2 Commits

Author SHA1 Message Date
fd860fd390
WIP 2024-06-14 08:42:44 +02:00
a1fab197b2
WIP 2024-06-14 08:24:54 +02:00
7 changed files with 168 additions and 13 deletions

View File

@ -1,7 +1,7 @@
package com.chylex.intellij.inspectionlens.editor
import com.chylex.intellij.inspectionlens.settings.LensSettings
import com.intellij.codeInsight.daemon.impl.HighlightInfo
import com.intellij.lang.annotation.HighlightSeverity
import com.intellij.openapi.Disposable
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.editor.ex.MarkupModelEx
@ -17,6 +17,7 @@ import com.intellij.openapi.util.Key
*/
internal class LensMarkupModelListener private constructor(editor: Editor) : MarkupModelListener {
private val lensManagerDispatcher = EditorLensManagerDispatcher(EditorLensManager.getOrCreate(editor))
private val severityFilter = LensSettings.createSeverityFilter()
override fun afterAdded(highlighter: RangeHighlighterEx) {
showIfValid(highlighter)
@ -48,20 +49,23 @@ internal class LensMarkupModelListener private constructor(editor: Editor) : Mar
highlighters.forEach(::showIfValid)
}
private fun getFilteredHighlightInfo(highlighter: RangeHighlighter): HighlightInfo? {
return HighlightInfo.fromRangeHighlighter(highlighter)?.takeIf { severityFilter.test(it.severity) }
}
private inline fun runWithHighlighterIfValid(highlighter: RangeHighlighter, actionForImmediate: (HighlighterWithInfo) -> Unit, actionForAsync: (HighlighterWithInfo.Async) -> Unit) {
val info = getHighlightInfoIfValid(highlighter)
if (info != null) {
processHighlighterWithInfo(HighlighterWithInfo.from(highlighter, info), actionForImmediate, actionForAsync)
}
}
private fun getHighlightInfoIfValid(highlighter: RangeHighlighter): HighlightInfo? {
return highlighter.takeIf { it.isValid }?.let(::getFilteredHighlightInfo)
}
companion object {
private val EDITOR_KEY = Key<LensMarkupModelListener>(LensMarkupModelListener::class.java.name)
private val MINIMUM_SEVERITY = HighlightSeverity.TEXT_ATTRIBUTES.myVal + 1
private fun getFilteredHighlightInfo(highlighter: RangeHighlighter): HighlightInfo? {
return HighlightInfo.fromRangeHighlighter(highlighter)?.takeIf { it.severity.myVal >= MINIMUM_SEVERITY }
}
private inline fun runWithHighlighterIfValid(highlighter: RangeHighlighter, actionForImmediate: (HighlighterWithInfo) -> Unit, actionForAsync: (HighlighterWithInfo.Async) -> Unit) {
val info = highlighter.takeIf { it.isValid }?.let(::getFilteredHighlightInfo)
if (info != null) {
processHighlighterWithInfo(HighlighterWithInfo.from(highlighter, info), actionForImmediate, actionForAsync)
}
}
private inline fun processHighlighterWithInfo(highlighterWithInfo: HighlighterWithInfo, actionForImmediate: (HighlighterWithInfo) -> Unit, actionForAsync: (HighlighterWithInfo.Async) -> Unit) {
if (highlighterWithInfo is HighlighterWithInfo.Async) {

View File

@ -0,0 +1,18 @@
package com.chylex.intellij.inspectionlens.editor
import com.intellij.lang.annotation.HighlightSeverity
import java.util.function.Predicate
class LensSeverityFilter(private val disabledSeverityIds: Set<String>) : Predicate<HighlightSeverity> {
override fun test(severity: HighlightSeverity): Boolean {
return Global.test(severity) && severity.name !in disabledSeverityIds
}
object Global {
private val MINIMUM_SEVERITY = HighlightSeverity.TEXT_ATTRIBUTES.myVal + 1
fun test(severity: HighlightSeverity): Boolean {
return severity.myVal >= MINIMUM_SEVERITY
}
}
}

View File

@ -0,0 +1,67 @@
package com.chylex.intellij.inspectionlens.settings
import com.chylex.intellij.inspectionlens.editor.LensSeverityFilter
import com.intellij.codeInsight.daemon.impl.SeverityRegistrar
import com.intellij.openapi.components.service
import com.intellij.openapi.editor.markup.TextAttributes
import com.intellij.openapi.options.BoundConfigurable
import com.intellij.openapi.options.ConfigurableWithId
import com.intellij.openapi.ui.DialogPanel
import com.intellij.ui.EditorTextFieldCellRenderer.SimpleRendererComponent
import com.intellij.ui.components.JBCheckBox
import com.intellij.ui.dsl.builder.Cell
import com.intellij.ui.dsl.builder.RightGap
import com.intellij.ui.dsl.builder.RowLayout
import com.intellij.ui.dsl.builder.bindSelected
import com.intellij.ui.dsl.builder.panel
class LensApplicationConfigurable : BoundConfigurable("Inspection Lens"), ConfigurableWithId {
companion object {
const val ID = "InspectionLens"
}
override fun getId(): String {
return ID
}
override fun createPanel(): DialogPanel {
val settings = service<LensSettingsState>().state
return panel {
group("Enabled Severities") {
val registrar = SeverityRegistrar.getSeverityRegistrar(null)
val knownSeverities = registrar.allSeverities
.filter(LensSeverityFilter.Global::test)
.map { StoredSeverity(it) to registrar.getHighlightInfoTypeBySeverity(it).attributesKey.defaultAttributes }
val knownSeverityIds = knownSeverities
.mapTo(HashSet()) { it.first.id }
val unknownSeverities = settings.disabledSeverities
.filterNot { it.id in knownSeverityIds }
.map { it to null as TextAttributes? }
val allSeverities = (knownSeverities + unknownSeverities)
.sortedByDescending { it.first.priority }
for ((severity, textAttributes) in allSeverities) {
row {
val checkBox = checkBox(severity.name)
checkBox.bindSelectedToNotIn(settings.disabledSeverities, severity)
checkBox.gap(RightGap.COLUMNS)
val exampleLabel = SimpleRendererComponent(null, null, true)
exampleLabel.setText("Example", textAttributes, false)
exampleLabel.editor.contentComponent.setOpaque(false)
cell(exampleLabel)
}.layout(RowLayout.PARENT_GRID)
}
}
}
}
private fun <T> Cell<JBCheckBox>.bindSelectedToNotIn(set: MutableCollection<T>, value: T): Cell<JBCheckBox> {
return bindSelected({ value !in set }, { if (it) set.remove(value) else set.add(value) })
}
}

View File

@ -0,0 +1,13 @@
package com.chylex.intellij.inspectionlens.settings
import com.chylex.intellij.inspectionlens.editor.LensSeverityFilter
import com.intellij.openapi.components.service
object LensSettings {
private val state
get() = service<LensSettingsState>().state
fun createSeverityFilter(): LensSeverityFilter {
return LensSeverityFilter(state.disabledSeveritiesReadOnly.mapTo(HashSet(), StoredSeverity::id))
}
}

View File

@ -0,0 +1,23 @@
package com.chylex.intellij.inspectionlens.settings
import com.intellij.openapi.components.BaseState
import com.intellij.openapi.components.SettingsCategory
import com.intellij.openapi.components.SimplePersistentStateComponent
import com.intellij.openapi.components.State
import com.intellij.openapi.components.Storage
import com.intellij.util.xmlb.annotations.XCollection
@State(
name = LensApplicationConfigurable.ID,
storages = [ Storage("chylex.inspectionLens.xml") ],
category = SettingsCategory.UI
)
class LensSettingsState : SimplePersistentStateComponent<LensSettingsState.State>(State()) {
class State : BaseState() {
@get:XCollection
val disabledSeverities by list<StoredSeverity>()
val disabledSeveritiesReadOnly: List<StoredSeverity>
get() = disabledSeverities
}
}

View File

@ -0,0 +1,22 @@
package com.chylex.intellij.inspectionlens.settings
import com.intellij.configurationStore.Property
import com.intellij.lang.annotation.HighlightSeverity
import com.intellij.util.xmlb.annotations.Tag
@Tag("severity")
data class StoredSeverity(
@Property var id: String = "",
@Property var name: String = "",
@Property var priority: Int = 0,
) {
constructor(severity: HighlightSeverity) : this(id = severity.name, name = severity.displayCapitalizedName, priority = severity.myVal)
override fun equals(other: Any?): Boolean {
return other is StoredSeverity && id == other.id
}
override fun hashCode(): Int {
return id.hashCode()
}
}

View File

@ -63,6 +63,14 @@
<depends>com.intellij.modules.platform</depends>
<depends optional="true" config-file="compatibility/InspectionLens-Grazie.xml">tanvd.grazi</depends>
<extensions defaultExtensionNs="com.intellij">
<applicationService serviceImplementation="com.chylex.intellij.inspectionlens.settings.LensSettingsState" />
<applicationConfigurable id="com.chylex.intellij.inspectionlens.settings.LensApplicationConfigurable"
instance="com.chylex.intellij.inspectionlens.settings.LensApplicationConfigurable"
displayName="Inspection Lens"
parentId="tools" />
</extensions>
<applicationListeners>
<listener class="com.chylex.intellij.inspectionlens.InspectionLensPluginListener" topic="com.intellij.ide.plugins.DynamicPluginListener" />
</applicationListeners>