separate switch step and student file creation
authorLiana.Bakradze <liana.bakradze@jetbrains.com>
Fri, 30 Sep 2016 14:21:46 +0000 (17:21 +0300)
committerliana.bakradze <liana.bakradze@jetbrains.com>
Thu, 17 Nov 2016 14:08:23 +0000 (17:08 +0300)
python/educational-core/student/src/com/jetbrains/edu/learning/StudySubtaskUtils.java
python/educational-core/student/src/com/jetbrains/edu/learning/core/EduUtils.java
python/educational-core/student/src/com/jetbrains/edu/learning/courseFormat/AnswerPlaceholder.java

index 45499d5aa637d4bb5a08db9798501d03bf4e2651..3debdae673392317fd900abfb0c4b327d997dafd 100644 (file)
@@ -78,44 +78,7 @@ public class StudySubtaskUtils {
                                              int toSubtaskIndex) {
     taskFile.setTrackLengths(false);
     for (AnswerPlaceholder placeholder : taskFile.getAnswerPlaceholders()) {
                                              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);
   }
     }
     taskFile.setTrackLengths(true);
   }
index 276c1988f73d65e285ef0a9efaa31c7796b28e77..64b5572fbfe12a04b62184dc915969250341dba3 100644 (file)
@@ -20,7 +20,7 @@ import com.intellij.openapi.vfs.VfsUtilCore;
 import com.intellij.openapi.vfs.VirtualFile;
 import com.intellij.openapi.vfs.VirtualFileManager;
 import com.intellij.psi.PsiDirectory;
 import com.intellij.openapi.vfs.VirtualFile;
 import com.intellij.openapi.vfs.VirtualFileManager;
 import com.intellij.psi.PsiDirectory;
-import com.jetbrains.edu.learning.StudySubtaskUtils;
+import com.intellij.util.containers.ContainerUtil;
 import com.jetbrains.edu.learning.StudyUtils;
 import com.jetbrains.edu.learning.courseFormat.*;
 import org.jetbrains.annotations.NonNls;
 import com.jetbrains.edu.learning.StudyUtils;
 import com.jetbrains.edu.learning.courseFormat.*;
 import org.jetbrains.annotations.NonNls;
@@ -164,11 +164,35 @@ public class EduUtils {
     }
     EduDocumentListener listener = new EduDocumentListener(taskFile, false);
     studentDocument.addDocumentListener(listener);
     }
     EduDocumentListener listener = new EduDocumentListener(taskFile, false);
     studentDocument.addDocumentListener(listener);
-    StudySubtaskUtils.updatePlaceholderTexts(project, studentDocument, taskFile, task.getActiveSubtaskIndex(), toSubtaskIndex);
+    taskFile.setTrackLengths(false);
+    for (AnswerPlaceholder placeholder : taskFile.getAnswerPlaceholders()) {
+      int fromSubtask = task.getActiveSubtaskIndex();
+      placeholder.switchSubtask(project, studentDocument, fromSubtask, toSubtaskIndex);
+    }
+    for (AnswerPlaceholder placeholder : taskFile.getAnswerPlaceholders()) {
+      replaceWithTaskText(project, studentDocument, placeholder, toSubtaskIndex);
+    }
+    taskFile.setTrackChanges(true);
     studentDocument.removeDocumentListener(listener);
     return Pair.create(studentFile, taskFile);
   }
 
     studentDocument.removeDocumentListener(listener);
     return Pair.create(studentFile, taskFile);
   }
 
+  private static void replaceWithTaskText(Project project, Document studentDocument, AnswerPlaceholder placeholder, int toSubtaskIndex) {
+    AnswerPlaceholderSubtaskInfo info = placeholder.getSubtaskInfos().get(toSubtaskIndex);
+    if (info == null) {
+      return;
+    }
+    String replacementText;
+    if (Collections.min(placeholder.getSubtaskInfos().keySet()) == toSubtaskIndex) {
+      replacementText = info.getPlaceholderText();
+    }
+    else {
+      Integer max = Collections.max(ContainerUtil.filter(placeholder.getSubtaskInfos().keySet(), i -> i < toSubtaskIndex));
+      replacementText = placeholder.getSubtaskInfos().get(max).getPossibleAnswer();
+    }
+    replaceAnswerPlaceholder(project, studentDocument, placeholder, placeholder.getVisibleLength(toSubtaskIndex), replacementText);
+  }
+
   public static void replaceAnswerPlaceholder(@NotNull final Project project,
                                               @NotNull final Document document,
                                               @NotNull final AnswerPlaceholder answerPlaceholder,
   public static void replaceAnswerPlaceholder(@NotNull final Project project,
                                               @NotNull final Document document,
                                               @NotNull final AnswerPlaceholder answerPlaceholder,
index 556e592c648c963a8574078d014942a34a463109..2436ab4c5c03f79dc08ca25ced121c0ada0a0a56 100644 (file)
@@ -2,14 +2,18 @@ package com.jetbrains.edu.learning.courseFormat;
 
 import com.google.gson.annotations.Expose;
 import com.google.gson.annotations.SerializedName;
 
 import com.google.gson.annotations.Expose;
 import com.google.gson.annotations.SerializedName;
+import com.intellij.openapi.editor.Document;
+import com.intellij.openapi.project.Project;
 import com.intellij.util.containers.ContainerUtil;
 import com.intellij.util.containers.hash.HashMap;
 import com.intellij.util.xmlb.annotations.Transient;
 import com.intellij.util.containers.ContainerUtil;
 import com.intellij.util.containers.hash.HashMap;
 import com.intellij.util.xmlb.annotations.Transient;
+import com.jetbrains.edu.learning.core.EduUtils;
 import org.jetbrains.annotations.NotNull;
 
 import java.util.Collections;
 import java.util.List;
 import java.util.Map;
 import org.jetbrains.annotations.NotNull;
 
 import java.util.Collections;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 
 /**
  * Implementation of windows which user should type in
 
 /**
  * Implementation of windows which user should type in
@@ -237,4 +241,38 @@ public class AnswerPlaceholder {
     int maxIndex = Collections.max(ContainerUtil.filter(mySubtaskInfos.keySet(), i -> i < subtaskIndex));
     return getUseLength() ? length : mySubtaskInfos.get(maxIndex).getPossibleAnswer().length();
   }
     int maxIndex = Collections.max(ContainerUtil.filter(mySubtaskInfos.keySet(), i -> i < subtaskIndex));
     return getUseLength() ? length : mySubtaskInfos.get(maxIndex).getPossibleAnswer().length();
   }
+
+  public void switchSubtask(@NotNull Project project, @NotNull Document document, int fromSubtask, int toSubtask) {
+    Set<Integer> indexes = mySubtaskInfos.keySet();
+    int visibleLength = getVisibleLength(fromSubtask);
+    if (indexes.contains(fromSubtask) && indexes.contains(toSubtask)) {
+      if (!myUseLength) {
+        String replacementText = mySubtaskInfos.get(toSubtask).getPossibleAnswer();
+        EduUtils.replaceAnswerPlaceholder(project, document, this, visibleLength, replacementText);
+      }
+      return;
+    }
+    Integer minIndex = Collections.min(indexes);
+    if (fromSubtask < toSubtask) {
+      if (minIndex > fromSubtask && minIndex <= toSubtask) {
+        Integer maxIndex = Collections.max(ContainerUtil.filter(indexes, integer -> integer <= toSubtask));
+        AnswerPlaceholderSubtaskInfo maxInfo = mySubtaskInfos.get(maxIndex);
+        String replacementText = myUseLength ? maxInfo.getPlaceholderText() : maxInfo.getPossibleAnswer();
+        EduUtils.replaceAnswerPlaceholder(project, document, this, visibleLength, replacementText);
+        return;
+      }
+    }
+    if (fromSubtask > toSubtask) {
+      if (minIndex > toSubtask && minIndex <= fromSubtask) {
+        AnswerPlaceholderSubtaskInfo minInfo = mySubtaskInfos.get(minIndex);
+        if (minInfo.isNeedInsertText()) {
+          EduUtils.replaceAnswerPlaceholder(project, document, this, visibleLength, "");
+        }
+        else {
+          String replacementText = minInfo.getPlaceholderText();
+          EduUtils.replaceAnswerPlaceholder(project, document, this, visibleLength, replacementText);
+        }
+      }
+    }
+  }
 }
 }