void () { Disposable disposable = Disposer.newDisposable(); try { AtomicInteger nestingCount = new AtomicInteger(); ApplicationImpl application = (ApplicationImpl)ApplicationManager.getApplication(); application.addApplicationListener(new ApplicationListener() { @Override public void beforeWriteActionStart(@NotNull Object action) { nestingCount.incrementAndGet(); assertTrue(application.isWriteIntentLockAcquired()); assertEquals(nestingCount.get() > 1, application.isWriteAccessAllowed()); assertTrue(application.isWriteActionPending()); assertThrows(IllegalStateException.class,()->application.runWriteAction(() -> {})); } @Override public void writeActionStarted(@NotNull Object action) { assertTrue(application.isWriteIntentLockAcquired()); assertTrue(application.isWriteActionInProgress()); assertTrue(application.isWriteAccessAllowed()); assertFalse(application.isWriteActionPending()); // jury is still out on whether we should allow starting write action from here //assertThrows(IllegalStateException.class,()->application.runWriteAction(() -> {})); } @Override public void writeActionFinished(@NotNull Object action) { assertTrue(application.isWriteIntentLockAcquired()); assertTrue(application.isWriteAccessAllowed()); assertTrue(application.isWriteActionInProgress()); assertFalse(application.isWriteActionPending()); if (nestingCount.get() < 2) { // strange to allow it but this is how reformat works application.runWriteAction(() -> { }); } } @Override public void afterWriteActionFinished(@NotNull Object action) { assertTrue(application.isWriteIntentLockAcquired()); assertFalse(application.isWriteAccessAllowed()); assertFalse(application.isWriteActionInProgress()); assertFalse(application.isWriteActionPending()); if (nestingCount.get() < 2) { // strange to allow it but this is how a lot of code is application.runWriteAction(() -> { }); } } }, disposable); application.runWriteAction(() -> { assertTrue(application.isWriteIntentLockAcquired()); assertTrue(application.isWriteAccessAllowed()); assertFalse(application.isWriteActionPending()); if (nestingCount.get() < 2) { application.runWriteAction(() -> { assertTrue(application.isWriteIntentLockAcquired()); assertTrue(application.isWriteAccessAllowed()); assertFalse(application.isWriteActionPending()); }); } }); } finally { Disposer.dispose(disposable); } }