Search in sources :

Example 46 with FileDocumentManager

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

the class ActionsTest method saveDocument.

private static void saveDocument(VirtualFile f) {
    FileDocumentManager dm = FileDocumentManager.getInstance();
    Document document = dm.getDocument(f);
    assertNotNull(f.getPath(), document);
    dm.saveDocument(document);
}
Also used : FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager) Document(com.intellij.openapi.editor.Document)

Example 47 with FileDocumentManager

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

the class FileEncodingTest method testUndoChangeEncoding.

public void testUndoChangeEncoding() throws IOException {
    VirtualFile file = createTempFile("txt", null, "xxx", CharsetToolkit.UTF8_CHARSET);
    file.setCharset(CharsetToolkit.UTF8_CHARSET);
    UIUtil.dispatchAllInvocationEvents();
    assertEquals(CharsetToolkit.UTF8_CHARSET, file.getCharset());
    FileDocumentManager documentManager = FileDocumentManager.getInstance();
    assertNull(documentManager.getCachedDocument(file));
    change(file, WINDOWS_1251);
    assertEquals(WINDOWS_1251, file.getCharset());
    Document document = documentManager.getDocument(file);
    change(file, CharsetToolkit.UTF8_CHARSET);
    assertEquals(CharsetToolkit.UTF8_CHARSET, file.getCharset());
    globalUndo();
    UIUtil.dispatchAllInvocationEvents();
    assertEquals(WINDOWS_1251, file.getCharset());
    document.getText();
}
Also used : FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager) Document(com.intellij.openapi.editor.Document)

Example 48 with FileDocumentManager

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

the class EditorImpl method processKeyTyped.

private boolean processKeyTyped(char c) {
    // [vova] This is patch for Mac OS X. Under Mac "input methods"
    // is handled before our EventQueue consume upcoming KeyEvents.
    IdeEventQueue queue = IdeEventQueue.getInstance();
    if (queue.shouldNotTypeInEditor() || ProgressManager.getInstance().hasModalProgressIndicator()) {
        return false;
    }
    FileDocumentManager manager = FileDocumentManager.getInstance();
    final VirtualFile file = manager.getFile(myDocument);
    if (file != null && !file.isValid()) {
        return false;
    }
    DataContext context = getDataContext();
    Graphics graphics = GraphicsUtil.safelyGetGraphics(myEditorComponent);
    if (graphics != null) {
        // editor component is not showing
        processKeyTypedImmediately(c, graphics, context);
        graphics.dispose();
    }
    ActionManagerEx.getInstanceEx().fireBeforeEditorTyping(c, context);
    EditorUIUtil.hideCursorInEditor(this);
    processKeyTypedNormally(c, context);
    return true;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager)

Example 49 with FileDocumentManager

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

the class TrailingSpacesStripper method strip.

private void strip(@NotNull final Document document) {
    if (!document.isWritable())
        return;
    FileDocumentManager fileDocumentManager = FileDocumentManager.getInstance();
    VirtualFile file = fileDocumentManager.getFile(document);
    if (file == null || !file.isValid() || Boolean.TRUE.equals(DISABLE_FOR_FILE_KEY.get(file)))
        return;
    final EditorSettingsExternalizable settings = EditorSettingsExternalizable.getInstance();
    if (settings == null)
        return;
    final String overrideStripTrailingSpacesData = file.getUserData(OVERRIDE_STRIP_TRAILING_SPACES_KEY);
    final Boolean overrideEnsureNewlineData = file.getUserData(OVERRIDE_ENSURE_NEWLINE_KEY);
    @EditorSettingsExternalizable.StripTrailingSpaces String stripTrailingSpaces = overrideStripTrailingSpacesData != null ? overrideStripTrailingSpacesData : settings.getStripTrailingSpaces();
    final boolean doStrip = !stripTrailingSpaces.equals(EditorSettingsExternalizable.STRIP_TRAILING_SPACES_NONE);
    final boolean ensureEOL = overrideEnsureNewlineData != null ? overrideEnsureNewlineData.booleanValue() : settings.isEnsureNewLineAtEOF();
    if (doStrip) {
        final boolean inChangedLinesOnly = !stripTrailingSpaces.equals(EditorSettingsExternalizable.STRIP_TRAILING_SPACES_WHOLE);
        boolean success = strip(document, inChangedLinesOnly, settings.isKeepTrailingSpacesOnCaretLine());
        if (!success) {
            myDocumentsToStripLater.add(document);
        }
    }
    final int lines = document.getLineCount();
    if (ensureEOL && lines > 0) {
        final int start = document.getLineStartOffset(lines - 1);
        final int end = document.getLineEndOffset(lines - 1);
        if (start != end) {
            final CharSequence content = document.getCharsSequence();
            ApplicationManager.getApplication().runWriteAction(new DocumentRunnable(document, null) {

                @Override
                public void run() {
                    CommandProcessor.getInstance().runUndoTransparentAction(() -> {
                        if (CharArrayUtil.containsOnlyWhiteSpaces(content.subSequence(start, end)) && doStrip) {
                            document.deleteString(start, end);
                        } else {
                            document.insertString(end, "\n");
                        }
                    });
                }
            });
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager) EditorSettingsExternalizable(com.intellij.openapi.editor.ex.EditorSettingsExternalizable)

Example 50 with FileDocumentManager

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

the class HgChangeProvider method processUnsavedChanges.

/**
   * Finds modified but unsaved files in the given list of dirty files and notifies the builder about MODIFIED changes.
   * Changes contained in <code>alreadyProcessed</code> are skipped - they have already been processed as modified, or else.
   */
public void processUnsavedChanges(ChangelistBuilder builder, Set<FilePath> dirtyFiles, Collection<HgChange> alreadyProcessed) {
    // exclude already processed
    for (HgChange c : alreadyProcessed) {
        dirtyFiles.remove(c.beforeFile().toFilePath());
        dirtyFiles.remove(c.afterFile().toFilePath());
    }
    final ProjectLevelVcsManager vcsManager = ProjectLevelVcsManager.getInstance(myProject);
    final FileDocumentManager fileDocumentManager = FileDocumentManager.getInstance();
    for (FilePath filePath : dirtyFiles) {
        final VirtualFile vf = filePath.getVirtualFile();
        if (vf != null && fileDocumentManager.isFileModified(vf)) {
            final VirtualFile root = vcsManager.getVcsRootFor(vf);
            if (root != null && HgUtil.isHgRoot(root)) {
                final HgRevisionNumber beforeRevisionNumber = new HgWorkingCopyRevisionsCommand(myProject).firstParent(root);
                final ContentRevision beforeRevision = (beforeRevisionNumber == null ? null : HgContentRevision.create(myProject, new HgFile(myProject, vf), beforeRevisionNumber));
                builder.processChange(new Change(beforeRevision, CurrentContentRevision.create(filePath), FileStatus.MODIFIED), myVcsKey);
            }
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager) HgWorkingCopyRevisionsCommand(org.zmlx.hg4idea.command.HgWorkingCopyRevisionsCommand)

Aggregations

FileDocumentManager (com.intellij.openapi.fileEditor.FileDocumentManager)56 Document (com.intellij.openapi.editor.Document)39 VirtualFile (com.intellij.openapi.vfs.VirtualFile)32 IOException (java.io.IOException)16 LightVirtualFile (com.intellij.testFramework.LightVirtualFile)7 File (java.io.File)7 Nullable (org.jetbrains.annotations.Nullable)7 Editor (com.intellij.openapi.editor.Editor)6 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 Project (com.intellij.openapi.project.Project)3 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 Result (com.intellij.openapi.application.Result)2