do not show errors when switching to the next subtask
[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.fileEditor.FileDocumentManager;
6 import com.intellij.openapi.project.Project;
7 import com.intellij.openapi.vfs.VirtualFile;
8 import com.intellij.problems.WolfTheProblemSolver;
9 import com.intellij.ui.EditorNotifications;
10 import com.jetbrains.edu.learning.checker.StudyCheckUtils;
11 import com.jetbrains.edu.learning.core.EduNames;
12 import com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder;
13 import com.jetbrains.edu.learning.courseFormat.Task;
14 import com.jetbrains.edu.learning.courseFormat.TaskFile;
15 import com.jetbrains.edu.learning.ui.StudyToolWindow;
16 import org.jetbrains.annotations.NotNull;
17
18 import java.util.*;
19
20 public class StudySubtaskUtils {
21   private StudySubtaskUtils() {
22   }
23
24   /***
25    * @param toSubtaskIndex from 0 to subtaskNum - 1
26    */
27   public static void switchStep(@NotNull Project project, @NotNull Task task, int toSubtaskIndex) {
28     if (toSubtaskIndex == task.getActiveSubtaskIndex()) {
29       return;
30     }
31     VirtualFile taskDir = task.getTaskDir(project);
32     if (taskDir == null) {
33       return;
34     }
35     VirtualFile srcDir = taskDir.findChild(EduNames.SRC);
36     if (srcDir != null) {
37       taskDir = srcDir;
38     }
39     int fromSubtaskIndex = task.getActiveSubtaskIndex();
40     for (Map.Entry<String, TaskFile> entry : task.getTaskFiles().entrySet()) {
41       String name = entry.getKey();
42       VirtualFile virtualFile = taskDir.findChild(name);
43       if (virtualFile == null) {
44         continue;
45       }
46       TaskFile taskFile = entry.getValue();
47       Document document = FileDocumentManager.getInstance().getDocument(virtualFile);
48       if (document == null) {
49         continue;
50       }
51       updatePlaceholderTexts(project, document, taskFile, fromSubtaskIndex, toSubtaskIndex);
52       EditorNotifications.getInstance(project).updateNotifications(virtualFile);
53       if (StudyUtils.isStudyProject(project)) {
54         WolfTheProblemSolver.getInstance(project).clearProblems(virtualFile);
55         taskFile.setHighlightErrors(false);
56       }
57     }
58     task.setActiveSubtaskIndex(toSubtaskIndex);
59     update(project, task, taskDir);
60   }
61
62   private static void update(@NotNull Project project, @NotNull Task task, VirtualFile taskDir) {
63     StudyCheckUtils.drawAllPlaceholders(project, task, taskDir);
64     ProjectView.getInstance(project).refresh();
65     StudyToolWindow toolWindow = StudyUtils.getStudyToolWindow(project);
66     if (toolWindow != null) {
67       String text = StudyUtils.getTaskTextFromTask(taskDir, task);
68       if (text == null) {
69         toolWindow.setEmptyText(project);
70         return;
71       }
72       toolWindow.setTaskText(text, taskDir, project);
73     }
74   }
75
76   private static void updatePlaceholderTexts(@NotNull Project project,
77                                              @NotNull Document document,
78                                              @NotNull TaskFile taskFile,
79                                              int fromSubtaskIndex,
80                                              int toSubtaskIndex) {
81     taskFile.setTrackLengths(false);
82     for (AnswerPlaceholder placeholder : taskFile.getAnswerPlaceholders()) {
83       placeholder.switchSubtask(project, document, fromSubtaskIndex, toSubtaskIndex);
84     }
85     taskFile.setTrackLengths(true);
86   }
87 }