Search in sources :

Example 6 with FileDocumentManagerAdapter

use of com.intellij.openapi.fileEditor.FileDocumentManagerAdapter in project intellij-community by JetBrains.

the class ConsoleHistoryController method install.

public void install() {
    class Listener extends FileDocumentManagerAdapter implements ProjectEx.ProjectSaved {

        @Override
        public void beforeDocumentSaving(@NotNull Document document) {
            if (document == myConsole.getEditorDocument()) {
                saveHistory();
            }
        }

        @Override
        public void saved(@NotNull Project project) {
            saveHistory();
        }
    }
    Listener listener = new Listener();
    ApplicationManager.getApplication().getMessageBus().connect(myConsole).subscribe(ProjectEx.ProjectSaved.TOPIC, listener);
    myConsole.getProject().getMessageBus().connect(myConsole).subscribe(AppTopics.FILE_DOCUMENT_SYNC, listener);
    myConsole.getVirtualFile().putUserData(CONTROLLER_KEY, this);
    Disposer.register(myConsole, new Disposable() {

        @Override
        public void dispose() {
            myConsole.getVirtualFile().putUserData(CONTROLLER_KEY, null);
            saveHistory();
        }
    });
    if (myHelper.getModel().getHistorySize() == 0) {
        loadHistory(myHelper.getId());
    }
    configureActions();
    myLastSaveStamp = getCurrentTimeStamp();
}
Also used : Disposable(com.intellij.openapi.Disposable) Project(com.intellij.openapi.project.Project) FileDocumentManagerAdapter(com.intellij.openapi.fileEditor.FileDocumentManagerAdapter) NotNull(org.jetbrains.annotations.NotNull)

Example 7 with FileDocumentManagerAdapter

use of com.intellij.openapi.fileEditor.FileDocumentManagerAdapter in project intellij-community by JetBrains.

the class FileDocumentManagerImplTest method testReplaceDocumentTextWithTheSameText.

public void testReplaceDocumentTextWithTheSameText() throws Exception {
    final VirtualFile file = createFile();
    final DocumentEx document = (DocumentEx) myDocumentManager.getDocument(file);
    assertNotNull(file.toString(), document);
    WriteCommandAction.runWriteCommandAction(myProject, () -> {
        final String newText = "test text";
        document.replaceString(0, document.getTextLength(), newText);
        assertTrue(myDocumentManager.isDocumentUnsaved(document));
        myDocumentManager.saveDocument(document);
        getProject().getMessageBus().connect(getTestRootDisposable()).subscribe(AppTopics.FILE_DOCUMENT_SYNC, new FileDocumentManagerAdapter() {

            @Override
            public void beforeDocumentSaving(@NotNull Document documentToSave) {
                assertNotSame(document, documentToSave);
            }
        });
        final long modificationStamp = document.getModificationStamp();
        document.replaceString(0, document.getTextLength(), newText);
        if (myDocumentManager.isDocumentUnsaved(document)) {
            assertTrue(document.getModificationStamp() > modificationStamp);
        } else {
            assertEquals(modificationStamp, document.getModificationStamp());
        }
    });
}
Also used : LightVirtualFile(com.intellij.testFramework.LightVirtualFile) MockVirtualFile(com.intellij.mock.MockVirtualFile) DocumentEx(com.intellij.openapi.editor.ex.DocumentEx) FileDocumentManagerAdapter(com.intellij.openapi.fileEditor.FileDocumentManagerAdapter) Document(com.intellij.openapi.editor.Document)

Example 8 with FileDocumentManagerAdapter

use of com.intellij.openapi.fileEditor.FileDocumentManagerAdapter in project intellij-community by JetBrains.

the class FileDocumentManagerImplTest method testSaveDocument_DoNotSaveIfModStampEqualsToFile.

public void testSaveDocument_DoNotSaveIfModStampEqualsToFile() throws Exception {
    final VirtualFile file = createFile();
    final DocumentEx document = (DocumentEx) myDocumentManager.getDocument(file);
    assertNotNull(file.toString(), document);
    WriteCommandAction.runWriteCommandAction(myProject, () -> {
        document.insertString(0, "zzz");
        document.setModificationStamp(file.getModificationStamp());
    });
    getProject().getMessageBus().connect(getTestRootDisposable()).subscribe(AppTopics.FILE_DOCUMENT_SYNC, new FileDocumentManagerAdapter() {

        @Override
        public void beforeDocumentSaving(@NotNull Document documentToSave) {
            assertNotSame(document, documentToSave);
        }
    });
    myDocumentManager.saveDocument(document);
}
Also used : LightVirtualFile(com.intellij.testFramework.LightVirtualFile) MockVirtualFile(com.intellij.mock.MockVirtualFile) DocumentEx(com.intellij.openapi.editor.ex.DocumentEx) FileDocumentManagerAdapter(com.intellij.openapi.fileEditor.FileDocumentManagerAdapter) Document(com.intellij.openapi.editor.Document)

Aggregations

FileDocumentManagerAdapter (com.intellij.openapi.fileEditor.FileDocumentManagerAdapter)8 Document (com.intellij.openapi.editor.Document)6 MockVirtualFile (com.intellij.mock.MockVirtualFile)3 LightVirtualFile (com.intellij.testFramework.LightVirtualFile)3 MessageBusConnection (com.intellij.util.messages.MessageBusConnection)3 Disposable (com.intellij.openapi.Disposable)2 DocumentEx (com.intellij.openapi.editor.ex.DocumentEx)2 FileDocumentManager (com.intellij.openapi.fileEditor.FileDocumentManager)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 NotNull (org.jetbrains.annotations.NotNull)2 DocumentAdapter (com.intellij.openapi.editor.event.DocumentAdapter)1 DocumentEvent (com.intellij.openapi.editor.event.DocumentEvent)1 FileDocumentManagerListener (com.intellij.openapi.fileEditor.FileDocumentManagerListener)1 FileEditorManager (com.intellij.openapi.fileEditor.FileEditorManager)1 FileEditorManagerListener (com.intellij.openapi.fileEditor.FileEditorManagerListener)1 Project (com.intellij.openapi.project.Project)1 ModuleRootEvent (com.intellij.openapi.roots.ModuleRootEvent)1 ModuleRootListener (com.intellij.openapi.roots.ModuleRootListener)1 PsiFile (com.intellij.psi.PsiFile)1 IncorrectOperationException (com.intellij.util.IncorrectOperationException)1