void () { AtomicInteger invocationCount = new AtomicInteger(); myModel.addListener(new FoldingListener() { @Override public void beforeFoldRegionDisposed(@NotNull FoldRegion region) { invocationCount.incrementAndGet(); } }, getTestRootDisposable()); Ref<FoldRegion> regionRef = new Ref<>(); myModel.runBatchFoldingOperation(() -> regionRef.set(myModel.addFoldRegion(1, 2, "."))); assertNotNull(regionRef.get()); assertTrue(regionRef.get().isValid()); assertEquals(0, invocationCount.get()); runWriteCommand(() -> getEditor().getDocument().deleteString(0, 3)); assertFalse(regionRef.get().isValid()); assertEquals(1, invocationCount.get()); }