exclude test files for future subtasks
[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.application.ApplicationManager;
5 import com.intellij.openapi.diagnostic.Logger;
6 import com.intellij.openapi.editor.Document;
7 import com.intellij.openapi.editor.Editor;
8 import com.intellij.openapi.fileEditor.FileDocumentManager;
9 import com.intellij.openapi.project.Project;
10 import com.intellij.openapi.util.io.FileUtil;
11 import com.intellij.openapi.util.io.FileUtilRt;
12 import com.intellij.openapi.vfs.VirtualFile;
13 import com.intellij.problems.WolfTheProblemSolver;
14 import com.intellij.ui.EditorNotifications;
15 import com.jetbrains.edu.learning.checker.StudyCheckUtils;
16 import com.jetbrains.edu.learning.core.EduNames;
17 import com.jetbrains.edu.learning.core.EduUtils;
18 import com.jetbrains.edu.learning.courseFormat.*;
19 import com.jetbrains.edu.learning.ui.StudyToolWindow;
20 import org.jetbrains.annotations.NotNull;
21
22 import java.io.IOException;
23 import java.util.Map;
24
25 public class StudySubtaskUtils {
26   private static final Logger LOG = Logger.getInstance(StudySubtaskUtils.class);
27
28   private StudySubtaskUtils() {
29   }
30
31   /***
32    * @param toSubtaskIndex from 0 to subtaskNum - 1
33    */
34   public static void switchStep(@NotNull Project project, @NotNull Task task, int toSubtaskIndex) {
35     if (toSubtaskIndex == task.getActiveSubtaskIndex()) {
36       return;
37     }
38     VirtualFile taskDir = task.getTaskDir(project);
39     if (taskDir == null) {
40       return;
41     }
42     VirtualFile srcDir = taskDir.findChild(EduNames.SRC);
43     if (srcDir != null) {
44       taskDir = srcDir;
45     }
46     int fromSubtaskIndex = task.getActiveSubtaskIndex();
47     for (Map.Entry<String, TaskFile> entry : task.getTaskFiles().entrySet()) {
48       String name = entry.getKey();
49       VirtualFile virtualFile = taskDir.findChild(name);
50       if (virtualFile == null) {
51         continue;
52       }
53       TaskFile taskFile = entry.getValue();
54       Document document = FileDocumentManager.getInstance().getDocument(virtualFile);
55       if (document == null) {
56         continue;
57       }
58       updatePlaceholderTexts(project, document, taskFile, fromSubtaskIndex, toSubtaskIndex);
59       EditorNotifications.getInstance(project).updateNotifications(virtualFile);
60       if (StudyUtils.isStudentProject(project)) {
61         WolfTheProblemSolver.getInstance(project).clearProblems(virtualFile);
62         taskFile.setHighlightErrors(false);
63       }
64     }
65     transformTestFile(project, toSubtaskIndex, taskDir);
66     task.setActiveSubtaskIndex(toSubtaskIndex);
67     updateUI(project, task, taskDir);
68   }
69
70   private static void transformTestFile(@NotNull Project project, int toSubtaskIndex, VirtualFile taskDir) {
71     Course course = StudyTaskManager.getInstance(project).getCourse();
72     if (course == null) {
73       return;
74     }
75     StudyLanguageManager manager = StudyUtils.getLanguageManager(course);
76     if (manager == null) {
77       return;
78     }
79     String defaultTestFileName = manager.getTestFileName();
80     String nameWithoutExtension = FileUtil.getNameWithoutExtension(defaultTestFileName);
81     String extension = FileUtilRt.getExtension(defaultTestFileName);
82     String subtaskTestFileName = nameWithoutExtension + EduNames.SUBTASK_MARKER + toSubtaskIndex;
83     VirtualFile subtaskTestFile = taskDir.findChild(subtaskTestFileName + ".txt");
84     if (subtaskTestFile != null) {
85       ApplicationManager.getApplication().runWriteAction(() -> {
86         try {
87           subtaskTestFile.rename(project, subtaskTestFileName + "." + extension);
88         }
89         catch (IOException e) {
90           LOG.error(e);
91         }
92       });
93     }
94   }
95
96   private static void updateUI(@NotNull Project project, @NotNull Task task, VirtualFile taskDir) {
97     StudyCheckUtils.drawAllPlaceholders(project, task, taskDir);
98     ProjectView.getInstance(project).refresh();
99     StudyToolWindow toolWindow = StudyUtils.getStudyToolWindow(project);
100     if (toolWindow != null) {
101       String text = StudyUtils.getTaskTextFromTask(taskDir, task);
102       if (text == null) {
103         toolWindow.setEmptyText(project);
104         return;
105       }
106       toolWindow.setTaskText(text, taskDir, project);
107     }
108   }
109
110   private static void updatePlaceholderTexts(@NotNull Project project,
111                                              @NotNull Document document,
112                                              @NotNull TaskFile taskFile,
113                                              int fromSubtaskIndex,
114                                              int toSubtaskIndex) {
115     taskFile.setTrackLengths(false);
116     for (AnswerPlaceholder placeholder : taskFile.getAnswerPlaceholders()) {
117       placeholder.switchSubtask(project, document, fromSubtaskIndex, toSubtaskIndex);
118     }
119     taskFile.setTrackLengths(true);
120   }
121
122   public static void refreshPlaceholder(@NotNull Project project, @NotNull Editor editor, @NotNull AnswerPlaceholder placeholder) {
123     int prevSubtaskIndex = placeholder.getActiveSubtaskIndex() - 1;
124     AnswerPlaceholderSubtaskInfo info = placeholder.getSubtaskInfos().get(prevSubtaskIndex);
125     String replacementText = info != null ? info.getAnswer() : placeholder.getTaskText();
126     EduUtils.replaceAnswerPlaceholder(project, editor.getDocument(), placeholder, placeholder.getRealLength(), replacementText);
127   }
128 }