show in project view only active subtask test
[idea/community.git] / python / educational-core / course-creator / src / com / jetbrains / edu / coursecreator / projectView / CCTreeStructureProvider.java
1 package com.jetbrains.edu.coursecreator.projectView;
2
3 import com.intellij.ide.projectView.ViewSettings;
4 import com.intellij.ide.projectView.impl.nodes.PsiFileNode;
5 import com.intellij.ide.util.treeView.AbstractTreeNode;
6 import com.intellij.openapi.project.Project;
7 import com.intellij.openapi.util.io.FileUtilRt;
8 import com.intellij.openapi.vfs.VirtualFile;
9 import com.intellij.psi.PsiDirectory;
10 import com.intellij.psi.PsiFile;
11 import com.jetbrains.edu.coursecreator.CCUtils;
12 import com.jetbrains.edu.learning.StudyLanguageManager;
13 import com.jetbrains.edu.learning.StudyTaskManager;
14 import com.jetbrains.edu.learning.StudyUtils;
15 import com.jetbrains.edu.learning.core.EduNames;
16 import com.jetbrains.edu.learning.courseFormat.Course;
17 import com.jetbrains.edu.learning.courseFormat.Task;
18 import com.jetbrains.edu.learning.projectView.StudyTreeStructureProvider;
19 import org.jetbrains.annotations.NotNull;
20
21 import java.util.Collection;
22
23 public class CCTreeStructureProvider extends StudyTreeStructureProvider {
24   @NotNull
25   @Override
26   public Collection<AbstractTreeNode> modify(@NotNull AbstractTreeNode parent,
27                                              @NotNull Collection<AbstractTreeNode> children,
28                                              ViewSettings settings) {
29     if (!needModify(parent)) {
30       return children;
31     }
32     Collection<AbstractTreeNode> modifiedChildren = super.modify(parent, children, settings);
33
34     for (AbstractTreeNode node : children) {
35       Project project = node.getProject();
36       if (project == null) {
37         continue;
38       }
39       if (node.getValue() instanceof PsiDirectory) {
40         String name = ((PsiDirectory)node.getValue()).getName();
41         if ("zip".equals(FileUtilRt.getExtension(name))) {
42           modifiedChildren.add(node);
43           continue;
44         }
45       }
46       if (node instanceof PsiFileNode) {
47         PsiFileNode fileNode = (PsiFileNode)node;
48         VirtualFile virtualFile = fileNode.getVirtualFile();
49         if (virtualFile == null) {
50           continue;
51         }
52         if (StudyUtils.getTaskFile(project, virtualFile) != null || StudyUtils.isTaskDescriptionFile(virtualFile.getName())) {
53           continue;
54         }
55         PsiFile psiFile = ((PsiFileNode)node).getValue();
56         boolean handled = handleTests(project, virtualFile, psiFile, modifiedChildren, settings);
57         if (!handled) {
58           modifiedChildren.add(new CCStudentInvisibleFileNode(project, psiFile, settings));
59         }
60       }
61     }
62     return modifiedChildren;
63   }
64
65   protected boolean needModify(@NotNull final AbstractTreeNode parent) {
66     Project project = parent.getProject();
67     if (project == null) {
68       return false;
69     }
70     return CCUtils.isCourseCreator(project);
71   }
72
73   private static boolean handleTests(Project project,
74                                      VirtualFile virtualFile,
75                                      PsiFile psiFile,
76                                      Collection<AbstractTreeNode> modifiedChildren,
77                                      ViewSettings settings) {
78     Course course = StudyTaskManager.getInstance(project).getCourse();
79     if (course == null) {
80       return false;
81     }
82     if (!CCUtils.isTestsFile(project, virtualFile)) {
83       return false;
84     }
85     VirtualFile taskDir = StudyUtils.getTaskDir(virtualFile);
86     if (taskDir == null) {
87       return false;
88     }
89     Task task = StudyUtils.getTask(project, taskDir);
90     if (task == null) {
91       return false;
92     }
93     if (isActiveSubtaskTest(task, virtualFile)) {
94       StudyLanguageManager manager = StudyUtils.getLanguageManager(course);
95       String testsFileName = manager != null ? manager.getTestFileName() : psiFile.getName();
96       modifiedChildren.add(new CCStudentInvisibleFileNode(project, psiFile, settings,
97                                                           testsFileName));
98     }
99     return true;
100   }
101
102   private static boolean isActiveSubtaskTest(Task task, VirtualFile virtualFile) {
103     if (!task.hasSubtasks()) {
104       return true;
105     }
106
107     boolean isSubtaskTestFile = virtualFile.getName().contains(EduNames.SUBTASK_MARKER);
108     if (task.getActiveSubtaskIndex() == 0) {
109       return !isSubtaskTestFile;
110     }
111     if (!isSubtaskTestFile) {
112       return false;
113     }
114     String nameWithoutExtension = virtualFile.getNameWithoutExtension();
115     int stepMarkerStart = nameWithoutExtension.indexOf(EduNames.SUBTASK_MARKER);
116     int stepIndex = Integer.valueOf(nameWithoutExtension.substring(EduNames.SUBTASK_MARKER.length() + stepMarkerStart));
117     return stepIndex == task.getActiveSubtaskIndex();
118   }
119 }