delete subtask
[idea/community.git] / python / educational-core / student / src / com / jetbrains / edu / learning / StudySubtaskUtils.java
index 45499d5aa637d4bb5a08db9798501d03bf4e2651..2f2c2c2111929d96f2f25aac1ff2ec1c60b71848 100644 (file)
@@ -1,25 +1,30 @@
 package com.jetbrains.edu.learning;
 
 import com.intellij.ide.projectView.ProjectView;
+import com.intellij.openapi.application.ApplicationManager;
+import com.intellij.openapi.diagnostic.Logger;
 import com.intellij.openapi.editor.Document;
+import com.intellij.openapi.editor.Editor;
 import com.intellij.openapi.fileEditor.FileDocumentManager;
 import com.intellij.openapi.project.Project;
+import com.intellij.openapi.util.io.FileUtil;
+import com.intellij.openapi.util.io.FileUtilRt;
 import com.intellij.openapi.vfs.VirtualFile;
+import com.intellij.problems.WolfTheProblemSolver;
 import com.intellij.ui.EditorNotifications;
-import com.intellij.util.containers.ContainerUtil;
 import com.jetbrains.edu.learning.checker.StudyCheckUtils;
 import com.jetbrains.edu.learning.core.EduNames;
 import com.jetbrains.edu.learning.core.EduUtils;
-import com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder;
-import com.jetbrains.edu.learning.courseFormat.AnswerPlaceholderSubtaskInfo;
-import com.jetbrains.edu.learning.courseFormat.Task;
-import com.jetbrains.edu.learning.courseFormat.TaskFile;
+import com.jetbrains.edu.learning.courseFormat.*;
 import com.jetbrains.edu.learning.ui.StudyToolWindow;
 import org.jetbrains.annotations.NotNull;
 
-import java.util.*;
+import java.io.IOException;
+import java.util.Map;
 
 public class StudySubtaskUtils {
+  private static final Logger LOG = Logger.getInstance(StudySubtaskUtils.class);
+
   private StudySubtaskUtils() {
   }
 
@@ -52,12 +57,43 @@ public class StudySubtaskUtils {
       }
       updatePlaceholderTexts(project, document, taskFile, fromSubtaskIndex, toSubtaskIndex);
       EditorNotifications.getInstance(project).updateNotifications(virtualFile);
+      if (StudyUtils.isStudentProject(project)) {
+        WolfTheProblemSolver.getInstance(project).clearProblems(virtualFile);
+        taskFile.setHighlightErrors(false);
+      }
     }
+    transformTestFile(project, toSubtaskIndex, taskDir);
     task.setActiveSubtaskIndex(toSubtaskIndex);
-    update(project, task, taskDir);
+    updateUI(project, task, taskDir);
+  }
+
+  private static void transformTestFile(@NotNull Project project, int toSubtaskIndex, VirtualFile taskDir) {
+    Course course = StudyTaskManager.getInstance(project).getCourse();
+    if (course == null) {
+      return;
+    }
+    StudyLanguageManager manager = StudyUtils.getLanguageManager(course);
+    if (manager == null) {
+      return;
+    }
+    String defaultTestFileName = manager.getTestFileName();
+    String nameWithoutExtension = FileUtil.getNameWithoutExtension(defaultTestFileName);
+    String extension = FileUtilRt.getExtension(defaultTestFileName);
+    String subtaskTestFileName = nameWithoutExtension + EduNames.SUBTASK_MARKER + toSubtaskIndex;
+    VirtualFile subtaskTestFile = taskDir.findChild(subtaskTestFileName + ".txt");
+    if (subtaskTestFile != null) {
+      ApplicationManager.getApplication().runWriteAction(() -> {
+        try {
+          subtaskTestFile.rename(project, subtaskTestFileName + "." + extension);
+        }
+        catch (IOException e) {
+          LOG.error(e);
+        }
+      });
+    }
   }
 
-  private static void update(@NotNull Project project, @NotNull Task task, VirtualFile taskDir) {
+  public static void updateUI(@NotNull Project project, @NotNull Task task, VirtualFile taskDir) {
     StudyCheckUtils.drawAllPlaceholders(project, task, taskDir);
     ProjectView.getInstance(project).refresh();
     StudyToolWindow toolWindow = StudyUtils.getStudyToolWindow(project);
@@ -71,52 +107,22 @@ public class StudySubtaskUtils {
     }
   }
 
-  public static void updatePlaceholderTexts(@NotNull Project 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()) {
-      AnswerPlaceholderSubtaskInfo fromSubtaskInfo = placeholder.getSubtaskInfos().get(fromSubtaskIndex);
-      if (fromSubtaskIndex == toSubtaskIndex && fromSubtaskInfo != null) {
-        String placeholderText = fromSubtaskInfo.getPlaceholderText();
-        if (placeholderText != null) {
-          EduUtils.replaceAnswerPlaceholder(project, document, placeholder, placeholder.getRealLength(), placeholderText);
-        }
-        continue;
-      }
-      Set<Integer> indexes = placeholder.getSubtaskInfos().keySet();
-      Integer minIndex = Collections.min(indexes);
-      int visibleLength = placeholder.getVisibleLength(fromSubtaskIndex);
-      if (indexes.contains(toSubtaskIndex) && indexes.contains(fromSubtaskIndex)) {
-        if (!placeholder.getUseLength()) {
-          String replacementText = placeholder.getSubtaskInfos().get(toSubtaskIndex).getPossibleAnswer();
-          EduUtils.replaceAnswerPlaceholder(project, document, placeholder, visibleLength, replacementText);
-        }
-        continue;
-      }
-      if (fromSubtaskIndex < toSubtaskIndex) {
-        if (minIndex > fromSubtaskIndex && minIndex <= toSubtaskIndex) {
-          Integer maxIndex = Collections.max(ContainerUtil.filter(indexes, integer -> integer <= toSubtaskIndex));
-          AnswerPlaceholderSubtaskInfo maxInfo = placeholder.getSubtaskInfos().get(maxIndex);
-          String replacementText = placeholder.getUseLength() ? maxInfo.getPlaceholderText() : maxInfo.getPossibleAnswer();
-          EduUtils.replaceAnswerPlaceholder(project, document, placeholder, visibleLength, replacementText);
-        }
-      }
-      else {
-        if (minIndex > toSubtaskIndex && minIndex <= fromSubtaskIndex) {
-          AnswerPlaceholderSubtaskInfo minInfo = placeholder.getSubtaskInfos().get(minIndex);
-          if (minInfo.isNeedInsertText()) {
-            EduUtils.replaceAnswerPlaceholder(project, document, placeholder, visibleLength, "");
-          }
-          else {
-            String replacementText = minInfo.getPlaceholderText();
-            EduUtils.replaceAnswerPlaceholder(project, document, placeholder, visibleLength, replacementText);
-          }
-        }
-      }
+      placeholder.switchSubtask(project, document, fromSubtaskIndex, toSubtaskIndex);
     }
     taskFile.setTrackLengths(true);
   }
+
+  public static void refreshPlaceholder(@NotNull Project project, @NotNull Editor editor, @NotNull AnswerPlaceholder placeholder) {
+    int prevSubtaskIndex = placeholder.getActiveSubtaskIndex() - 1;
+    AnswerPlaceholderSubtaskInfo info = placeholder.getSubtaskInfos().get(prevSubtaskIndex);
+    String replacementText = info != null ? info.getAnswer() : placeholder.getTaskText();
+    EduUtils.replaceAnswerPlaceholder(project, editor.getDocument(), placeholder, placeholder.getRealLength(), replacementText);
+  }
 }
\ No newline at end of file