Search in sources :

Example 11 with StudyEditor

use of com.jetbrains.edu.learning.editor.StudyEditor in project intellij-community by JetBrains.

the class StudyUtils method updateAction.

public static void updateAction(@NotNull final AnActionEvent e) {
    final Presentation presentation = e.getPresentation();
    presentation.setEnabled(false);
    final Project project = e.getProject();
    if (project != null) {
        final StudyEditor studyEditor = getSelectedStudyEditor(project);
        if (studyEditor != null) {
            presentation.setEnabledAndVisible(true);
        }
    }
}
Also used : Project(com.intellij.openapi.project.Project) StudyEditor(com.jetbrains.edu.learning.editor.StudyEditor) Presentation(com.intellij.openapi.actionSystem.Presentation)

Example 12 with StudyEditor

use of com.jetbrains.edu.learning.editor.StudyEditor in project intellij-community by JetBrains.

the class EduAdaptiveStepicConnector method addNextRecommendedTask.

public static void addNextRecommendedTask(@NotNull Project project, @NotNull ProgressIndicator indicator, int reaction) {
    final StudyEditor editor = StudyUtils.getSelectedStudyEditor(project);
    final Course course = StudyTaskManager.getInstance(project).getCourse();
    if (course != null && editor != null && editor.getTaskFile() != null) {
        indicator.checkCanceled();
        final StepicUser user = StepicUpdateSettings.getInstance().getUser();
        final boolean recommendationReaction = postRecommendationReaction(String.valueOf(editor.getTaskFile().getTask().getLesson().getId()), String.valueOf(user.getId()), reaction);
        if (recommendationReaction) {
            indicator.checkCanceled();
            final Task task = getNextRecommendation(project, course);
            if (task != null) {
                final Lesson adaptive = course.getLessons().get(0);
                final Task unsolvedTask = adaptive.getTaskList().get(adaptive.getTaskList().size() - 1);
                final String lessonName = EduNames.LESSON + String.valueOf(adaptive.getIndex());
                if (reaction == TOO_HARD_RECOMMENDATION_REACTION || reaction == TOO_BORING_RECOMMENDATION_REACTION) {
                    unsolvedTask.copyParametersOf(task);
                    final Map<String, TaskFile> taskFiles = task.getTaskFiles();
                    if (taskFiles.size() == 1) {
                        final TaskFile taskFile = editor.getTaskFile();
                        taskFile.text = ((TaskFile) taskFiles.values().toArray()[0]).text;
                        ApplicationManager.getApplication().invokeLater(() -> ApplicationManager.getApplication().runWriteAction(() -> {
                            final Document document = editor.getEditor().getDocument();
                            document.setText(taskFile.text);
                        }));
                    } else {
                        LOG.warn("Got task without unexpected number of task files: " + taskFiles.size());
                    }
                    final File lessonDirectory = new File(course.getCourseDirectory(), lessonName);
                    final String taskName = EduNames.TASK + String.valueOf(adaptive.getTaskList().size());
                    final File taskDirectory = new File(lessonDirectory, taskName);
                    StudyProjectGenerator.flushTask(task, taskDirectory);
                    StudyProjectGenerator.flushCourseJson(course, new File(course.getCourseDirectory()));
                    final VirtualFile lessonDir = project.getBaseDir().findChild(lessonName);
                    if (lessonDir != null) {
                        createTestFiles(course, project, unsolvedTask, lessonDir);
                    }
                    final StudyToolWindow window = StudyUtils.getStudyToolWindow(project);
                    if (window != null) {
                        window.setTaskText(StudyUtils.wrapTextToDisplayLatex(unsolvedTask.getText()), unsolvedTask.getTaskDir(project), project);
                    }
                    StudyNavigator.navigateToTask(project, lessonName, taskName);
                } else {
                    adaptive.addTask(task);
                    task.setIndex(adaptive.getTaskList().size());
                    final VirtualFile lessonDir = project.getBaseDir().findChild(lessonName);
                    if (lessonDir != null) {
                        ApplicationManager.getApplication().invokeLater(() -> ApplicationManager.getApplication().runWriteAction(() -> {
                            try {
                                final File lessonDirectory = new File(course.getCourseDirectory(), lessonName);
                                final String taskName = EduNames.TASK + String.valueOf(task.getIndex());
                                final File taskDir = new File(lessonDirectory, taskName);
                                StudyProjectGenerator.flushTask(task, taskDir);
                                StudyProjectGenerator.flushCourseJson(course, new File(course.getCourseDirectory()));
                                StudyGenerator.createTask(task, lessonDir, new File(course.getCourseDirectory(), lessonDir.getName()), project);
                                adaptive.initLesson(course, true);
                                StudyNavigator.navigateToTask(project, lessonName, taskName);
                            } catch (IOException e) {
                                LOG.warn(e.getMessage());
                            }
                        }));
                    }
                }
            } else {
                ApplicationManager.getApplication().invokeLater(() -> {
                    final Balloon balloon = JBPopupFactory.getInstance().createHtmlTextBalloonBuilder("Couldn't load a new recommendation", MessageType.ERROR, null).createBalloon();
                    StudyUtils.showCheckPopUp(project, balloon);
                });
            }
            ApplicationManager.getApplication().invokeLater(() -> {
                VirtualFileManager.getInstance().refreshWithoutFileWatcher(false);
                ProjectView.getInstance(project).refresh();
            });
        } else {
            LOG.warn("Recommendation reactions weren't posted");
            ApplicationManager.getApplication().invokeLater(() -> StudyUtils.showErrorPopupOnToolbar(project, "Couldn't post your reaction"));
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Balloon(com.intellij.openapi.ui.popup.Balloon) StudyToolWindow(com.jetbrains.edu.learning.ui.StudyToolWindow) IOException(java.io.IOException) Document(com.intellij.openapi.editor.Document) StudyEditor(com.jetbrains.edu.learning.editor.StudyEditor) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File)

Example 13 with StudyEditor

use of com.jetbrains.edu.learning.editor.StudyEditor in project intellij-community by JetBrains.

the class StudyRefreshAnswerPlaceholder method getAnswerPlaceholder.

@Nullable
private static AnswerPlaceholder getAnswerPlaceholder(AnActionEvent e) {
    final Project project = e.getProject();
    if (project == null) {
        return null;
    }
    StudyEditor studyEditor = StudyUtils.getSelectedStudyEditor(project);
    final StudyState studyState = new StudyState(studyEditor);
    if (studyEditor == null || !studyState.isValid()) {
        return null;
    }
    final Editor editor = studyState.getEditor();
    final TaskFile taskFile = studyState.getTaskFile();
    return taskFile.getAnswerPlaceholder(editor.getCaretModel().getOffset());
}
Also used : TaskFile(com.jetbrains.edu.learning.courseFormat.TaskFile) Project(com.intellij.openapi.project.Project) StudyEditor(com.jetbrains.edu.learning.editor.StudyEditor) StudyEditor(com.jetbrains.edu.learning.editor.StudyEditor) Editor(com.intellij.openapi.editor.Editor) Nullable(org.jetbrains.annotations.Nullable)

Example 14 with StudyEditor

use of com.jetbrains.edu.learning.editor.StudyEditor in project intellij-community by JetBrains.

the class StudyRefreshAnswerPlaceholder method actionPerformed.

@Override
public void actionPerformed(AnActionEvent e) {
    Project project = e.getProject();
    if (project == null) {
        return;
    }
    for (StudyActionListener listener : Extensions.getExtensions(StudyActionListener.EP_NAME)) {
        listener.beforeCheck(e);
    }
    final AnswerPlaceholder answerPlaceholder = getAnswerPlaceholder(e);
    if (answerPlaceholder == null) {
        return;
    }
    StudyEditor studyEditor = StudyUtils.getSelectedStudyEditor(project);
    final StudyState studyState = new StudyState(studyEditor);
    if (answerPlaceholder.getTaskFile().getTask().hasSubtasks()) {
        StudySubtaskUtils.refreshPlaceholder(studyState.getEditor(), answerPlaceholder);
        return;
    }
    Document patternDocument = StudyUtils.getPatternDocument(answerPlaceholder.getTaskFile(), studyState.getVirtualFile().getName());
    if (patternDocument == null) {
        return;
    }
    AnswerPlaceholder.MyInitialState initialState = answerPlaceholder.getInitialState();
    int startOffset = initialState.getOffset();
    final String text = patternDocument.getText(new TextRange(startOffset, startOffset + initialState.getLength()));
    CommandProcessor.getInstance().executeCommand(project, () -> ApplicationManager.getApplication().runWriteAction(() -> {
        Document document = studyState.getEditor().getDocument();
        int offset = answerPlaceholder.getOffset();
        document.deleteString(offset, offset + answerPlaceholder.getRealLength());
        document.insertString(offset, text);
    }), NAME, null);
}
Also used : Project(com.intellij.openapi.project.Project) AnswerPlaceholder(com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder) StudyEditor(com.jetbrains.edu.learning.editor.StudyEditor) TextRange(com.intellij.openapi.util.TextRange) Document(com.intellij.openapi.editor.Document)

Example 15 with StudyEditor

use of com.jetbrains.edu.learning.editor.StudyEditor in project intellij-community by JetBrains.

the class StudyRefreshTaskFileAction method update.

@Override
public void update(AnActionEvent event) {
    StudyUtils.updateAction(event);
    final Project project = event.getProject();
    if (project != null) {
        StudyEditor studyEditor = StudyUtils.getSelectedStudyEditor(project);
        StudyState studyState = new StudyState(studyEditor);
        Presentation presentation = event.getPresentation();
        if (!studyState.isValid()) {
            presentation.setEnabled(false);
            return;
        }
        Course course = StudyTaskManager.getInstance(project).getCourse();
        if (course == null) {
            return;
        }
        if (!EduNames.STUDY.equals(course.getCourseMode())) {
            presentation.setVisible(true);
            presentation.setEnabled(false);
        }
    }
}
Also used : Project(com.intellij.openapi.project.Project) StudyEditor(com.jetbrains.edu.learning.editor.StudyEditor) Presentation(com.intellij.openapi.actionSystem.Presentation)

Aggregations

StudyEditor (com.jetbrains.edu.learning.editor.StudyEditor)20 Project (com.intellij.openapi.project.Project)10 TaskFile (com.jetbrains.edu.learning.courseFormat.TaskFile)7 VirtualFile (com.intellij.openapi.vfs.VirtualFile)5 Presentation (com.intellij.openapi.actionSystem.Presentation)4 Editor (com.intellij.openapi.editor.Editor)4 FileEditor (com.intellij.openapi.fileEditor.FileEditor)4 StudyState (com.jetbrains.edu.learning.StudyState)4 Task (com.jetbrains.edu.learning.courseFormat.Task)4 Document (com.intellij.openapi.editor.Document)3 UserTest (com.jetbrains.edu.learning.courseFormat.UserTest)3 Balloon (com.intellij.openapi.ui.popup.Balloon)2 StudyTaskManager (com.jetbrains.edu.learning.StudyTaskManager)2 AnswerPlaceholder (com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder)2 Map (java.util.Map)2 Nullable (org.jetbrains.annotations.Nullable)2 FileDocumentManager (com.intellij.openapi.fileEditor.FileDocumentManager)1 BalloonBuilder (com.intellij.openapi.ui.popup.BalloonBuilder)1 JBPopup (com.intellij.openapi.ui.popup.JBPopup)1 TextRange (com.intellij.openapi.util.TextRange)1