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);
}
}
}
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"));
}
}
}
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());
}
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);
}
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);
}
}
}
Aggregations