Search in sources :

Example 56 with FileDocumentManager

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

the class InsertStepikDirectives method actionPerformed.

@Override
public void actionPerformed(AnActionEvent e) {
    Project project = e.getProject();
    if (project == null) {
        return;
    }
    StudyNode selectedNode = StepikProjectManager.getSelected(project);
    if (!(selectedNode instanceof StepNode) || ((StepNode) selectedNode).getType() != CODE) {
        return;
    }
    StepNode targetStepNode = (StepNode) selectedNode;
    FileDocumentManager documentManager = FileDocumentManager.getInstance();
    for (VirtualFile file : FileEditorManager.getInstance(project).getOpenFiles()) {
        Document document = documentManager.getDocument(file);
        if (document != null)
            documentManager.saveDocument(document);
    }
    SupportedLanguages currentLang = targetStepNode.getCurrentLang();
    VirtualFile src = getOrCreateSrcDirectory(project, targetStepNode, true);
    if (src == null) {
        return;
    }
    VirtualFile file = src.findChild(currentLang.getMainFileName());
    if (file == null) {
        return;
    }
    String text = getFileText(file);
    StepikProjectManager projectManager = StepikProjectManager.getInstance(project);
    boolean showHint = projectManager != null && projectManager.getShowHint();
    boolean needInsert = !containsDirectives(text, currentLang);
    if (needInsert) {
        text = insertAmbientCode(text, currentLang, showHint);
        Metrics.insertAmbientCodeAction(project, targetStepNode, SUCCESSFUL);
    } else {
        text = removeAmbientCode(text, showHint, currentLang, true);
        Metrics.removeAmbientCodeAction(project, targetStepNode, SUCCESSFUL);
    }
    writeInToFile(text, file, project);
    if (needInsert) {
        final Document document = FileDocumentManager.getInstance().getDocument(file);
        if (document != null) {
            ReformatUtils.reformatSelectedEditor(project, document);
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) SupportedLanguages(org.stepik.core.SupportedLanguages) Project(com.intellij.openapi.project.Project) StepikProjectManager(org.stepik.core.StepikProjectManager) StepNode(org.stepik.core.courseFormat.StepNode) FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager) Document(com.intellij.openapi.editor.Document) StudyNode(org.stepik.core.courseFormat.StudyNode)

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