Search in sources :

Example 11 with FileDocumentManager

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

the class EncodingProjectManagerImpl method reload.

private static void reload(@NotNull final VirtualFile virtualFile) {
    ApplicationManager.getApplication().runWriteAction(() -> {
        FileDocumentManager documentManager = FileDocumentManager.getInstance();
        ((VirtualFileListener) documentManager).contentsChanged(new VirtualFileEvent(null, virtualFile, virtualFile.getName(), virtualFile.getParent()));
    });
}
Also used : FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager)

Example 12 with FileDocumentManager

use of com.intellij.openapi.fileEditor.FileDocumentManager 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);
    }
}
Also used : Disposable(com.intellij.openapi.Disposable) MessageBusConnection(com.intellij.util.messages.MessageBusConnection) FileDocumentManagerAdapter(com.intellij.openapi.fileEditor.FileDocumentManagerAdapter) FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager) Document(com.intellij.openapi.editor.Document)

Example 13 with FileDocumentManager

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

the class EncodingUtil method checkCanReload.

@NotNull
public static // returns pair (existing charset (null means N/A); failReason: null means enabled, notnull means disabled and contains error message)
Pair<Charset, String> checkCanReload(@NotNull VirtualFile virtualFile) {
    if (virtualFile.isDirectory()) {
        return Pair.create(null, REASON_FILE_IS_A_DIRECTORY);
    }
    FileDocumentManager documentManager = FileDocumentManager.getInstance();
    Document document = documentManager.getDocument(virtualFile);
    if (document == null)
        return Pair.create(null, REASON_BINARY_FILE);
    Charset charsetFromContent = ((EncodingManagerImpl) EncodingManager.getInstance()).computeCharsetFromContent(virtualFile);
    Charset existing = virtualFile.getCharset();
    String autoDetectedFrom = LoadTextUtil.wasCharsetDetectedFromBytes(virtualFile);
    String failReason;
    if (autoDetectedFrom != null) {
        // no point changing encoding if it was auto-detected
        failReason = "the encoding was " + autoDetectedFrom;
    } else if (charsetFromContent != null) {
        failReason = REASON_HARDCODED_IN_TEXT;
        existing = charsetFromContent;
    } else {
        failReason = fileTypeDescriptionError(virtualFile);
    }
    return Pair.create(existing, failReason);
}
Also used : Charset(java.nio.charset.Charset) FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager) Document(com.intellij.openapi.editor.Document) NotNull(org.jetbrains.annotations.NotNull)

Example 14 with FileDocumentManager

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

the class KillToWordEndActionTest method testSubsequentKillsInterruptedBySave.

public void testSubsequentKillsInterruptedBySave() throws Exception {
    String text = "public class ParentCopy {\n" + "        public Insets getBorderInsets(<caret>Component c) {\n" + "        }\n" + "    }";
    configureFromFileText(getTestName(false) + ".java", text);
    cutToLineEnd();
    cutToLineEnd();
    final FileDocumentManager manager = FileDocumentManager.getInstance();
    manager.saveAllDocuments();
    cutToLineEnd();
    cutToLineEnd();
    checkResultByText("public class ParentCopy {\n" + "        public Insets getBorderInsets(    }");
    Object string = getContents();
    assertEquals("Component c) {\n        }\n", string);
}
Also used : FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager)

Example 15 with FileDocumentManager

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

the class FileDocumentManagerImplTest method testFileTypeModificationDocumentPreservation.

public void testFileTypeModificationDocumentPreservation() 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.wtf");
    Document afterRename = documentManager.getDocument(file);
    assertTrue(afterRename + " != " + original, afterRename == original);
}
Also used : LightVirtualFile(com.intellij.testFramework.LightVirtualFile) MockVirtualFile(com.intellij.mock.MockVirtualFile) FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager) Document(com.intellij.openapi.editor.Document) PsiFile(com.intellij.psi.PsiFile) LightVirtualFile(com.intellij.testFramework.LightVirtualFile) MockVirtualFile(com.intellij.mock.MockVirtualFile) File(java.io.File)

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