Search in sources :

Example 31 with FileDocumentManager

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

the class FileDocumentManagerListener method beforeDocumentSaving.

@Override
public void beforeDocumentSaving(@NotNull Document document) {
    LOG.trace("FileDocumentManagerListener#beforeDocumentSaving({})", document);
    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) Editor(com.intellij.openapi.editor.Editor)

Example 32 with FileDocumentManager

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

the class VirtualFileListener method propertyChanged.

@Override
public void propertyChanged(@NotNull VirtualFilePropertyEvent event) {
    VirtualFile file = event.getFile();
    FileDocumentManager documentManager = FileDocumentManager.getInstance();
    Document document = documentManager.getDocument(file);
    if (document != null) {
        Editor[] editors = EditorFactory.getInstance().getEditors(document);
        for (Editor editor : editors) {
            Project project = editor.getProject();
            DiscordIntegrationProjectComponent component = DiscordIntegrationProjectComponent.getInstance(project);
            if (component != null && component.getFiles().containsKey(file)) {
                if ((event.getPropertyName().equals(VirtualFile.PROP_NAME))) {
                    component.fileUpdateName(file);
                } else if ((event.getPropertyName().equals(VirtualFile.PROP_WRITABLE))) {
                    component.fileUpdateReadOnly(event.getFile());
                }
            }
        }
    }
}
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 33 with FileDocumentManager

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

the class ProgrammingLanguageUtils method closeStepNodeFile.

private static ArrayList<VirtualFile> closeStepNodeFile(@NotNull Project project, @NotNull StepNode targetStepNode) {
    FileDocumentManager documentManager = FileDocumentManager.getInstance();
    ArrayList<VirtualFile> needClose = new ArrayList<>();
    for (VirtualFile file : FileEditorManager.getInstance(project).getOpenFiles()) {
        if (StudyUtils.getStudyNode(project, file) != targetStepNode) {
            continue;
        }
        final Document document = ApplicationManager.getApplication().runReadAction((Computable<Document>) () -> documentManager.getDocument(file));
        if (document == null) {
            continue;
        }
        ApplicationManager.getApplication().invokeAndWait(() -> documentManager.saveDocument(document));
        needClose.add(file);
    }
    return needClose;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ArrayList(java.util.ArrayList) FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager) Document(com.intellij.openapi.editor.Document)

Example 34 with FileDocumentManager

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

the class StepikResetStepAction method resetFile.

private static void resetFile(@Nullable final Project project) {
    StepNode stepNode = getCurrentCodeStepNode(project);
    if (stepNode == null) {
        return;
    }
    VirtualFile src = getOrCreateSrcDirectory(project, stepNode, true);
    if (src == null) {
        return;
    }
    String mainFileName = stepNode.getCurrentLang().getMainFileName();
    VirtualFile mainFile = src.findChild(mainFileName);
    if (mainFile != null) {
        FileDocumentManager documentManager = FileDocumentManager.getInstance();
        Document document = documentManager.getDocument(mainFile);
        if (document != null) {
            resetDocument(project, document, stepNode);
            if (!project.isDisposed()) {
                ProjectView.getInstance(project).refresh();
                WolfTheProblemSolver.getInstance(project).clearProblems(mainFile);
            }
            StepikProjectManager.updateSelection(project);
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) StepNode(org.stepik.core.courseFormat.StepNode) FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager) Document(com.intellij.openapi.editor.Document)

Example 35 with FileDocumentManager

use of com.intellij.openapi.fileEditor.FileDocumentManager in project scss-lint-plugin by idok.

the class ActualFile method getOrCreateActualFile.

@Nullable
public static ActualFile getOrCreateActualFile(@NotNull Key<ThreadLocalActualFile> key, @NotNull VirtualFile virtualFile, @Nullable String content) {
    FileDocumentManager fileDocumentManager = FileDocumentManager.getInstance();
    if (!fileDocumentManager.isFileModified(virtualFile)) {
        File file = new File(virtualFile.getPath());
        if (file.isFile()) {
            return new ActualFile(file);
        }
    }
    ThreadLocalActualFile threadLocal = key.get(virtualFile);
    if (threadLocal == null) {
        threadLocal = virtualFile.putUserDataIfAbsent(key, new ThreadLocalActualFile(virtualFile));
    }
    File file = threadLocal.getOrCreateFile();
    if (file == null) {
        return null;
    }
    if (content == null) {
        Document document = fileDocumentManager.getDocument(virtualFile);
        if (document != null) {
            content = document.getText();
        }
    }
    if (content == null) {
        return null;
    }
    try {
        FileUtil.writeToFile(file, content);
        return new ActualFile(new File(virtualFile.getPath()), file);
    } catch (IOException e) {
        LOG.warn("Can not write to " + file.getAbsolutePath(), e);
    }
    return null;
}
Also used : FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager) IOException(java.io.IOException) Document(com.intellij.openapi.editor.Document) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) 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