Search in sources :

Example 1 with FileDocumentManagerAdapter

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

the class GeneratedSourceFileChangeTrackerImpl method projectOpened.

@Override
public void projectOpened() {
    final Update check = new Update("check for changes in generated files") {

        @Override
        public void run() {
            checkFiles();
        }
    };
    EditorFactory.getInstance().getEventMulticaster().addDocumentListener(new DocumentAdapter() {

        @Override
        public void documentChanged(DocumentEvent e) {
            VirtualFile file = myDocumentManager.getFile(e.getDocument());
            if (file != null) {
                myFilesToCheck.add(file);
                myCheckingQueue.queue(check);
            }
        }
    }, myProject);
    MessageBusConnection connection = myProject.getMessageBus().connect();
    connection.subscribe(AppTopics.FILE_DOCUMENT_SYNC, new FileDocumentManagerAdapter() {

        @Override
        public void fileContentReloaded(@NotNull VirtualFile file, @NotNull Document document) {
            myFilesToCheck.remove(file);
            if (myEditedGeneratedFiles.remove(file)) {
                myEditorNotifications.updateNotifications(file);
            }
        }
    });
    connection.subscribe(FileEditorManagerListener.FILE_EDITOR_MANAGER, new FileEditorManagerListener() {

        @Override
        public void fileClosed(@NotNull FileEditorManager source, @NotNull VirtualFile file) {
            myEditedGeneratedFiles.remove(file);
        }
    });
    connection.subscribe(ProjectTopics.PROJECT_ROOTS, new ModuleRootListener() {

        @Override
        public void rootsChanged(ModuleRootEvent event) {
            myFilesToCheck.addAll(myEditedGeneratedFiles);
            myEditedGeneratedFiles.clear();
            myCheckingQueue.queue(check);
        }
    });
    myCheckingQueue.activate();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FileEditorManager(com.intellij.openapi.fileEditor.FileEditorManager) MessageBusConnection(com.intellij.util.messages.MessageBusConnection) FileDocumentManagerAdapter(com.intellij.openapi.fileEditor.FileDocumentManagerAdapter) ModuleRootEvent(com.intellij.openapi.roots.ModuleRootEvent) DocumentAdapter(com.intellij.openapi.editor.event.DocumentAdapter) Update(com.intellij.util.ui.update.Update) DocumentEvent(com.intellij.openapi.editor.event.DocumentEvent) Document(com.intellij.openapi.editor.Document) FileEditorManagerListener(com.intellij.openapi.fileEditor.FileEditorManagerListener) ModuleRootListener(com.intellij.openapi.roots.ModuleRootListener)

Example 2 with FileDocumentManagerAdapter

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

the class EncodingUtil method reloadIn.

static void reloadIn(@NotNull final VirtualFile virtualFile, @NotNull final Charset charset) {
    final FileDocumentManager documentManager = FileDocumentManager.getInstance();
    if (documentManager.getCachedDocument(virtualFile) == null) {
        // no need to reload document
        EncodingManager.getInstance().setEncoding(virtualFile, charset);
        return;
    }
    final Disposable disposable = Disposer.newDisposable();
    MessageBusConnection connection = ApplicationManager.getApplication().getMessageBus().connect(disposable);
    connection.subscribe(AppTopics.FILE_DOCUMENT_SYNC, new FileDocumentManagerAdapter() {

        @Override
        public void beforeFileContentReload(VirtualFile file, @NotNull Document document) {
            if (!file.equals(virtualFile))
                return;
            // disconnect
            Disposer.dispose(disposable);
            EncodingManager.getInstance().setEncoding(file, charset);
            LoadTextUtil.setCharsetWasDetectedFromBytes(file, null);
        }
    });
    // if file was modified, the user will be asked here
    try {
        EncodingProjectManagerImpl.suppressReloadDuring(() -> ((VirtualFileListener) documentManager).contentsChanged(new VirtualFileEvent(null, virtualFile, virtualFile.getName(), virtualFile.getParent())));
    } finally {
        Disposer.dispose(disposable);
    }
}
Also used : Disposable(com.intellij.openapi.Disposable) MessageBusConnection(com.intellij.util.messages.MessageBusConnection) FileDocumentManagerAdapter(com.intellij.openapi.fileEditor.FileDocumentManagerAdapter) FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager) Document(com.intellij.openapi.editor.Document)

Example 3 with FileDocumentManagerAdapter

use of com.intellij.openapi.fileEditor.FileDocumentManagerAdapter 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)

Example 4 with FileDocumentManagerAdapter

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

the class DelayedDocumentWatcher method activate.

public void activate() {
    if (myConnection == null) {
        myListenerDisposable = Disposer.newDisposable();
        Disposer.register(myProject, myListenerDisposable);
        EditorFactory.getInstance().getEventMulticaster().addDocumentListener(myListener, myListenerDisposable);
        myConnection = ApplicationManager.getApplication().getMessageBus().connect(myProject);
        myConnection.subscribe(AppTopics.FILE_DOCUMENT_SYNC, new FileDocumentManagerAdapter() {

            @Override
            public void beforeAllDocumentsSaving() {
                myDocumentSavingInProgress = true;
                ApplicationManager.getApplication().invokeLater(() -> myDocumentSavingInProgress = false, ModalityState.any());
            }
        });
    }
}
Also used : FileDocumentManagerAdapter(com.intellij.openapi.fileEditor.FileDocumentManagerAdapter)

Example 5 with FileDocumentManagerAdapter

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

the class LineSeparatorPanel method install.

@Override
public void install(@NotNull StatusBar statusBar) {
    super.install(statusBar);
    MessageBusConnection connection = ApplicationManager.getApplication().getMessageBus().connect(this);
    connection.subscribe(AppTopics.FILE_DOCUMENT_SYNC, new FileDocumentManagerAdapter() {

        @Override
        public void fileContentReloaded(@NotNull VirtualFile file, @NotNull Document document) {
            update();
        }
    });
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) MessageBusConnection(com.intellij.util.messages.MessageBusConnection) 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