Search in sources :

Example 1 with FileDocumentManager

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

the class StudyRunAction method run.

public void run(@NotNull final Project project) {
    if (myHandler != null && !myHandler.isProcessTerminated())
        return;
    Editor selectedEditor = StudyUtils.getSelectedEditor(project);
    FileDocumentManager fileDocumentManager = FileDocumentManager.getInstance();
    assert selectedEditor != null;
    VirtualFile openedFile = fileDocumentManager.getFile(selectedEditor.getDocument());
    if (openedFile != null && openedFile.getCanonicalPath() != null) {
        String filePath = openedFile.getCanonicalPath();
        executeFile(project, openedFile, filePath);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager) StudyEditor(com.jetbrains.edu.learning.editor.StudyEditor) Editor(com.intellij.openapi.editor.Editor)

Example 2 with FileDocumentManager

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

the class StudyWindowNavigationAction method navigateToPlaceholder.

private void navigateToPlaceholder(@NotNull final Project project) {
    final Editor selectedEditor = StudyUtils.getSelectedEditor(project);
    if (selectedEditor != null) {
        final FileDocumentManager fileDocumentManager = FileDocumentManager.getInstance();
        final VirtualFile openedFile = fileDocumentManager.getFile(selectedEditor.getDocument());
        if (openedFile != null) {
            final TaskFile selectedTaskFile = StudyUtils.getTaskFile(project, openedFile);
            if (selectedTaskFile != null) {
                final int offset = selectedEditor.getCaretModel().getOffset();
                final AnswerPlaceholder targetPlaceholder = getTargetPlaceholder(selectedTaskFile, offset);
                if (targetPlaceholder == null) {
                    return;
                }
                StudyNavigator.navigateToAnswerPlaceholder(selectedEditor, targetPlaceholder);
            }
        }
    }
}
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) Editor(com.intellij.openapi.editor.Editor)

Example 3 with FileDocumentManager

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

the class StudySmartChecker method smartCheck.

public static void smartCheck(@NotNull final AnswerPlaceholder placeholder, @NotNull final Project project, @NotNull final VirtualFile answerFile, @NotNull final TaskFile answerTaskFile, @NotNull final TaskFile usersTaskFile, @NotNull final StudyTestRunner testRunner, @NotNull final VirtualFile virtualFile, @NotNull final Document usersDocument) {
    VirtualFile fileWindows = null;
    File resourceFile = null;
    VirtualFile windowCopy = null;
    try {
        final int index = placeholder.getIndex();
        String windowCopyName = answerFile.getNameWithoutExtension() + index + EduNames.WINDOW_POSTFIX + answerFile.getExtension();
        windowCopy = answerFile.copy(project, answerFile.getParent(), windowCopyName);
        final FileDocumentManager documentManager = FileDocumentManager.getInstance();
        final Document windowDocument = documentManager.getDocument(windowCopy);
        if (windowDocument != null) {
            resourceFile = StudyUtils.copyResourceFile(virtualFile.getName(), windowCopy.getName(), project, usersTaskFile.getTask());
            TaskFile windowTaskFile = answerTaskFile.getTask().copy().getTaskFile(StudyUtils.pathRelativeToTask(virtualFile));
            if (windowTaskFile == null) {
                return;
            }
            EduDocumentListener listener = new EduDocumentListener(windowTaskFile);
            windowDocument.addDocumentListener(listener);
            int start = placeholder.getOffset();
            int end = start + placeholder.getRealLength();
            final AnswerPlaceholder userAnswerPlaceholder = usersTaskFile.getAnswerPlaceholders().get(placeholder.getIndex());
            int userStart = userAnswerPlaceholder.getOffset();
            int userEnd = userStart + userAnswerPlaceholder.getRealLength();
            String text = usersDocument.getText(new TextRange(userStart, userEnd));
            windowDocument.replaceString(start, end, text);
            ApplicationManager.getApplication().runWriteAction(() -> documentManager.saveDocument(windowDocument));
            fileWindows = EduUtils.flushWindows(windowTaskFile, windowCopy);
            Process smartTestProcess = testRunner.createCheckProcess(project, windowCopy.getPath());
            final CapturingProcessHandler handler = new CapturingProcessHandler(smartTestProcess, null, windowCopy.getPath());
            final ProcessOutput output = handler.runProcess();
            final Course course = StudyTaskManager.getInstance(project).getCourse();
            if (course != null) {
                boolean res = StudyTestsOutputParser.getTestsOutput(output, course.isAdaptive()).isSuccess();
                StudyTaskManager.getInstance(project).setStatus(userAnswerPlaceholder, res ? StudyStatus.Solved : StudyStatus.Failed);
            }
        }
    } catch (ExecutionException | IOException e) {
        LOG.error(e);
    } finally {
        StudyUtils.deleteFile(windowCopy);
        StudyUtils.deleteFile(fileWindows);
        if (resourceFile != null && resourceFile.exists() && !resourceFile.delete()) {
            LOG.error("failed to delete", resourceFile.getPath());
        }
    }
}
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) TextRange(com.intellij.openapi.util.TextRange) IOException(java.io.IOException) Document(com.intellij.openapi.editor.Document) ProcessOutput(com.intellij.execution.process.ProcessOutput) Course(com.jetbrains.edu.learning.courseFormat.Course) ExecutionException(com.intellij.execution.ExecutionException) VirtualFile(com.intellij.openapi.vfs.VirtualFile) TaskFile(com.jetbrains.edu.learning.courseFormat.TaskFile) File(java.io.File) CapturingProcessHandler(com.intellij.execution.process.CapturingProcessHandler) EduDocumentListener(com.jetbrains.edu.learning.core.EduDocumentListener)

Example 4 with FileDocumentManager

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

the class VirtualFileDiffElement method refreshFile.

public static void refreshFile(boolean userInitiated, VirtualFile virtualFile) {
    if (userInitiated) {
        final List<Document> docsToSave = new ArrayList<>();
        final FileDocumentManager manager = FileDocumentManager.getInstance();
        for (Document document : manager.getUnsavedDocuments()) {
            VirtualFile file = manager.getFile(document);
            if (file != null && VfsUtilCore.isAncestor(virtualFile, file, false)) {
                docsToSave.add(document);
            }
        }
        if (!docsToSave.isEmpty()) {
            new WriteAction() {

                @Override
                protected void run(@NotNull Result result) throws Throwable {
                    for (Document document : docsToSave) {
                        manager.saveDocument(document);
                    }
                }
            }.execute();
        }
        ModalityState modalityState = ProgressManager.getInstance().getProgressIndicator().getModalityState();
        VfsUtil.markDirty(true, true, virtualFile);
        RefreshQueue.getInstance().refresh(false, true, null, modalityState, virtualFile);
    }
}
Also used : WriteAction(com.intellij.openapi.application.WriteAction) ArrayList(java.util.ArrayList) ModalityState(com.intellij.openapi.application.ModalityState) FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager) Document(com.intellij.openapi.editor.Document) Result(com.intellij.openapi.application.Result)

Example 5 with FileDocumentManager

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

the class BaseDiffAction method actionPerformed.

public void actionPerformed(AnActionEvent e) {
    DiffRequest diffData = getDiffData(e.getDataContext());
    if (diffData == null)
        return;
    final DiffContent[] contents = diffData.getContents();
    final FileDocumentManager documentManager = FileDocumentManager.getInstance();
    ApplicationManager.getApplication().runWriteAction(() -> {
        for (DiffContent content : contents) {
            Document document = content.getDocument();
            if (document != null) {
                documentManager.saveDocument(document);
            }
        }
    });
    DiffManager.getInstance().getDiffTool().show(diffData);
}
Also used : DiffRequest(com.intellij.openapi.diff.DiffRequest) FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager) Document(com.intellij.openapi.editor.Document) DiffContent(com.intellij.openapi.diff.DiffContent)

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