use of com.intellij.openapi.fileEditor.FileDocumentManagerListener in project intellij-community by JetBrains.
the class FileDocumentManagerImplTest method testNoPSIModificationsDuringSave.
public void testNoPSIModificationsDuringSave() throws IOException {
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 FileDocumentManagerAdapter() {
@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(AppTopics.FILE_DOCUMENT_SYNC, saveListener);
final Document document = PsiDocumentManager.getInstance(getProject()).getDocument(file);
assertNotNull(document);
WriteCommandAction.runWriteCommandAction(getProject(), () -> document.insertString(1, "y"));
FileDocumentManager.getInstance().saveAllDocuments();
}
Aggregations