refresh task file for subtasks
authorLiana.Bakradze <liana.bakradze@jetbrains.com>
Mon, 3 Oct 2016 09:48:13 +0000 (12:48 +0300)
committerliana.bakradze <liana.bakradze@jetbrains.com>
Thu, 17 Nov 2016 14:08:25 +0000 (17:08 +0300)
python/educational-core/student/src/com/jetbrains/edu/learning/actions/StudyRefreshTaskFileAction.java
python/educational-core/student/src/com/jetbrains/edu/learning/checker/StudyCheckTask.java
python/educational-core/student/src/com/jetbrains/edu/learning/courseFormat/AnswerPlaceholderSubtaskInfo.java

index 59b89211bdbe1ca0ed3f0130ff2feb41f943fb7c..2a1f54ad2cb1024f9e1c8bcb3a67452e4cd5ea6a 100644 (file)
@@ -26,10 +26,8 @@ import com.jetbrains.edu.learning.StudyTaskManager;
 import com.jetbrains.edu.learning.StudyUtils;
 import com.jetbrains.edu.learning.core.EduAnswerPlaceholderPainter;
 import com.jetbrains.edu.learning.core.EduNames;
-import com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder;
-import com.jetbrains.edu.learning.courseFormat.Course;
-import com.jetbrains.edu.learning.courseFormat.StudyStatus;
-import com.jetbrains.edu.learning.courseFormat.TaskFile;
+import com.jetbrains.edu.learning.core.EduUtils;
+import com.jetbrains.edu.learning.courseFormat.*;
 import com.jetbrains.edu.learning.editor.StudyEditor;
 import com.jetbrains.edu.learning.navigation.StudyNavigator;
 import icons.InteractiveLearningIcons;
@@ -44,7 +42,8 @@ public class StudyRefreshTaskFileAction extends StudyActionWithShortcut {
   private static final Logger LOG = Logger.getInstance(StudyRefreshTaskFileAction.class.getName());
 
   public StudyRefreshTaskFileAction() {
-    super("Reset Task File (" + KeymapUtil.getShortcutText(new KeyboardShortcut(KeyStroke.getKeyStroke(SHORTCUT), null)) + ")", "Refresh current task",
+    super("Reset Task File (" + KeymapUtil.getShortcutText(new KeyboardShortcut(KeyStroke.getKeyStroke(SHORTCUT), null)) + ")",
+          "Refresh current task",
           InteractiveLearningIcons.ResetTaskFile);
   }
 
@@ -63,9 +62,19 @@ public class StudyRefreshTaskFileAction extends StudyActionWithShortcut {
   private static void refreshFile(@NotNull final StudyState studyState, @NotNull final Project project) {
     final Editor editor = studyState.getEditor();
     final TaskFile taskFile = studyState.getTaskFile();
-    if (!resetTaskFile(editor.getDocument(), project, taskFile, studyState.getVirtualFile().getName())) {
-      Messages.showInfoMessage("The initial text of task file is unavailable", "Failed to Refresh Task File");
-      return;
+    if (taskFile.getTask().hasSubtasks()) {
+      int prevSubtaskIndex = taskFile.getTask().getActiveSubtaskIndex() - 1;
+      for (AnswerPlaceholder placeholder : taskFile.getActivePlaceholders()) {
+        AnswerPlaceholderSubtaskInfo info = placeholder.getSubtaskInfos().get(prevSubtaskIndex);
+        String replacementText = info != null ? info.getAnswer() : placeholder.getTaskText();
+        EduUtils.replaceAnswerPlaceholder(project, editor.getDocument(), placeholder, placeholder.getRealLength(), replacementText);
+      }
+    }
+    else {
+      if (!resetTaskFile(editor.getDocument(), project, taskFile, studyState.getVirtualFile().getName())) {
+        Messages.showInfoMessage("The initial text of task file is unavailable", "Failed to Refresh Task File");
+        return;
+      }
     }
     WolfTheProblemSolver.getInstance(project).clearProblems(studyState.getVirtualFile());
     taskFile.setHighlightErrors(false);
index 168012eef90aceccc17969aadd83e79833d55492..b38f438a078dca52f122d385b4799138c0ef08d1 100644 (file)
@@ -5,24 +5,27 @@ import com.intellij.execution.process.ProcessOutput;
 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.fileEditor.FileDocumentManager;
 import com.intellij.openapi.progress.ProgressIndicator;
 import com.intellij.openapi.project.Project;
 import com.intellij.openapi.ui.MessageType;
 import com.intellij.openapi.util.Pair;
 import com.intellij.openapi.util.Ref;
+import com.intellij.openapi.util.TextRange;
 import com.intellij.openapi.vfs.VirtualFile;
 import com.jetbrains.edu.learning.*;
 import com.jetbrains.edu.learning.actions.StudyAfterCheckAction;
 import com.jetbrains.edu.learning.core.EduNames;
 import com.jetbrains.edu.learning.core.EduUtils;
-import com.jetbrains.edu.learning.courseFormat.Course;
-import com.jetbrains.edu.learning.courseFormat.StudyStatus;
-import com.jetbrains.edu.learning.courseFormat.Task;
+import com.jetbrains.edu.learning.courseFormat.*;
 import com.jetbrains.edu.learning.stepic.EduAdaptiveStepicConnector;
 import com.jetbrains.edu.learning.stepic.EduStepicConnector;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
 
+import java.util.Map;
+
 public class StudyCheckTask extends com.intellij.openapi.progress.Task.Backgroundable {
 
   private static final Logger LOG = Logger.getInstance(StudyCheckTask.class);
@@ -192,13 +195,44 @@ public class StudyCheckTask extends com.intellij.openapi.progress.Task.Backgroun
           String resultMessage = !hasMoreSubtasks ? message : "Subtask " + visibleSubtaskIndex + "/" + myTask.getSubtaskNum() + " solved";
           StudyCheckUtils.showTestResultPopUp(resultMessage, MessageType.INFO.getPopupBackground(), myProject);
           if (hasMoreSubtasks) {
-            StudySubtaskUtils.switchStep(myProject, myTask, myTask.getActiveSubtaskIndex() + 1);
+            int nextSubtaskIndex = myTask.getActiveSubtaskIndex() + 1;
+            StudySubtaskUtils.switchStep(myProject, myTask, nextSubtaskIndex);
+            rememberAnswers(nextSubtaskIndex);
           }
         });
       }
     }
   }
 
+  private void rememberAnswers(int nextSubtaskIndex) {
+    VirtualFile taskDir = myTask.getTaskDir(myProject);
+    if (taskDir == null) {
+      return;
+    }
+    VirtualFile srcDir = taskDir.findChild(EduNames.SRC);
+    if (srcDir != null) {
+      taskDir = srcDir;
+    }
+    for (Map.Entry<String, TaskFile> entry : myTask.getTaskFiles().entrySet()) {
+      TaskFile taskFile = entry.getValue();
+      VirtualFile virtualFile = taskDir.findChild(entry.getKey());
+      if (virtualFile == null) {
+        continue;
+      }
+      Document document = FileDocumentManager.getInstance().getDocument(virtualFile);
+      if (document == null) {
+        continue;
+      }
+      for (AnswerPlaceholder placeholder : taskFile.getActivePlaceholders()) {
+        if (placeholder.getSubtaskInfos().containsKey(nextSubtaskIndex - 1)) {
+          int offset = placeholder.getOffset();
+          String answer = document.getText(TextRange.create(offset, offset + placeholder.getRealLength()));
+          placeholder.getSubtaskInfos().get(nextSubtaskIndex - 1).setAnswer(answer);
+        }
+      }
+    }
+  }
+
   private void runAfterTaskCheckedActions() {
     StudyPluginConfigurator configurator = StudyUtils.getConfigurator(myProject);
     if (configurator != null) {
index f7f3d2b859646854878b3bf2d9b197bddbe05871..58703fd809d3408765b7cb2c5601bf2dabe44d73 100644 (file)
@@ -23,6 +23,8 @@ public class AnswerPlaceholderSubtaskInfo {
   @Expose private String possibleAnswer = "";
 
   @Expose private String myPlaceholderText;
+
+  private String myAnswer = "";
   private boolean mySelected = false;
   private StudyStatus myStatus = StudyStatus.Unchecked;
   @Expose private boolean myHasFrame = true;
@@ -136,4 +138,12 @@ public class AnswerPlaceholderSubtaskInfo {
   public void setNeedInsertText(boolean needInsertText) {
     myNeedInsertText = needInsertText;
   }
+
+  public String getAnswer() {
+    return myAnswer;
+  }
+
+  public void setAnswer(String answer) {
+    myAnswer = answer;
+  }
 }