diff --git a/platform/analysis-impl/src/com/intellij/codeInsight/daemon/impl/UpdateHighlightersUtil.java b/platform/analysis-impl/src/com/intellij/codeInsight/daemon/impl/UpdateHighlightersUtil.java index d12706bba8ed..768a2d81fcbc 100644 --- a/platform/analysis-impl/src/com/intellij/codeInsight/daemon/impl/UpdateHighlightersUtil.java +++ b/platform/analysis-impl/src/com/intellij/codeInsight/daemon/impl/UpdateHighlightersUtil.java @@ -476,4 +476,25 @@ public final class UpdateHighlightersUtil { } } + /** + * Remove all highlighters with exactly the given range from {@link DocumentMarkupModel} produced by given inspection. + * This might be useful in quick fixes and intention actions to provide immediate feedback. + * This method currently works in O(total highlighter count in file) time. + */ + public static void removeHighlightersWithExactRange(@NotNull Document document, @NotNull Project project, @NotNull Segment range, @NotNull String inspectionToolId) { + if (IntentionPreviewUtils.isIntentionPreviewActive()) return; + ThreadingAssertions.assertEventDispatchThread(); + MarkupModel model = DocumentMarkupModel.forDocument(document, project, false); + if (model == null) return; + + for (RangeHighlighter highlighter : model.getAllHighlighters()) { + if (TextRange.areSegmentsEqual(range, highlighter)) { + var highlightInfo = HighlightInfo.fromRangeHighlighter(highlighter); + if (highlightInfo != null && !inspectionToolId.equals(highlightInfo.getInspectionToolId())) { + continue; + } + model.removeHighlighter(highlighter); + } + } + } } diff --git a/spellchecker/src/com/intellij/spellchecker/quickfixes/SaveTo.java b/spellchecker/src/com/intellij/spellchecker/quickfixes/SaveTo.java index 53a448a71004..3b813466f322 100644 --- a/spellchecker/src/com/intellij/spellchecker/quickfixes/SaveTo.java +++ b/spellchecker/src/com/intellij/spellchecker/quickfixes/SaveTo.java @@ -16,6 +16,7 @@ import com.intellij.psi.PsiFile; import com.intellij.spellchecker.DictionaryLayer; import com.intellij.spellchecker.DictionaryLayersProvider; import com.intellij.spellchecker.SpellCheckerManager; +import com.intellij.spellchecker.inspections.SpellCheckingInspection; import com.intellij.spellchecker.util.SpellCheckerBundle; import com.intellij.ui.components.JBList; import com.intellij.util.containers.ContainerUtil; @@ -101,7 +102,7 @@ public final class SaveTo implements SpellCheckerQuickFix, LowPriorityAction { SpellCheckerManager.getInstance(project).acceptWordAsCorrect$intellij_spellchecker(word, file.getViewProvider().getVirtualFile(), project, layer); TextRange range = descriptor.getTextRangeInElement().shiftRight(psi.getTextRange().getStartOffset()); - UpdateHighlightersUtil.removeHighlightersWithExactRange(file.getViewProvider().getDocument(), project, range); + UpdateHighlightersUtil.removeHighlightersWithExactRange(file.getViewProvider().getDocument(), project, range, SpellCheckingInspection.SPELL_CHECKING_INSPECTION_TOOL_NAME); } @Override