Search in sources :

Example 21 with FileDocumentManager

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

the class PubspecYamlUtil method getPubspecYamlInfo.

@Nullable
private static Map<String, Object> getPubspecYamlInfo(@NotNull final VirtualFile pubspecYamlFile) {
    // do not use Yaml plugin here - IntelliJ IDEA Community Edition doesn't contain it.
    Pair<Long, Map<String, Object>> data = pubspecYamlFile.getUserData(MOD_STAMP_TO_PUBSPEC_NAME);
    final FileDocumentManager documentManager = FileDocumentManager.getInstance();
    final Document cachedDocument = documentManager.getCachedDocument(pubspecYamlFile);
    final Long currentTimestamp = cachedDocument != null ? cachedDocument.getModificationStamp() : pubspecYamlFile.getModificationCount();
    final Long cachedTimestamp = data == null ? null : data.first;
    if (cachedTimestamp == null || !cachedTimestamp.equals(currentTimestamp)) {
        data = null;
        pubspecYamlFile.putUserData(MOD_STAMP_TO_PUBSPEC_NAME, null);
        try {
            final Map<String, Object> pubspecYamlInfo;
            if (cachedDocument != null) {
                pubspecYamlInfo = loadPubspecYamlInfo(cachedDocument.getText());
            } else {
                pubspecYamlInfo = loadPubspecYamlInfo(VfsUtilCore.loadText(pubspecYamlFile));
            }
            if (pubspecYamlInfo != null) {
                data = Pair.create(currentTimestamp, pubspecYamlInfo);
                pubspecYamlFile.putUserData(MOD_STAMP_TO_PUBSPEC_NAME, data);
            }
        } catch (IOException ignored) {
        /* unlucky */
        }
    }
    return data == null ? null : data.second;
}
Also used : FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager) IOException(java.io.IOException) Document(com.intellij.openapi.editor.Document) Map(java.util.Map) Nullable(org.jetbrains.annotations.Nullable)

Example 22 with FileDocumentManager

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

the class IdeaGateway method doCreateEntry.

@Nullable
private Entry doCreateEntry(@NotNull VirtualFile file, boolean forDeletion) {
    if (!file.isDirectory()) {
        if (!isVersioned(file))
            return null;
        Pair<StoredContent, Long> contentAndStamps;
        if (forDeletion) {
            FileDocumentManager m = FileDocumentManager.getInstance();
            // should not try to load document
            Document d = m.isFileModified(file) ? m.getCachedDocument(file) : null;
            contentAndStamps = acquireAndClearCurrentContent(file, d);
        } else {
            contentAndStamps = getActualContentNoAcquire(file);
        }
        return doCreateFileEntry(file, contentAndStamps);
    }
    DirectoryEntry newDir = null;
    if (file instanceof VirtualFileSystemEntry) {
        int nameId = ((VirtualFileSystemEntry) file).getNameId();
        if (nameId > 0) {
            newDir = new DirectoryEntry(nameId);
        }
    }
    if (newDir == null) {
        newDir = new DirectoryEntry(file.getName());
    }
    doCreateChildren(newDir, iterateDBChildren(file), forDeletion);
    if (!isVersioned(file) && newDir.getChildren().isEmpty())
        return null;
    return newDir;
}
Also used : VirtualFileSystemEntry(com.intellij.openapi.vfs.newvfs.impl.VirtualFileSystemEntry) FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager) StoredContent(com.intellij.history.core.StoredContent) Document(com.intellij.openapi.editor.Document) DirectoryEntry(com.intellij.history.core.tree.DirectoryEntry) Nullable(org.jetbrains.annotations.Nullable)

Example 23 with FileDocumentManager

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

the class LossyEncodingInspection method isGoodCharset.

// check if file was loaded in correct encoding
// returns true if text converted with charset is equals to the bytes currently on disk
private static boolean isGoodCharset(@NotNull VirtualFile virtualFile, @NotNull Charset charset) {
    FileDocumentManager documentManager = FileDocumentManager.getInstance();
    Document document = documentManager.getDocument(virtualFile);
    if (document == null)
        return true;
    byte[] loadedBytes;
    byte[] bytesToSave;
    try {
        loadedBytes = virtualFile.contentsToByteArray();
        bytesToSave = new String(loadedBytes, charset).getBytes(charset);
    } catch (Exception e) {
        return true;
    }
    byte[] bom = virtualFile.getBOM();
    if (bom != null && !ArrayUtil.startsWith(bytesToSave, bom)) {
        // for 2-byte encodings String.getBytes(Charset) adds BOM automatically
        bytesToSave = ArrayUtil.mergeArrays(bom, bytesToSave);
    }
    boolean equals = Arrays.equals(bytesToSave, loadedBytes);
    if (!equals && LOG.isDebugEnabled()) {
        try {
            String tempDir = FileUtil.getTempDirectory();
            FileUtil.writeToFile(new File(tempDir, "lossy-bytes-to-save"), bytesToSave);
            FileUtil.writeToFile(new File(tempDir, "lossy-loaded-bytes"), loadedBytes);
            LOG.debug("lossy bytes dumped into " + tempDir);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    return equals;
}
Also used : FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager) IOException(java.io.IOException) Document(com.intellij.openapi.editor.Document) VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiFile(com.intellij.psi.PsiFile) File(java.io.File) IOException(java.io.IOException)

Example 24 with FileDocumentManager

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

the class XXXXX 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;
    }
    ActionManagerEx actionManager = ActionManagerEx.getInstanceEx();
    DataContext dataContext = getDataContext();
    actionManager.fireBeforeEditorTyping(c, dataContext);
    MacUIUtil.hideCursor();
    EditorActionManager.getInstance().getTypedAction().actionPerformed(this, c, dataContext);
    return true;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) DataContext(com.intellij.openapi.actionSystem.DataContext) FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager) ActionManagerEx(com.intellij.openapi.actionSystem.ex.ActionManagerEx)

Example 25 with FileDocumentManager

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

the class DocumentImpl method beforeChangedUpdate.

private void beforeChangedUpdate(DocumentEvent event) {
    Application app = ApplicationManager.getApplication();
    if (app != null) {
        FileDocumentManager manager = FileDocumentManager.getInstance();
        VirtualFile file = manager.getFile(this);
        if (file != null && !file.isValid()) {
            LOG.error("File of this document has been deleted.");
        }
    }
    assertInsideCommand();
    // initialize line set to track changed lines
    getLineSet();
    if (!ShutDownTracker.isShutdownHookRunning()) {
        DocumentListener[] listeners = getListeners();
        for (int i = listeners.length - 1; i >= 0; i--) {
            try {
                listeners[i].beforeDocumentChange(event);
            } catch (Throwable e) {
                LOG.error(e);
            }
        }
    }
    myEventsHandling = true;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) DocumentListener(com.intellij.openapi.editor.event.DocumentListener) FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager) Application(com.intellij.openapi.application.Application)

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