use of com.intellij.openapi.fileEditor.FileDocumentManager in project intellij-community by JetBrains.
the class LightPlatformTestCase method doSetup.
public static void doSetup(@NotNull LightProjectDescriptor descriptor, @NotNull LocalInspectionTool[] localInspectionTools, @NotNull Disposable parentDisposable) throws Exception {
assertNull("Previous test " + ourTestCase + " hasn't called tearDown(). Probably overridden without super call.", ourTestCase);
IdeaLogger.ourErrorsOccurred = null;
ApplicationManager.getApplication().assertIsDispatchThread();
boolean reusedProject = true;
if (ourProject == null || ourProjectDescriptor == null || !ourProjectDescriptor.equals(descriptor)) {
initProject(descriptor);
reusedProject = false;
}
ProjectManagerEx projectManagerEx = ProjectManagerEx.getInstanceEx();
projectManagerEx.openTestProject(ourProject);
if (reusedProject) {
DumbService.getInstance(ourProject).queueTask(new UnindexedFilesUpdater(ourProject));
}
MessageBusConnection connection = ourProject.getMessageBus().connect(parentDisposable);
connection.subscribe(ProjectTopics.MODULES, new ModuleListener() {
@Override
public void moduleAdded(@NotNull Project project, @NotNull Module module) {
fail("Adding modules is not permitted in LightIdeaTestCase.");
}
});
clearUncommittedDocuments(getProject());
InspectionsKt.configureInspections(localInspectionTools, getProject(), parentDisposable);
assertFalse(getPsiManager().isDisposed());
Boolean passed = null;
try {
passed = StartupManagerEx.getInstanceEx(getProject()).startupActivityPassed();
} catch (Exception ignored) {
}
assertTrue("open: " + getProject().isOpen() + "; disposed:" + getProject().isDisposed() + "; startup passed:" + passed + "; all open projects: " + Arrays.asList(ProjectManager.getInstance().getOpenProjects()), getProject().isInitialized());
CodeStyleSettingsManager.getInstance(getProject()).setTemporarySettings(new CodeStyleSettings());
final FileDocumentManager manager = FileDocumentManager.getInstance();
if (manager instanceof FileDocumentManagerImpl) {
Document[] unsavedDocuments = manager.getUnsavedDocuments();
manager.saveAllDocuments();
ApplicationManager.getApplication().runWriteAction(((FileDocumentManagerImpl) manager)::dropAllUnsavedDocuments);
assertEmpty("There are unsaved documents", Arrays.asList(unsavedDocuments));
}
// startup activities
UIUtil.dispatchAllInvocationEvents();
((FileTypeManagerImpl) FileTypeManager.getInstance()).drainReDetectQueue();
}
use of com.intellij.openapi.fileEditor.FileDocumentManager in project intellij-community by JetBrains.
the class FileManagerImpl method reloadFromDisk.
void reloadFromDisk(@NotNull PsiFile file, boolean ignoreDocument) {
ApplicationManager.getApplication().assertWriteAccessAllowed();
VirtualFile vFile = file.getVirtualFile();
assert vFile != null;
if (file instanceof PsiBinaryFile)
return;
FileDocumentManager fileDocumentManager = myFileDocumentManager;
Document document = fileDocumentManager.getCachedDocument(vFile);
if (document != null && !ignoreDocument) {
fileDocumentManager.reloadFromDisk(document);
} else {
FileViewProvider latestProvider = createFileViewProvider(vFile, false);
PsiFile psi = latestProvider.getPsi(latestProvider.getBaseLanguage());
if (psi instanceof PsiLargeFile || psi instanceof PsiBinaryFile) {
forceReload(vFile);
return;
}
FileViewProvider viewProvider = file.getViewProvider();
if (viewProvider instanceof SingleRootFileViewProvider) {
((SingleRootFileViewProvider) viewProvider).onContentReload();
} else {
LOG.error("Invalid view provider: " + viewProvider + " of " + viewProvider.getClass());
}
}
}
use of com.intellij.openapi.fileEditor.FileDocumentManager in project intellij-community by JetBrains.
the class PsiDirectoryImpl method add.
@Override
public PsiElement add(@NotNull PsiElement element) throws IncorrectOperationException {
checkAdd(element);
if (element instanceof PsiFile) {
PsiFile originalFile = (PsiFile) element;
try {
VirtualFile newVFile;
final PsiDocumentManager psiDocumentManager = PsiDocumentManager.getInstance(myManager.getProject());
if (originalFile instanceof PsiFileImpl) {
newVFile = myFile.createChildData(myManager, originalFile.getName());
String text = originalFile.getText();
final PsiFile psiFile = getManager().findFile(newVFile);
final Document document = psiFile == null ? null : psiDocumentManager.getDocument(psiFile);
final FileDocumentManager fileDocumentManager = FileDocumentManager.getInstance();
if (document != null) {
document.setText(text);
fileDocumentManager.saveDocument(document);
} else {
String lineSeparator = fileDocumentManager.getLineSeparator(newVFile, getProject());
if (!lineSeparator.equals("\n")) {
text = StringUtil.convertLineSeparators(text, lineSeparator);
}
LoadTextUtil.write(getProject(), newVFile, myManager, text, -1);
}
} else {
byte[] storedContents = ((PsiBinaryFileImpl) originalFile).getStoredContents();
if (storedContents != null) {
newVFile = myFile.createChildData(myManager, originalFile.getName());
newVFile.setBinaryContent(storedContents);
} else {
newVFile = VfsUtilCore.copyFile(null, originalFile.getVirtualFile(), myFile);
}
}
psiDocumentManager.commitAllDocuments();
PsiFile newFile = myManager.findFile(newVFile);
if (newFile == null)
throw new IncorrectOperationException("Could not find file " + newVFile);
updateAddedFile(newFile);
return newFile;
} catch (IOException e) {
throw new IncorrectOperationException(e);
}
}
throw new IncorrectOperationException(element + " (" + element.getClass() + ")");
}
use of com.intellij.openapi.fileEditor.FileDocumentManager in project intellij-community by JetBrains.
the class CorruptedProjectFilesDialog method saveExternally.
/*
private class VisualMerge extends AbstractAction {
public VisualMerge() {
putValue(NAME, "Visual Merge");
}
public void actionPerformed(ActionEvent e) {
try {
VirtualFile currentVirtualFile = getCurrentVirtualFile();
final Map<VirtualFile, List<String>> fileToRevisions = new com.intellij.util.containers.HashMap<VirtualFile, List<String>>();
fileToRevisions.put(currentVirtualFile, CvsUtil.getAllRevisionsForFile(currentVirtualFile));
CvsMergeAction internalMergeAction = new CvsMergeAction(currentVirtualFile, myProject, fileToRevisions, new AbstractMergeAction.FileValueHolder());
Document conflictWasResolved = internalMergeAction.showMergeDialogForFile(null);
if (conflictWasResolved != null) {
saveExternally(currentVirtualFile, conflictWasResolved);
onCurrentFileProcessed(false);
}
}
catch (VcsException e1) {
AbstractVcsHelper.getInstance(myProject).showErrors(new ArrayList<VcsException>(Collections.singleton(e1)), "Merge errors");
}
}
}
*/
private void saveExternally(VirtualFile currentVirtualFile, Document document) {
FileDocumentManager fileDocumentManager = FileDocumentManager.getInstance();
fileDocumentManager.saveDocument(fileDocumentManager.getDocument(currentVirtualFile));
File ioProjectFile = VfsUtil.virtualToIoFile(currentVirtualFile);
try {
OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(ioProjectFile));
try {
FileUtil.copy(new ByteArrayInputStream(document.getText().getBytes(currentVirtualFile.getCharset().name())), outputStream);
} finally {
outputStream.close();
}
} catch (IOException e) {
LOG.error(e);
}
}
use of com.intellij.openapi.fileEditor.FileDocumentManager in project intellij-community by JetBrains.
the class ChangeFileEncodingAction method createPopup.
@Nullable
public ListPopup createPopup(@NotNull DataContext dataContext) {
final VirtualFile virtualFile = CommonDataKeys.VIRTUAL_FILE.getData(dataContext);
if (virtualFile == null)
return null;
boolean enabled = checkEnabled(virtualFile);
if (!enabled)
return null;
Editor editor = CommonDataKeys.EDITOR.getData(dataContext);
FileDocumentManager documentManager = FileDocumentManager.getInstance();
final Document document = documentManager.getDocument(virtualFile);
if (!allowDirectories && virtualFile.isDirectory() || document == null && !virtualFile.isDirectory())
return null;
final byte[] bytes;
try {
bytes = virtualFile.isDirectory() ? null : VfsUtilCore.loadBytes(virtualFile);
} catch (IOException e) {
return null;
}
DefaultActionGroup group = createActionGroup(virtualFile, editor, document, bytes, null);
return JBPopupFactory.getInstance().createActionGroupPopup(getTemplatePresentation().getText(), group, dataContext, JBPopupFactory.ActionSelectionAid.SPEEDSEARCH, false);
}
Aggregations