void () { initText(" ".repeat(100)); Document document = getDocument(getFile()); MarkupModelEx markupModel = (MarkupModelEx)DocumentMarkupModel.forDocument(document, getProject(), true); RangeHighlighter highlighter = markupModel.addRangeHighlighter(10, 11, 1, null, HighlighterTargetArea.EXACT_RANGE); RangeHighlighter highlighter2 = markupModel.addRangeHighlighter(10, 11, 1, null, HighlighterTargetArea.EXACT_RANGE); AtomicInteger fired = new AtomicInteger(); markupModel.addMarkupModelListener(getTestRootDisposable(), new MarkupModelListener() { @Override public void beforeRemoved(@NotNull RangeHighlighterEx highlighter1) { fired.incrementAndGet(); assertSame(highlighter, highlighter1); assertTrue(highlighter2.isValid()); assertThrows(IncorrectOperationException.class, ()->markupModel.addRangeHighlighter(1, 3, 1, null, HighlighterTargetArea.EXACT_RANGE)); assertThrows(IncorrectOperationException.class, ()->markupModel.removeHighlighter(highlighter2)); } }); highlighter.dispose(); assertEquals(1, fired.get()); }