draw placeholders correctly when switching subtask
[idea/community.git] / python / educational-core / student / src / com / jetbrains / edu / learning / StudySubtaskUtils.java
1 package com.jetbrains.edu.learning;
2
3 import com.intellij.ide.projectView.ProjectView;
4 import com.intellij.openapi.editor.Document;
5 import com.intellij.openapi.fileEditor.FileDocumentManager;
6 import com.intellij.openapi.project.Project;
7 import com.intellij.openapi.vfs.VirtualFile;
8 import com.intellij.ui.EditorNotifications;
9 import com.intellij.util.containers.ContainerUtil;
10 import com.jetbrains.edu.learning.checker.StudyCheckUtils;
11 import com.jetbrains.edu.learning.core.EduNames;
12 import com.jetbrains.edu.learning.core.EduUtils;
13 import com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder;
14 import com.jetbrains.edu.learning.courseFormat.AnswerPlaceholderSubtaskInfo;
15 import com.jetbrains.edu.learning.courseFormat.Task;
16 import com.jetbrains.edu.learning.courseFormat.TaskFile;
17 import com.jetbrains.edu.learning.ui.StudyToolWindow;
18 import org.jetbrains.annotations.NotNull;
19
20 import java.util.*;
21
22 public class StudySubtaskUtils {
23   private StudySubtaskUtils() {
24   }
25
26   /***
27    * @param toSubtaskIndex from 0 to subtaskNum - 1
28    */
29   public static void switchStep(@NotNull Project project, @NotNull Task task, int toSubtaskIndex) {
30     if (toSubtaskIndex == task.getActiveSubtaskIndex()) {
31       return;
32     }
33     VirtualFile taskDir = task.getTaskDir(project);
34     if (taskDir == null) {
35       return;
36     }
37     VirtualFile srcDir = taskDir.findChild(EduNames.SRC);
38     if (srcDir != null) {
39       taskDir = srcDir;
40     }
41     int fromSubtaskIndex = task.getActiveSubtaskIndex();
42     for (Map.Entry<String, TaskFile> entry : task.getTaskFiles().entrySet()) {
43       String name = entry.getKey();
44       VirtualFile virtualFile = taskDir.findChild(name);
45       if (virtualFile == null) {
46         continue;
47       }
48       TaskFile taskFile = entry.getValue();
49       updatePlaceholderTexts(project, virtualFile, taskFile, fromSubtaskIndex, toSubtaskIndex);
50       EditorNotifications.getInstance(project).updateNotifications(virtualFile);
51     }
52     task.setActiveSubtaskIndex(toSubtaskIndex);
53     update(project, task, taskDir);
54   }
55
56   private static void update(@NotNull Project project, @NotNull Task task, VirtualFile taskDir) {
57     StudyCheckUtils.drawAllPlaceholders(project, task, taskDir);
58     ProjectView.getInstance(project).refresh();
59     StudyToolWindow toolWindow = StudyUtils.getStudyToolWindow(project);
60     if (toolWindow != null) {
61       String text = StudyUtils.getTaskTextFromTask(taskDir, task);
62       if (text == null) {
63         toolWindow.setEmptyText(project);
64         return;
65       }
66       toolWindow.setTaskText(text, taskDir, project);
67     }
68   }
69
70   private static void updatePlaceholderTexts(@NotNull Project project,
71                                              @NotNull VirtualFile virtualFile,
72                                              @NotNull TaskFile taskFile,
73                                              int fromSubtaskIndex,
74                                              int toSubtaskIndex) {
75     Document document = FileDocumentManager.getInstance().getDocument(virtualFile);
76     if (document == null) {
77       return;
78     }
79     taskFile.setTrackLengths(false);
80     for (AnswerPlaceholder placeholder : taskFile.getAnswerPlaceholders()) {
81       Set<Integer> indexes = placeholder.getSubtaskInfos().keySet();
82       Integer minIndex = Collections.min(indexes);
83       int visibleLength = placeholder.getVisibleLength(fromSubtaskIndex);
84       if (indexes.contains(toSubtaskIndex) && indexes.contains(fromSubtaskIndex)) {
85         if (!placeholder.getUseLength()) {
86           String replacementText = placeholder.getSubtaskInfos().get(toSubtaskIndex).getPossibleAnswer();
87           EduUtils.replaceAnswerPlaceholder(project, document, placeholder, visibleLength, replacementText);
88         }
89         continue;
90       }
91       if (fromSubtaskIndex < toSubtaskIndex) {
92         if (minIndex > fromSubtaskIndex && minIndex <= toSubtaskIndex) {
93           Integer maxIndex = Collections.max(ContainerUtil.filter(indexes, integer -> integer <= toSubtaskIndex));
94           AnswerPlaceholderSubtaskInfo maxInfo = placeholder.getSubtaskInfos().get(maxIndex);
95           String replacementText = placeholder.getUseLength() ? maxInfo.getPlaceholderText() : maxInfo.getPossibleAnswer();
96           EduUtils.replaceAnswerPlaceholder(project, document, placeholder, visibleLength, replacementText);
97         }
98       }
99       else {
100         if (minIndex > toSubtaskIndex && minIndex <= fromSubtaskIndex) {
101           AnswerPlaceholderSubtaskInfo minInfo = placeholder.getSubtaskInfos().get(minIndex);
102           if (minInfo.isNeedInsertText()) {
103             EduUtils.replaceAnswerPlaceholder(project, document, placeholder, visibleLength, "");
104           }
105           else {
106             String replacementText = minInfo.getPlaceholderText();
107             EduUtils.replaceAnswerPlaceholder(project, document, placeholder, visibleLength, replacementText);
108           }
109         }
110       }
111     }
112     taskFile.setTrackLengths(true);
113   }
114 }