void (@NotNull Editor editor, @NotNull HighlightManager highlightManager, @NotNull List<? extends TextRange> rangesToHighlight, @Nullable TextAttributes attributes, @Nullable TextAttributesKey attributesKey) { assert attributes != null || attributesKey != null : "Both attributes and attributesKey are null"; if (editor instanceof EditorWindow) editor = ((EditorWindow)editor).getDelegate(); RangeHighlighter[] highlighters = ((HighlightManagerImpl)highlightManager).getHighlighters(editor); Arrays.sort(highlighters, Comparator.comparingInt(RangeMarker::getStartOffset)); rangesToHighlight.sort(Comparator.comparingInt(TextRange::getStartOffset)); int i = 0; int j = 0; while (i < highlighters.length && j < rangesToHighlight.size()) { RangeHighlighter highlighter = highlighters[i]; TextRange highlighterRange = highlighter.getTextRange(); TextRange refRange = rangesToHighlight.get(j); if (refRange.equals(highlighterRange) && highlighter.getLayer() == HighlighterLayer.SELECTION - 1 && (Objects.equals(attributesKey, highlighter.getTextAttributesKey()) || Objects.equals(attributes, highlighter.getTextAttributes(editor.getColorsScheme())))) { highlightManager.removeSegmentHighlighter(editor, highlighter); i++; } else if (refRange.getStartOffset() > highlighterRange.getEndOffset()) { i++; } else if (refRange.getEndOffset() < highlighterRange.getStartOffset()) { j++; } else { i++; j++; } } }