Search in sources :

Example 1 with EduDocumentListener

use of com.jetbrains.edu.learning.core.EduDocumentListener 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 2 with EduDocumentListener

use of com.jetbrains.edu.learning.core.EduDocumentListener in project intellij-community by JetBrains.

the class StudySubtaskUtils method switchStep.

/***
   * @param toSubtaskIndex from 0 to subtaskNum - 1
   */
public static void switchStep(@NotNull Project project, @NotNull Task task, int toSubtaskIndex, boolean navigateToTask) {
    if (toSubtaskIndex == task.getActiveSubtaskIndex()) {
        return;
    }
    VirtualFile taskDir = task.getTaskDir(project);
    if (taskDir == null) {
        return;
    }
    VirtualFile srcDir = taskDir.findChild(EduNames.SRC);
    if (srcDir != null) {
        taskDir = srcDir;
    }
    int fromSubtaskIndex = task.getActiveSubtaskIndex();
    for (Map.Entry<String, TaskFile> entry : task.getTaskFiles().entrySet()) {
        String name = entry.getKey();
        VirtualFile virtualFile = taskDir.findFileByRelativePath(name);
        if (virtualFile == null) {
            continue;
        }
        TaskFile taskFile = entry.getValue();
        Document document = FileDocumentManager.getInstance().getDocument(virtualFile);
        if (document == null) {
            continue;
        }
        EduDocumentListener listener = null;
        if (!FileEditorManager.getInstance(project).isFileOpen(virtualFile)) {
            listener = new EduDocumentListener(taskFile, true);
            document.addDocumentListener(listener);
        }
        updatePlaceholderTexts(document, taskFile, fromSubtaskIndex, toSubtaskIndex);
        if (listener != null) {
            document.removeDocumentListener(listener);
        }
        UndoManager.getInstance(project).nonundoableActionPerformed(DocumentReferenceManager.getInstance().create(document), false);
        EditorNotifications.getInstance(project).updateNotifications(virtualFile);
        if (StudyUtils.isStudentProject(project)) {
            WolfTheProblemSolver.getInstance(project).clearProblems(virtualFile);
            taskFile.setHighlightErrors(false);
        }
    }
    transformTestFile(project, toSubtaskIndex, taskDir);
    task.setActiveSubtaskIndex(toSubtaskIndex);
    updateUI(project, task, taskDir, navigateToTask);
    for (StudySubtaskChangeListener listener : Extensions.getExtensions(StudySubtaskChangeListener.EP_NAME)) {
        listener.subtaskChanged(project, task, fromSubtaskIndex, toSubtaskIndex);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Document(com.intellij.openapi.editor.Document) Map(java.util.Map) EduDocumentListener(com.jetbrains.edu.learning.core.EduDocumentListener)

Example 3 with EduDocumentListener

use of com.jetbrains.edu.learning.core.EduDocumentListener in project intellij-community by JetBrains.

the class CCFromCourseArchive method createAnswerFile.

public static void createAnswerFile(@NotNull final Project project, @NotNull final VirtualFile userFileDir, @NotNull final Map.Entry<String, TaskFile> taskFileEntry) {
    final String name = taskFileEntry.getKey();
    final TaskFile taskFile = taskFileEntry.getValue();
    VirtualFile file = userFileDir.findFileByRelativePath(name);
    assert file != null;
    final Document originDocument = FileDocumentManager.getInstance().getDocument(file);
    if (originDocument == null) {
        return;
    }
    final Document document = FileDocumentManager.getInstance().getDocument(file);
    if (document == null)
        return;
    CommandProcessor.getInstance().executeCommand(project, () -> ApplicationManager.getApplication().runWriteAction(() -> document.replaceString(0, document.getTextLength(), originDocument.getCharsSequence())), "Create answer document", "Create answer document");
    EduDocumentListener listener = new EduDocumentListener(taskFile, false);
    document.addDocumentListener(listener);
    taskFile.sortAnswerPlaceholders();
    for (int i = taskFile.getActivePlaceholders().size() - 1; i >= 0; i--) {
        final AnswerPlaceholder answerPlaceholder = taskFile.getAnswerPlaceholders().get(i);
        replaceAnswerPlaceholder(project, document, answerPlaceholder);
    }
    CommandProcessor.getInstance().executeCommand(project, () -> ApplicationManager.getApplication().runWriteAction(() -> FileDocumentManager.getInstance().saveDocument(document)), "x", "qwe");
    document.removeDocumentListener(listener);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Document(com.intellij.openapi.editor.Document) EduDocumentListener(com.jetbrains.edu.learning.core.EduDocumentListener)

Example 4 with EduDocumentListener

use of com.jetbrains.edu.learning.core.EduDocumentListener in project intellij-community by JetBrains.

the class StudyEditor method removeListener.

public static void removeListener(Document document) {
    final EduDocumentListener listener = myDocumentListeners.get(document);
    if (listener != null) {
        document.removeDocumentListener(listener);
    }
    myDocumentListeners.remove(document);
}
Also used : EduDocumentListener(com.jetbrains.edu.learning.core.EduDocumentListener)

Example 5 with EduDocumentListener

use of com.jetbrains.edu.learning.core.EduDocumentListener in project intellij-community by JetBrains.

the class StudyEditorFactoryListener method editorCreated.

@Override
public void editorCreated(@NotNull final EditorFactoryEvent event) {
    final Editor editor = event.getEditor();
    final Project project = editor.getProject();
    if (project == null) {
        return;
    }
    final Document document = editor.getDocument();
    final VirtualFile openedFile = FileDocumentManager.getInstance().getFile(document);
    if (openedFile != null) {
        final TaskFile taskFile = StudyUtils.getTaskFile(project, openedFile);
        if (taskFile != null) {
            WolfTheProblemSolver.getInstance(project).clearProblems(openedFile);
            final ToolWindow studyToolWindow = ToolWindowManager.getInstance(project).getToolWindow(StudyToolWindowFactory.STUDY_TOOL_WINDOW);
            if (studyToolWindow != null) {
                StudyUtils.updateToolWindows(project);
                studyToolWindow.show(null);
            }
            Course course = StudyTaskManager.getInstance(project).getCourse();
            if (course == null) {
                return;
            }
            StudyEditor.addDocumentListener(document, new EduDocumentListener(taskFile, true));
            if (!taskFile.getAnswerPlaceholders().isEmpty()) {
                StudyNavigator.navigateToFirstAnswerPlaceholder(editor, taskFile);
                boolean isStudyProject = EduNames.STUDY.equals(course.getCourseMode());
                StudyUtils.drawAllAnswerPlaceholders(editor, taskFile);
                if (isStudyProject) {
                    editor.addEditorMouseListener(new WindowSelectionListener(taskFile));
                }
            }
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) TaskFile(com.jetbrains.edu.learning.courseFormat.TaskFile) Project(com.intellij.openapi.project.Project) ToolWindow(com.intellij.openapi.wm.ToolWindow) Editor(com.intellij.openapi.editor.Editor) Document(com.intellij.openapi.editor.Document) Course(com.jetbrains.edu.learning.courseFormat.Course) EduDocumentListener(com.jetbrains.edu.learning.core.EduDocumentListener)

Aggregations

EduDocumentListener (com.jetbrains.edu.learning.core.EduDocumentListener)6 Document (com.intellij.openapi.editor.Document)5 VirtualFile (com.intellij.openapi.vfs.VirtualFile)5 TaskFile (com.jetbrains.edu.learning.courseFormat.TaskFile)3 FileDocumentManager (com.intellij.openapi.fileEditor.FileDocumentManager)2 AnswerPlaceholder (com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder)2 Course (com.jetbrains.edu.learning.courseFormat.Course)2 IOException (java.io.IOException)2 ExecutionException (com.intellij.execution.ExecutionException)1 CapturingProcessHandler (com.intellij.execution.process.CapturingProcessHandler)1 ProcessOutput (com.intellij.execution.process.ProcessOutput)1 Editor (com.intellij.openapi.editor.Editor)1 Project (com.intellij.openapi.project.Project)1 TextRange (com.intellij.openapi.util.TextRange)1 ToolWindow (com.intellij.openapi.wm.ToolWindow)1 File (java.io.File)1 Map (java.util.Map)1