From 5258e9628264dbb1e3339aee69aade89420f19b8 Mon Sep 17 00:00:00 2001 From: Max Medvedev Date: Mon, 20 Oct 2025 16:36:35 +0200 Subject: [PATCH] [daemon] IJPL-206789 extract ErrorCountStorage to a separate file GitOrigin-RevId: d5d089bb915044f88895e9f99ff26db711c5862f --- .../daemon/impl/ErrorCountStorage.kt | 44 +++++++++++++++++++ .../daemon/impl/TrafficLightRenderer.kt | 24 +++------- 2 files changed, 50 insertions(+), 18 deletions(-) create mode 100644 platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/ErrorCountStorage.kt diff --git a/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/ErrorCountStorage.kt b/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/ErrorCountStorage.kt new file mode 100644 index 000000000000..ce8bcb68d721 --- /dev/null +++ b/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/ErrorCountStorage.kt @@ -0,0 +1,44 @@ +// Copyright 2000-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. +package com.intellij.codeInsight.daemon.impl + +import com.intellij.codeInsight.multiverse.CodeInsightContext +import com.intellij.lang.annotation.HighlightSeverity +import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap +import org.jetbrains.annotations.ApiStatus +import kotlin.math.max + +/** + * Synchronized storage for error counts. + * Stores a separate number for each severity and context combination. + */ +@ApiStatus.Internal +class ErrorCountStorage { + private val errorCount = Object2IntOpenHashMap() // guarded by errorCount + + fun getErrorCount(severity: HighlightSeverity, context: CodeInsightContext): Int { + val contextHighlightKey = HighlightKey(severity, context) + return synchronized(errorCount) { + errorCount.getInt(contextHighlightKey) + } + } + + fun incErrorCount(infoSeverity: HighlightSeverity, context: CodeInsightContext, delta: Int) { + val highlightKey = HighlightKey(infoSeverity, context) + synchronized(errorCount) { + val oldVal = errorCount.getInt(highlightKey) + val newVal = max(0, oldVal + delta) + errorCount.put(highlightKey, newVal) + } + } + + fun clear() { + synchronized(errorCount) { + errorCount.clear() + } + } + + private data class HighlightKey( + val severity: HighlightSeverity, + val context: CodeInsightContext?, + ) +} \ No newline at end of file diff --git a/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/TrafficLightRenderer.kt b/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/TrafficLightRenderer.kt index a9317132af4d..66c7737e4b77 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/TrafficLightRenderer.kt +++ b/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/TrafficLightRenderer.kt @@ -49,11 +49,10 @@ import com.intellij.util.io.storage.HeavyProcessLatch import com.intellij.util.ui.EdtInvocationManager import com.intellij.util.ui.GridBag import it.unimi.dsi.fastutil.ints.IntArrayList -import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap import org.jetbrains.annotations.ApiStatus import org.jetbrains.annotations.Nls import java.awt.Container -import java.util.Collections +import java.util.* import java.util.concurrent.CancellationException open class TrafficLightRenderer private constructor( @@ -64,7 +63,7 @@ open class TrafficLightRenderer private constructor( ) : ErrorStripeRenderer, Disposable { private val daemonCodeAnalyzer: DaemonCodeAnalyzerImpl private val severityRegistrar: SeverityRegistrar - private val errorCount = Object2IntOpenHashMap() // guarded by errorCount + private val errorCount = ErrorCountStorage() @JvmField @ApiStatus.Internal protected val uiController: UIController @@ -167,8 +166,7 @@ open class TrafficLightRenderer private constructor( val context = getContext() for (severity in severities) { val severityIndex = severityRegistrar.getSeverityIdx(severity) - val highlightKey = HighlightKey(severity, context) - cachedErrors[severityIndex] = synchronized(errorCount) { errorCount.getInt(highlightKey) } + cachedErrors[severityIndex] = errorCount.getErrorCount(severity, context) } return cachedErrors } @@ -177,20 +175,15 @@ open class TrafficLightRenderer private constructor( } override fun dispose() { - synchronized(errorCount) { - errorCount.clear() - } + errorCount.clear() } private fun incErrorCount(highlighter: RangeHighlighter, delta: Int) { val info = HighlightInfo.fromRangeHighlighter(highlighter) ?: return val infoSeverity = info.severity if (infoSeverity > HighlightSeverity.TEXT_ATTRIBUTES) { - val highlightKey = HighlightKey(infoSeverity, getContext(highlighter)) - synchronized(errorCount) { - val oldVal = errorCount.getInt(highlightKey) - errorCount.put(highlightKey, Math.max(0, oldVal + delta)) - } + val context = getContext(highlighter) + errorCount.incErrorCount(infoSeverity, context, delta) } } @@ -675,8 +668,3 @@ open class TrafficLightRenderer private constructor( } } } - -private data class HighlightKey( - val severity: HighlightSeverity, - val context: CodeInsightContext, -) \ No newline at end of file