void () { File ioFile = IoTestUtil.createTestFile("test.txt", "<html>some text</html>"); VirtualFile virtualFile = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(ioFile); assertNotNull(ioFile.getPath(), virtualFile); FileDocumentManager documentManager = FileDocumentManager.getInstance(); Document original = documentManager.getDocument(virtualFile); assertNotNull(virtualFile.getPath(), original); final PsiFile file = getPsiFile(original); assertNotNull(file); FileDocumentManagerListener saveListener = new FileDocumentManagerListener() { @Override public void beforeDocumentSaving(@NotNull Document document) { WriteCommandAction.runWriteCommandAction(getProject(), () -> { try { file.getFirstChild().delete(); fail("Must not modify PSI inside save listener"); } catch (IncorrectOperationException e) { assertEquals("Must not modify PSI inside save listener", e.getMessage()); } }); } }; getProject().getMessageBus().connect(getTestRootDisposable()).subscribe(FileDocumentManagerListener.TOPIC, saveListener); final Document document = PsiDocumentManager.getInstance(getProject()).getDocument(file); assertNotNull(document); WriteCommandAction.runWriteCommandAction(getProject(), () -> document.insertString(1, "y")); FileDocumentManager.getInstance().saveAllDocuments(); }