Search in sources :

Example 6 with FileDocumentManager

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();
}
Also used : MessageBusConnection(com.intellij.util.messages.MessageBusConnection) ModuleListener(com.intellij.openapi.project.ModuleListener) FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager) FileDocumentManagerImpl(com.intellij.openapi.fileEditor.impl.FileDocumentManagerImpl) Document(com.intellij.openapi.editor.Document) FileTypeManagerImpl(com.intellij.openapi.fileTypes.impl.FileTypeManagerImpl) IncorrectOperationException(com.intellij.util.IncorrectOperationException) IOException(java.io.IOException) Project(com.intellij.openapi.project.Project) CodeStyleSettings(com.intellij.psi.codeStyle.CodeStyleSettings) Module(com.intellij.openapi.module.Module) ProjectManagerEx(com.intellij.openapi.project.ex.ProjectManagerEx) UnindexedFilesUpdater(com.intellij.util.indexing.UnindexedFilesUpdater)

Example 7 with FileDocumentManager

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());
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) LightVirtualFile(com.intellij.testFramework.LightVirtualFile) FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager) Document(com.intellij.openapi.editor.Document)

Example 8 with FileDocumentManager

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() + ")");
}
Also used : LightVirtualFile(com.intellij.testFramework.LightVirtualFile) PsiFileImpl(com.intellij.psi.impl.source.PsiFileImpl) FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager) IncorrectOperationException(com.intellij.util.IncorrectOperationException) IOException(java.io.IOException) Document(com.intellij.openapi.editor.Document)

Example 9 with FileDocumentManager

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);
    }
}
Also used : FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager) MergedWithConflictProjectOrModuleFile(com.intellij.cvsSupport2.cvsoperations.cvsUpdate.MergedWithConflictProjectOrModuleFile) VirtualFile(com.intellij.openapi.vfs.VirtualFile)

Example 10 with FileDocumentManager

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);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager) IOException(java.io.IOException) Editor(com.intellij.openapi.editor.Editor) Document(com.intellij.openapi.editor.Document) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

FileDocumentManager (com.intellij.openapi.fileEditor.FileDocumentManager)63 Document (com.intellij.openapi.editor.Document)42 VirtualFile (com.intellij.openapi.vfs.VirtualFile)32 IOException (java.io.IOException)16 Editor (com.intellij.openapi.editor.Editor)10 Project (com.intellij.openapi.project.Project)9 LightVirtualFile (com.intellij.testFramework.LightVirtualFile)7 File (java.io.File)7 Nullable (org.jetbrains.annotations.Nullable)7 DiscordIntegrationProjectComponent (com.almightyalpaca.jetbrains.plugins.discord.components.DiscordIntegrationProjectComponent)5 MockVirtualFile (com.intellij.mock.MockVirtualFile)5 PsiFile (com.intellij.psi.PsiFile)5 IncorrectOperationException (com.intellij.util.IncorrectOperationException)4 TaskFile (com.jetbrains.edu.learning.courseFormat.TaskFile)4 NotNull (org.jetbrains.annotations.NotNull)4 AnswerPlaceholder (com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder)3 ArrayList (java.util.ArrayList)3 DataContext (com.intellij.openapi.actionSystem.DataContext)2 ActionManagerEx (com.intellij.openapi.actionSystem.ex.ActionManagerEx)2 Application (com.intellij.openapi.application.Application)2