do not show errors when switching to the next subtask
[idea/community.git] / python / educational-core / student / src / com / jetbrains / edu / learning / StudySubtaskUtils.java
index 81072abf255db65ce18a92b17f23f3c5a336e5f3..895acdba3c2c691e93e74a7005104442ca869dc7 100644 (file)
@@ -1,17 +1,21 @@
 package com.jetbrains.edu.learning;
 
 import com.intellij.ide.projectView.ProjectView;
+import com.intellij.openapi.editor.Document;
+import com.intellij.openapi.fileEditor.FileDocumentManager;
 import com.intellij.openapi.project.Project;
 import com.intellij.openapi.vfs.VirtualFile;
+import com.intellij.problems.WolfTheProblemSolver;
 import com.intellij.ui.EditorNotifications;
 import com.jetbrains.edu.learning.checker.StudyCheckUtils;
 import com.jetbrains.edu.learning.core.EduNames;
+import com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder;
 import com.jetbrains.edu.learning.courseFormat.Task;
 import com.jetbrains.edu.learning.courseFormat.TaskFile;
 import com.jetbrains.edu.learning.ui.StudyToolWindow;
 import org.jetbrains.annotations.NotNull;
 
-import java.util.Map;
+import java.util.*;
 
 public class StudySubtaskUtils {
   private StudySubtaskUtils() {
@@ -21,6 +25,9 @@ public class StudySubtaskUtils {
    * @param toSubtaskIndex from 0 to subtaskNum - 1
    */
   public static void switchStep(@NotNull Project project, @NotNull Task task, int toSubtaskIndex) {
+    if (toSubtaskIndex == task.getActiveSubtaskIndex()) {
+      return;
+    }
     VirtualFile taskDir = task.getTaskDir(project);
     if (taskDir == null) {
       return;
@@ -29,13 +36,24 @@ public class StudySubtaskUtils {
     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.findChild(name);
       if (virtualFile == null) {
         continue;
       }
+      TaskFile taskFile = entry.getValue();
+      Document document = FileDocumentManager.getInstance().getDocument(virtualFile);
+      if (document == null) {
+        continue;
+      }
+      updatePlaceholderTexts(project, document, taskFile, fromSubtaskIndex, toSubtaskIndex);
       EditorNotifications.getInstance(project).updateNotifications(virtualFile);
+      if (StudyUtils.isStudyProject(project)) {
+        WolfTheProblemSolver.getInstance(project).clearProblems(virtualFile);
+        taskFile.setHighlightErrors(false);
+      }
     }
     task.setActiveSubtaskIndex(toSubtaskIndex);
     update(project, task, taskDir);
@@ -54,4 +72,16 @@ public class StudySubtaskUtils {
       toolWindow.setTaskText(text, taskDir, project);
     }
   }
+
+  private static void updatePlaceholderTexts(@NotNull Project project,
+                                             @NotNull Document document,
+                                             @NotNull TaskFile taskFile,
+                                             int fromSubtaskIndex,
+                                             int toSubtaskIndex) {
+    taskFile.setTrackLengths(false);
+    for (AnswerPlaceholder placeholder : taskFile.getAnswerPlaceholders()) {
+      placeholder.switchSubtask(project, document, fromSubtaskIndex, toSubtaskIndex);
+    }
+    taskFile.setTrackLengths(true);
+  }
 }
\ No newline at end of file