use of com.intellij.openapi.fileEditor.FileDocumentManager 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.FileDocumentManager in project intellij-community by JetBrains.
the class FileDocumentManagerImplTest method testDocumentUnsavedInsideChangeListener.
public void testDocumentUnsavedInsideChangeListener() throws IOException {
VirtualFile file = createFile("a.txt", "a");
FileDocumentManager manager = FileDocumentManager.getInstance();
Document document = manager.getDocument(file);
assertFalse(manager.isDocumentUnsaved(document));
AtomicInteger invoked = new AtomicInteger();
AtomicBoolean expectUnsaved = new AtomicBoolean(true);
DocumentListener listener = new DocumentListener() {
@Override
public void beforeDocumentChange(DocumentEvent e) {
assertFalse(manager.isDocumentUnsaved(document));
}
@Override
public void documentChanged(DocumentEvent event) {
invoked.incrementAndGet();
assertEquals(expectUnsaved.get(), manager.isDocumentUnsaved(document));
}
};
document.addDocumentListener(listener, getTestRootDisposable());
EditorFactory.getInstance().getEventMulticaster().addDocumentListener(listener, getTestRootDisposable());
WriteCommandAction.runWriteCommandAction(myProject, () -> document.insertString(0, "b"));
assertTrue(manager.isDocumentUnsaved(document));
assertEquals(2, invoked.get());
expectUnsaved.set(false);
FileDocumentManager.getInstance().saveAllDocuments();
FileUtil.writeToFile(VfsUtilCore.virtualToIoFile(file), "something");
file.refresh(false, false);
assertEquals("something", document.getText());
assertFalse(manager.isDocumentUnsaved(document));
assertEquals(4, invoked.get());
}
use of com.intellij.openapi.fileEditor.FileDocumentManager in project intellij-community by JetBrains.
the class FileDocumentManagerImplTest method testFileTypeChangeDocumentDetach.
public void testFileTypeChangeDocumentDetach() throws Exception {
File ioFile = IoTestUtil.createTestFile("test.html", "<html>some text</html>");
VirtualFile file = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(ioFile);
assertNotNull(ioFile.getPath(), file);
FileDocumentManager documentManager = FileDocumentManager.getInstance();
Document original = documentManager.getDocument(file);
assertNotNull(file.getPath(), original);
renameFile(file, "test.png");
Document afterRename = documentManager.getDocument(file);
assertNull(afterRename + " != null", afterRename);
}
use of com.intellij.openapi.fileEditor.FileDocumentManager in project intellij-community by JetBrains.
the class TrailingSpacesStripperTest method createHeavyEditor.
@NotNull
private static Editor createHeavyEditor(@NotNull String name, @NotNull String text) throws IOException {
VirtualFile myVFile = ApplicationManager.getApplication().runWriteAction(new Computable<VirtualFile>() {
@Override
public VirtualFile compute() {
try {
VirtualFile file = getSourceRoot().createChildData(null, name);
VfsUtil.saveText(file, text);
return file;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
});
final FileDocumentManager manager = FileDocumentManager.getInstance();
final Document document = manager.getDocument(myVFile);
manager.reloadFromDisk(document);
Editor editor = createEditor(myVFile);
EditorTestUtil.CaretAndSelectionState caretsState = EditorTestUtil.extractCaretAndSelectionMarkers(document);
EditorTestUtil.setCaretsAndSelection(editor, caretsState);
return editor;
}
use of com.intellij.openapi.fileEditor.FileDocumentManager in project intellij-community by JetBrains.
the class FileDocumentManagerImplTest method checkDocumentFiles.
private static void checkDocumentFiles(List<VirtualFile> files) throws Exception {
FileDocumentManager fdm = FileDocumentManager.getInstance();
List<Future> futures = new ArrayList<>();
for (VirtualFile file : files) {
assertNull(fdm.getCachedDocument(file));
for (int i = 0; i < 30; i++) {
futures.add(ApplicationManager.getApplication().executeOnPooledThread(() -> ReadAction.run(() -> {
Document document = fdm.getDocument(file);
assertEquals(file, fdm.getFile(document));
})));
}
}
for (Future future : futures) {
future.get(20, TimeUnit.SECONDS);
}
}
Aggregations