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();
}
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);
}
}
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();
}
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());
}
});
}
}
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();
}
});
}
Aggregations