008389400d2a010fd5d26ee2ba9f3e068f5470c9
[idea/community.git] / python / educational-core / student / src / com / jetbrains / edu / learning / StudySubtaskUtils.java
1 package com.jetbrains.edu.learning;
2
3 import com.intellij.ide.projectView.ProjectView;
4 import com.intellij.openapi.editor.Document;
5 import com.intellij.openapi.editor.Editor;
6 import com.intellij.openapi.fileEditor.FileDocumentManager;
7 import com.intellij.openapi.project.Project;
8 import com.intellij.openapi.vfs.VirtualFile;
9 import com.intellij.problems.WolfTheProblemSolver;
10 import com.intellij.ui.EditorNotifications;
11 import com.jetbrains.edu.learning.checker.StudyCheckUtils;
12 import com.jetbrains.edu.learning.core.EduNames;
13 import com.jetbrains.edu.learning.core.EduUtils;
14 import com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder;
15 import com.jetbrains.edu.learning.courseFormat.AnswerPlaceholderSubtaskInfo;
16 import com.jetbrains.edu.learning.courseFormat.Task;
17 import com.jetbrains.edu.learning.courseFormat.TaskFile;
18 import com.jetbrains.edu.learning.ui.StudyToolWindow;
19 import org.jetbrains.annotations.NotNull;
20
21 import java.util.*;
22
23 public class StudySubtaskUtils {
24   private StudySubtaskUtils() {
25   }
26
27   /***
28    * @param toSubtaskIndex from 0 to subtaskNum - 1
29    */
30   public static void switchStep(@NotNull Project project, @NotNull Task task, int toSubtaskIndex) {
31     if (toSubtaskIndex == task.getActiveSubtaskIndex()) {
32       return;
33     }
34     VirtualFile taskDir = task.getTaskDir(project);
35     if (taskDir == null) {
36       return;
37     }
38     VirtualFile srcDir = taskDir.findChild(EduNames.SRC);
39     if (srcDir != null) {
40       taskDir = srcDir;
41     }
42     int fromSubtaskIndex = task.getActiveSubtaskIndex();
43     for (Map.Entry<String, TaskFile> entry : task.getTaskFiles().entrySet()) {
44       String name = entry.getKey();
45       VirtualFile virtualFile = taskDir.findChild(name);
46       if (virtualFile == null) {
47         continue;
48       }
49       TaskFile taskFile = entry.getValue();
50       Document document = FileDocumentManager.getInstance().getDocument(virtualFile);
51       if (document == null) {
52         continue;
53       }
54       updatePlaceholderTexts(project, document, taskFile, fromSubtaskIndex, toSubtaskIndex);
55       EditorNotifications.getInstance(project).updateNotifications(virtualFile);
56       if (StudyUtils.isStudyProject(project)) {
57         WolfTheProblemSolver.getInstance(project).clearProblems(virtualFile);
58         taskFile.setHighlightErrors(false);
59       }
60     }
61     task.setActiveSubtaskIndex(toSubtaskIndex);
62     update(project, task, taskDir);
63   }
64
65   private static void update(@NotNull Project project, @NotNull Task task, VirtualFile taskDir) {
66     StudyCheckUtils.drawAllPlaceholders(project, task, taskDir);
67     ProjectView.getInstance(project).refresh();
68     StudyToolWindow toolWindow = StudyUtils.getStudyToolWindow(project);
69     if (toolWindow != null) {
70       String text = StudyUtils.getTaskTextFromTask(taskDir, task);
71       if (text == null) {
72         toolWindow.setEmptyText(project);
73         return;
74       }
75       toolWindow.setTaskText(text, taskDir, project);
76     }
77   }
78
79   private static void updatePlaceholderTexts(@NotNull Project project,
80                                              @NotNull Document document,
81                                              @NotNull TaskFile taskFile,
82                                              int fromSubtaskIndex,
83                                              int toSubtaskIndex) {
84     taskFile.setTrackLengths(false);
85     for (AnswerPlaceholder placeholder : taskFile.getAnswerPlaceholders()) {
86       placeholder.switchSubtask(project, document, fromSubtaskIndex, toSubtaskIndex);
87     }
88     taskFile.setTrackLengths(true);
89   }
90
91   public static void refreshPlaceholder(@NotNull Project project, @NotNull Editor editor, @NotNull AnswerPlaceholder placeholder) {
92     int prevSubtaskIndex = placeholder.getActiveSubtaskIndex() - 1;
93     AnswerPlaceholderSubtaskInfo info = placeholder.getSubtaskInfos().get(prevSubtaskIndex);
94     String replacementText = info != null ? info.getAnswer() : placeholder.getTaskText();
95     EduUtils.replaceAnswerPlaceholder(project, editor.getDocument(), placeholder, placeholder.getRealLength(), replacementText);
96   }
97 }