Search in sources :

Example 1 with FileDocumentManagerListener

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();
}
Also used : LightVirtualFile(com.intellij.testFramework.LightVirtualFile) MockVirtualFile(com.intellij.mock.MockVirtualFile) FileDocumentManagerAdapter(com.intellij.openapi.fileEditor.FileDocumentManagerAdapter) FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager) PsiFile(com.intellij.psi.PsiFile) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Document(com.intellij.openapi.editor.Document) PsiFile(com.intellij.psi.PsiFile) LightVirtualFile(com.intellij.testFramework.LightVirtualFile) MockVirtualFile(com.intellij.mock.MockVirtualFile) File(java.io.File) NotNull(org.jetbrains.annotations.NotNull) FileDocumentManagerListener(com.intellij.openapi.fileEditor.FileDocumentManagerListener)

Aggregations

MockVirtualFile (com.intellij.mock.MockVirtualFile)1 Document (com.intellij.openapi.editor.Document)1 FileDocumentManager (com.intellij.openapi.fileEditor.FileDocumentManager)1 FileDocumentManagerAdapter (com.intellij.openapi.fileEditor.FileDocumentManagerAdapter)1 FileDocumentManagerListener (com.intellij.openapi.fileEditor.FileDocumentManagerListener)1 PsiFile (com.intellij.psi.PsiFile)1 LightVirtualFile (com.intellij.testFramework.LightVirtualFile)1 IncorrectOperationException (com.intellij.util.IncorrectOperationException)1 File (java.io.File)1 NotNull (org.jetbrains.annotations.NotNull)1