toolId must be consistent with the tool which actually ran it, otherwise there are risks of dangling range highlighters (part of fixes of issues found via IJPL-209403)

GitOrigin-RevId: 0d087e1d25b6da3109428b6fbe430aa15db41b78
This commit is contained in:
Alexey Kudravtsev
2025-09-24 13:19:52 +00:00
committed by intellij-monorepo-bot
parent 6a68e11e20
commit 6332b5af81
2 changed files with 13 additions and 12 deletions
@@ -536,7 +536,7 @@ public final class HighlightInfoUpdaterImpl extends HighlightInfoUpdater impleme
recycler.recycleHighlighter(visitedPsiElement, oldInfo);
}
}
List<? extends HighlightInfo> newInfosToStore = assignRangeHighlighters(newInfos, session, psiFile, hostDocument, invalidElementRecycler, recycler, data);
List<? extends HighlightInfo> newInfosToStore = assignRangeHighlighters(newInfos, toolId, session, psiFile, hostDocument, invalidElementRecycler, recycler, data);
ToolHighlights notNullToolHighlights = toolHighlights == null ? data.computeIfAbsent(toolId, __ -> new ToolHighlights()) : toolHighlights;
if (newInfosToStore.isEmpty()) {
@@ -1201,7 +1201,7 @@ public final class HighlightInfoUpdaterImpl extends HighlightInfoUpdater impleme
* could be reentrant, be careful to avoid leaking/blinking RHs
*/
private static @NotNull @Unmodifiable List<? extends HighlightInfo> assignRangeHighlighters(@NotNull List<? extends HighlightInfo> newInfos,
@NotNull HighlightingSession session,
@NotNull Object toolId, @NotNull HighlightingSession session,
@NotNull PsiFile psiFile,
@NotNull Document hostDocument,
@NotNull ManagedHighlighterRecycler invalidElementRecycler,
@@ -1219,8 +1219,7 @@ public final class HighlightInfoUpdaterImpl extends HighlightInfoUpdater impleme
//noinspection ForLoopReplaceableByForEach
for (int i = 0; i < sorted.size(); i++) {
HighlightInfo newInfo = sorted.get(i);
//todo fails because of ProblemDescriptorWithReporterName
//assert toolId.equals(newInfo.toolId) : newInfo + "; toolId=" + toolId + "(" + toolId.getClass() + ")";
assert toolId.equals(newInfo.toolId) : "HighlightInfo generated by "+toolId + "(" + toolId.getClass() + ") must have consistent toolId, but got:"+ newInfo;
boolean isFileLevel = newInfo.isFileLevelAnnotation();
long finalInfoRange = isFileLevel
? TextRangeScalarUtil.toScalarRange(0, psiFile.getTextLength())
@@ -250,28 +250,29 @@ final class LocalInspectionsPass extends ProgressableTextEditorHighlightingPass
private void createHighlightsForDescriptor(@NotNull ProblemDescriptor descriptor,
@NotNull PsiElement psiElement,
@NotNull LocalInspectionToolWrapper tool,
@NotNull LocalInspectionToolWrapper toolWrapper,
@NotNull Consumer<? super HighlightInfo> infoProcessor) {
String originalShortName = toolWrapper.getShortName();
ApplicationManager.getApplication().assertIsNonDispatchThread();
if (descriptor instanceof ProblemDescriptorWithReporterName name) {
String reportingToolName = name.getReportingToolShortName();
tool = (LocalInspectionToolWrapper)myProfileWrapper.getInspectionTool(reportingToolName, psiElement);
toolWrapper = (LocalInspectionToolWrapper)myProfileWrapper.getInspectionTool(reportingToolName, psiElement);
}
if (myIgnoreSuppressed && tool.getTool().isSuppressedFor(psiElement)) {
registerSuppressedElements(psiElement, tool.getID(), tool.getAlternativeID(), mySuppressedElements);
if (myIgnoreSuppressed && toolWrapper.getTool().isSuppressedFor(psiElement)) {
registerSuppressedElements(psiElement, toolWrapper.getID(), toolWrapper.getAlternativeID(), mySuppressedElements);
return;
}
PsiFile psiFile = psiElement.getContainingFile();
HighlightDisplayKey displayKey = tool.getDisplayKey();
HighlightDisplayKey displayKey = toolWrapper.getDisplayKey();
if (displayKey == null) {
LOG.error("getDisplayKey() is null for " + tool + " (" + tool.getTool() + " ; " + tool.getTool().getClass() + ")");
LOG.error("getDisplayKey() is null for " + toolWrapper + " (" + toolWrapper.getTool() + " ; " + toolWrapper.getTool().getClass() + ")");
return;
}
HighlightSeverity severity = myProfileWrapper.getErrorLevel(displayKey, psiFile).getSeverity();
createHighlightsForDescriptor(emptyActionRegistered, psiFile, tool, severity, descriptor, psiElement, infoProcessor);
createHighlightsForDescriptor(emptyActionRegistered, psiFile, toolWrapper, originalShortName, severity, descriptor, psiElement, infoProcessor);
}
@Override
@@ -282,6 +283,7 @@ final class LocalInspectionsPass extends ProgressableTextEditorHighlightingPass
private void createHighlightsForDescriptor(@NotNull Set<? super Pair<TextRange, String>> emptyActionRegistered,
@NotNull PsiFile psiFile,
@NotNull LocalInspectionToolWrapper toolWrapper,
@NotNull String originalShortName,
@NotNull HighlightSeverity severity,
@NotNull ProblemDescriptor descriptor,
@NotNull PsiElement element,
@@ -335,7 +337,7 @@ final class LocalInspectionsPass extends ProgressableTextEditorHighlightingPass
if (info == null || !UpdateHighlightersUtil.HighlightInfoPostFilters.accept(myProject, info)) {
return;
}
info.setToolId(toolWrapper.getShortName());
info.setToolId(originalShortName); // toolId must be consistent with the tool which actually ran it
info.setGroup(HighlightInfoUpdaterImpl.MANAGED_HIGHLIGHT_INFO_GROUP);
if (isInInjected) {
Document documentRange = documentManager.getDocument(psiFile);