show in project view only active subtask test
[idea/community.git] / python / educational-core / course-creator / src / com / jetbrains / edu / coursecreator / projectView / CCTreeStructureProvider.java
index 2a4249929538e051e329f907cd68d014fe1ed966..0b89eaea50521bab595f57b21c2e36e04c4f175d 100644 (file)
@@ -9,7 +9,12 @@ import com.intellij.openapi.vfs.VirtualFile;
 import com.intellij.psi.PsiDirectory;
 import com.intellij.psi.PsiFile;
 import com.jetbrains.edu.coursecreator.CCUtils;
+import com.jetbrains.edu.learning.StudyLanguageManager;
+import com.jetbrains.edu.learning.StudyTaskManager;
 import com.jetbrains.edu.learning.StudyUtils;
+import com.jetbrains.edu.learning.core.EduNames;
+import com.jetbrains.edu.learning.courseFormat.Course;
+import com.jetbrains.edu.learning.courseFormat.Task;
 import com.jetbrains.edu.learning.projectView.StudyTreeStructureProvider;
 import org.jetbrains.annotations.NotNull;
 
@@ -48,7 +53,10 @@ public class CCTreeStructureProvider extends StudyTreeStructureProvider {
           continue;
         }
         PsiFile psiFile = ((PsiFileNode)node).getValue();
-        modifiedChildren.add(new CCStudentInvisibleFileNode(project, psiFile, settings));
+        boolean handled = handleTests(project, virtualFile, psiFile, modifiedChildren, settings);
+        if (!handled) {
+          modifiedChildren.add(new CCStudentInvisibleFileNode(project, psiFile, settings));
+        }
       }
     }
     return modifiedChildren;
@@ -61,4 +69,51 @@ public class CCTreeStructureProvider extends StudyTreeStructureProvider {
     }
     return CCUtils.isCourseCreator(project);
   }
+
+  private static boolean handleTests(Project project,
+                                     VirtualFile virtualFile,
+                                     PsiFile psiFile,
+                                     Collection<AbstractTreeNode> modifiedChildren,
+                                     ViewSettings settings) {
+    Course course = StudyTaskManager.getInstance(project).getCourse();
+    if (course == null) {
+      return false;
+    }
+    if (!CCUtils.isTestsFile(project, virtualFile)) {
+      return false;
+    }
+    VirtualFile taskDir = StudyUtils.getTaskDir(virtualFile);
+    if (taskDir == null) {
+      return false;
+    }
+    Task task = StudyUtils.getTask(project, taskDir);
+    if (task == null) {
+      return false;
+    }
+    if (isActiveSubtaskTest(task, virtualFile)) {
+      StudyLanguageManager manager = StudyUtils.getLanguageManager(course);
+      String testsFileName = manager != null ? manager.getTestFileName() : psiFile.getName();
+      modifiedChildren.add(new CCStudentInvisibleFileNode(project, psiFile, settings,
+                                                          testsFileName));
+    }
+    return true;
+  }
+
+  private static boolean isActiveSubtaskTest(Task task, VirtualFile virtualFile) {
+    if (!task.hasSubtasks()) {
+      return true;
+    }
+
+    boolean isSubtaskTestFile = virtualFile.getName().contains(EduNames.SUBTASK_MARKER);
+    if (task.getActiveSubtaskIndex() == 0) {
+      return !isSubtaskTestFile;
+    }
+    if (!isSubtaskTestFile) {
+      return false;
+    }
+    String nameWithoutExtension = virtualFile.getNameWithoutExtension();
+    int stepMarkerStart = nameWithoutExtension.indexOf(EduNames.SUBTASK_MARKER);
+    int stepIndex = Integer.valueOf(nameWithoutExtension.substring(EduNames.SUBTASK_MARKER.length() + stepMarkerStart));
+    return stepIndex == task.getActiveSubtaskIndex();
+  }
 }