Search in sources :

Example 61 with FileDocumentManager

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

the class StudyCheckUtils method getCopyWithAnswers.

private static Pair<VirtualFile, TaskFile> getCopyWithAnswers(@NotNull final VirtualFile taskDir, @NotNull final VirtualFile file, @NotNull final TaskFile source) {
    try {
        VirtualFile answerFile = file.copy(taskDir, taskDir, file.getNameWithoutExtension() + EduNames.ANSWERS_POSTFIX + "." + file.getExtension());
        final FileDocumentManager documentManager = FileDocumentManager.getInstance();
        final Document document = documentManager.getDocument(answerFile);
        if (document != null) {
            TaskFile answerTaskFile = source.getTask().copy().getTaskFile(StudyUtils.pathRelativeToTask(file));
            if (answerTaskFile == null) {
                return null;
            }
            EduDocumentListener listener = new EduDocumentListener(answerTaskFile);
            document.addDocumentListener(listener);
            for (AnswerPlaceholder answerPlaceholder : answerTaskFile.getActivePlaceholders()) {
                final int start = answerPlaceholder.getOffset();
                final int end = start + answerPlaceholder.getRealLength();
                final String text = answerPlaceholder.getPossibleAnswer();
                document.replaceString(start, end, text);
            }
            ApplicationManager.getApplication().runWriteAction(() -> documentManager.saveDocument(document));
            return Pair.create(answerFile, answerTaskFile);
        }
    } catch (IOException e) {
        LOG.error(e);
    }
    return null;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) TaskFile(com.jetbrains.edu.learning.courseFormat.TaskFile) AnswerPlaceholder(com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder) FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager) IOException(java.io.IOException) Document(com.intellij.openapi.editor.Document) EduDocumentListener(com.jetbrains.edu.learning.core.EduDocumentListener)

Example 62 with FileDocumentManager

use of com.intellij.openapi.fileEditor.FileDocumentManager in project Intellij-Discord-Integration by Almighty-Alpaca.

the class DocumentListener method documentChanged.

@Override
public void documentChanged(DocumentEvent event) {
    LOG.trace("DocumentListener#documentChanged({})", event.getDocument());
    Document document = event.getDocument();
    Editor[] editors = EditorFactory.getInstance().getEditors(document);
    FileDocumentManager documentManager = FileDocumentManager.getInstance();
    for (Editor editor : editors) {
        Project project = editor.getProject();
        DiscordIntegrationProjectComponent component = DiscordIntegrationProjectComponent.getInstance(project);
        if (component != null)
            component.updateTimeAccessed(documentManager.getFile(document));
    }
}
Also used : Project(com.intellij.openapi.project.Project) DiscordIntegrationProjectComponent(com.almightyalpaca.jetbrains.plugins.discord.components.DiscordIntegrationProjectComponent) FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager) Document(com.intellij.openapi.editor.Document) Editor(com.intellij.openapi.editor.Editor)

Example 63 with FileDocumentManager

use of com.intellij.openapi.fileEditor.FileDocumentManager in project Intellij-Discord-Integration by Almighty-Alpaca.

the class VisibleAreaListener method visibleAreaChanged.

@Override
public void visibleAreaChanged(VisibleAreaEvent event) {
    LOG.trace("VisibleAreaListener#visibleAreaChanged({})", event);
    FileDocumentManager documentManager = FileDocumentManager.getInstance();
    Project project = event.getEditor().getProject();
    DiscordIntegrationProjectComponent component = DiscordIntegrationProjectComponent.getInstance(project);
    if (component != null)
        component.updateTimeAccessed(documentManager.getFile(event.getEditor().getDocument()));
}
Also used : Project(com.intellij.openapi.project.Project) DiscordIntegrationProjectComponent(com.almightyalpaca.jetbrains.plugins.discord.components.DiscordIntegrationProjectComponent) FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager)

Example 64 with FileDocumentManager

use of com.intellij.openapi.fileEditor.FileDocumentManager in project sonarlint-intellij by SonarSource.

the class SonarLintUtils method getSelectedFile.

/**
 * FileEditorManager#getSelectedFiles does not work as expected. In split editors, the order of the files does not change depending
 * on which one of the split editors is selected.
 * This seems to work well with split editors.
 */
@CheckForNull
public static VirtualFile getSelectedFile(Project project) {
    if (project.isDisposed()) {
        return null;
    }
    FileEditorManager editorManager = FileEditorManager.getInstance(project);
    Editor editor = editorManager.getSelectedTextEditor();
    if (editor != null) {
        Document doc = editor.getDocument();
        FileDocumentManager docManager = FileDocumentManager.getInstance();
        return docManager.getFile(doc);
    }
    return null;
}
Also used : FileEditorManager(com.intellij.openapi.fileEditor.FileEditorManager) FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager) Editor(com.intellij.openapi.editor.Editor) Document(com.intellij.openapi.editor.Document) CheckForNull(javax.annotation.CheckForNull)

Example 65 with FileDocumentManager

use of com.intellij.openapi.fileEditor.FileDocumentManager in project sonarlint-intellij by SonarSource.

the class SonarLintUtils method saveFile.

/**
 * Must be called from EDT
 */
private static boolean saveFile(final VirtualFile virtualFile) {
    ApplicationManager.getApplication().assertIsDispatchThread();
    final FileDocumentManager fileDocumentManager = FileDocumentManager.getInstance();
    if (fileDocumentManager.isFileModified(virtualFile)) {
        final Document document = fileDocumentManager.getDocument(virtualFile);
        if (document != null) {
            fileDocumentManager.saveDocument(document);
            return true;
        }
    }
    return false;
}
Also used : FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager) Document(com.intellij.openapi.editor.Document)

Aggregations

FileDocumentManager (com.intellij.openapi.fileEditor.FileDocumentManager)69 Document (com.intellij.openapi.editor.Document)44 VirtualFile (com.intellij.openapi.vfs.VirtualFile)38 IOException (java.io.IOException)16 Editor (com.intellij.openapi.editor.Editor)12 Project (com.intellij.openapi.project.Project)11 LightVirtualFile (com.intellij.testFramework.LightVirtualFile)11 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 Module (com.intellij.openapi.module.Module)4 IncorrectOperationException (com.intellij.util.IncorrectOperationException)4 TaskFile (com.jetbrains.edu.learning.courseFormat.TaskFile)4 NotNull (org.jetbrains.annotations.NotNull)4 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