update task description toolwindow when switching between subtasks
authorLiana Bakradze <liana.bakradze@jetbrains.com>
Thu, 22 Sep 2016 14:49:02 +0000 (17:49 +0300)
committerliana.bakradze <liana.bakradze@jetbrains.com>
Thu, 17 Nov 2016 14:07:26 +0000 (17:07 +0300)
python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/actions/CCEditTaskTextAction.java
python/educational-core/student/src/com/jetbrains/edu/learning/StudyUtils.java
python/educational-core/student/src/com/jetbrains/edu/learning/courseFormat/Task.java
python/educational-core/student/src/com/jetbrains/edu/learning/ui/StudyToolWindow.java

index 5786ebe51199b1fc333052c591f9c1bfbf6405a8..fe4ae15c8de3c8b67625594ec010ab9e28f71e1d 100644 (file)
@@ -50,7 +50,7 @@ public class CCEditTaskTextAction extends ToggleAction implements DumbAware {
       return;
     }
     final StudyState studyState = new StudyState(selectedEditor);
-    VirtualFile taskTextFile = StudyUtils.findTaskDescriptionVirtualFile(studyState.getTaskDir());
+    VirtualFile taskTextFile = StudyUtils.findTaskDescriptionVirtualFile(project, studyState.getTaskDir());
     if (taskTextFile == null) {
       LOG.info("Failed to find task.html");
       return;
index 5ab871aee27eccb391b17f846343aef316264ff2..450047de2084f9a6f4ff73d2796f5d41bff48de6 100644 (file)
@@ -483,17 +483,37 @@ public class StudyUtils {
     }
     final Course course = task.getLesson().getCourse();
     String text = task.getText() != null ? task.getText() : getTaskTextFromTaskName(taskDirectory, EduNames.TASK_MD);
-    
+
     if (text == null) return null;
     if (course.isAdaptive()) text = wrapAdaptiveCourseText(text);
 
-    return wrapTextToDisplayLatex(text);
+    if (text != null && !text.isEmpty()) {
+      return wrapTextToDisplayLatex(text);
+    }
+    if (taskDirectory != null) {
+      String fileNameWithoutExtension = FileUtil.getNameWithoutExtension(EduNames.TASK_HTML);
+      int activeStepIndex = task.getActiveSubtaskIndex();
+      if (activeStepIndex != 0) {
+        fileNameWithoutExtension += EduNames.SUBTASK_MARKER + activeStepIndex;
+      }
+      final String taskTextFileHtml = getTaskTextFromTaskName(taskDirectory, getTaskDescriptionName(fileNameWithoutExtension, EduNames.TASK_HTML));
+      if (taskTextFileHtml != null) return wrapTextToDisplayLatex(taskTextFileHtml);
+
+      final String taskTextFileMd = getTaskTextFromTaskName(taskDirectory, getTaskDescriptionName(fileNameWithoutExtension, EduNames.TASK_MD));
+      if (taskTextFileMd != null) return wrapTextToDisplayLatex(convertToHtml(taskTextFileMd));      
+    }
+    return null;
   }
 
   private static String wrapAdaptiveCourseText(@NotNull String text) {
     return text + "\n\n<b>Note</b>: Use standard input to obtain input for the task.";
   }
 
+  @NotNull
+  private static String getTaskDescriptionName(String fileNameWithoutExtension, String defaultName) {
+    return fileNameWithoutExtension + "." + FileUtilRt.getExtension(defaultName);
+  }
+
   public static String wrapTextToDisplayLatex(String taskTextFileHtml) {
     final String prefix = String.format(ourPrefix, EditorColorsManager.getInstance().getGlobalScheme().getEditorFontSize());
     return prefix + taskTextFileHtml + ourPostfix;
@@ -503,7 +523,7 @@ public class StudyUtils {
   private static String getTaskTextFromTaskName(@Nullable VirtualFile taskDirectory, @NotNull String taskTextFilename) {
     if (taskDirectory == null) return null;
     taskDirectory.refresh(false, true);
-    VirtualFile taskTextFile = ObjectUtils.chooseNotNull(taskDirectory.findChild(EduNames.TASK_HTML), 
+    VirtualFile taskTextFile = ObjectUtils.chooseNotNull(taskDirectory.findChild(EduNames.TASK_HTML),
                                                          taskDirectory.findChild(EduNames.TASK_MD));
     if (taskTextFile == null) {
       VirtualFile srcDir = taskDirectory.findChild(EduNames.SRC);
@@ -702,8 +722,18 @@ public class StudyUtils {
   }
   
   @Nullable
-  public static VirtualFile findTaskDescriptionVirtualFile(@NotNull VirtualFile taskDir) {
-    return ObjectUtils.chooseNotNull(taskDir.findChild(EduNames.TASK_HTML), taskDir.findChild(EduNames.TASK_MD));
+  public static VirtualFile findTaskDescriptionVirtualFile(@NotNull Project project, @NotNull VirtualFile taskDir) {
+    Task task = getTask(project, taskDir.getName().contains(EduNames.TASK) ? taskDir: taskDir.getParent());
+    if (task == null) {
+      return null;
+    }
+    String fileNameWithoutExtension  = FileUtil.getNameWithoutExtension(EduNames.TASK_HTML);
+    int activeStepIndex = task.getActiveSubtaskIndex();
+    if (activeStepIndex != 0) {
+      fileNameWithoutExtension += EduNames.SUBTASK_MARKER + activeStepIndex;
+    }
+    return ObjectUtils.chooseNotNull(taskDir.findChild(getTaskDescriptionName(fileNameWithoutExtension, EduNames.TASK_HTML)),
+                                     taskDir.findChild(getTaskDescriptionName(fileNameWithoutExtension, EduNames.TASK_MD)));
   }
   
   @NotNull
index f9c804bca5fe85ab654d64a68cf2e8d5f0c95065..4d582f6016161406d053b974adb1f9184f132f47 100644 (file)
@@ -155,7 +155,7 @@ public class Task implements StudyItem {
     if (!StringUtil.isEmptyOrSpaces(text)) return text;
     final VirtualFile taskDir = getTaskDir(project);
     if (taskDir != null) {
-      final VirtualFile file = StudyUtils.findTaskDescriptionVirtualFile(taskDir);
+      final VirtualFile file = StudyUtils.findTaskDescriptionVirtualFile(project, taskDir);
       if (file == null) return "";
       final Document document = FileDocumentManager.getInstance().getDocument(file);
       if (document != null) {
index 545be6aface75fe46d056ad45aa6347be0f61a7c..e9b8b4fc8873aa1bf79efb380fb959857058dc47 100644 (file)
@@ -211,12 +211,12 @@ public abstract class StudyToolWindow extends SimpleToolWindowPanel implements D
         LOG.info("Failed to enter editing mode for StudyToolWindow");
         return;
       }
-      VirtualFile taskTextFile = StudyUtils.findTaskDescriptionVirtualFile(taskDirectory);
+      VirtualFile taskTextFile = StudyUtils.findTaskDescriptionVirtualFile(project, taskDirectory);
       enterEditingMode(taskTextFile, project);
       StudyTaskManager.getInstance(project).setTurnEditingMode(false);
     }
     if (taskDirectory != null && StudyTaskManager.getInstance(project).getToolWindowMode() == StudyToolWindowMode.EDITING) {
-      VirtualFile taskTextFile = StudyUtils.findTaskDescriptionVirtualFile(taskDirectory);
+      VirtualFile taskTextFile = StudyUtils.findTaskDescriptionVirtualFile(project, taskDirectory);
       enterEditingMode(taskTextFile, project);
     }
     else {