add new files as task files
authorliana.bakradze <liana.bakradze@jetbrains.com>
Mon, 4 Apr 2016 12:18:29 +0000 (15:18 +0300)
committerliana.bakradze <liana.bakradze@jetbrains.com>
Mon, 4 Apr 2016 12:18:29 +0000 (15:18 +0300)
python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/CCLanguageManager.java
python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/CCProjectComponent.java
python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/CCUtils.java
python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/CCVirtualFileListener.java [new file with mode: 0644]
python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/actions/CCCreateTask.java
python/educational-core/student/src/com/jetbrains/edu/learning/StudyUtils.java
python/educational-python/course-creator-python/src/com/jetbrains/edu/coursecreator/PyCCLanguageManager.java

index 5ab5f5591ed850529bf5d9ac55e09f1b647e291e..b6c8fd4ecf533aad3d0473f2bfa9a145981d4fb5 100644 (file)
@@ -3,6 +3,7 @@ package com.jetbrains.edu.coursecreator;
 import com.intellij.ide.fileTemplates.FileTemplate;
 import com.intellij.lang.LanguageExtension;
 import com.intellij.openapi.project.Project;
+import com.intellij.openapi.vfs.VirtualFile;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
 
@@ -21,4 +22,8 @@ public interface CCLanguageManager {
   FileTemplate getTestsTemplate(@NotNull final Project project);
 
   boolean doNotPackFile(File pathname);
+
+  default boolean isTestFile(VirtualFile file) {
+    return false;
+  }
 }
index e5871848ecf1e553fa713d8f3bec95e001dc8802..25ba06fd9b3042f75ac31581445ad426b1433821 100644 (file)
@@ -1,23 +1,13 @@
 package com.jetbrains.edu.coursecreator;
 
 import com.intellij.openapi.components.ProjectComponent;
-import com.intellij.openapi.editor.Editor;
-import com.intellij.openapi.editor.EditorFactory;
-import com.intellij.openapi.editor.event.EditorFactoryEvent;
-import com.intellij.openapi.editor.impl.EditorFactoryImpl;
-import com.intellij.openapi.fileEditor.FileEditor;
-import com.intellij.openapi.fileEditor.FileEditorManager;
-import com.intellij.openapi.fileEditor.impl.text.PsiAwareTextEditorImpl;
 import com.intellij.openapi.project.Project;
-import com.intellij.openapi.project.ProjectManager;
-import com.intellij.openapi.startup.StartupManager;
-import com.intellij.openapi.vfs.VirtualFile;
 import com.intellij.openapi.vfs.VirtualFileManager;
-import com.jetbrains.edu.learning.courseFormat.Course;
 import org.jetbrains.annotations.NotNull;
 
 public class CCProjectComponent implements ProjectComponent {
   private final Project myProject;
+  private final  CCVirtualFileListener myTaskFileLifeListener = new CCVirtualFileListener();
   private CCFileDeletedListener myListener;
 
   public CCProjectComponent(Project project) {
@@ -25,6 +15,7 @@ public class CCProjectComponent implements ProjectComponent {
   }
 
   public void initComponent() {
+    VirtualFileManager.getInstance().addVirtualFileListener(myTaskFileLifeListener);
   }
 
   public void disposeComponent() {
@@ -36,14 +27,15 @@ public class CCProjectComponent implements ProjectComponent {
   }
 
   public void projectOpened() {
+    VirtualFileManager.getInstance().addVirtualFileListener(myTaskFileLifeListener);
     StartupManager.getInstance(myProject).runWhenProjectIsInitialized(new Runnable() {
       @Override
       public void run() {
         final Course course = CCProjectService.getInstance(myProject).getCourse();
         if (course != null) {
           course.initCourse(true);
-          myListener = new CCFileDeletedListener(myProject);
-          VirtualFileManager.getInstance().addVirtualFileListener(myListener);
+          myTaskFileLifeListener = new CCFileDeletedListener(myProject);
+          VirtualFileManager.getInstance().addVirtualFileListener(myTaskFileLifeListener);
           final CCEditorFactoryListener editorFactoryListener = new CCEditorFactoryListener();
           EditorFactory.getInstance().addEditorFactoryListener(editorFactoryListener, myProject);
           VirtualFile[] files = FileEditorManager.getInstance(myProject).getOpenFiles();
@@ -64,8 +56,9 @@ public class CCProjectComponent implements ProjectComponent {
   }
 
   public void projectClosed() {
+    VirtualFileManager.getInstance().removeVirtualFileListener(myTaskFileLifeListener);
     if (myListener != null) {
-      VirtualFileManager.getInstance().removeVirtualFileListener(myListener);
+      VirtualFileManager.getInstance().removeVirtualFileListener(myTaskFileLifeListener);
     }
   }
 }
index 0c5a9f77561c1a8252d6543619f2b972dcda078d..139a15be6845fe9ba30bdce56837e21b64302481 100644 (file)
@@ -178,4 +178,16 @@ public class CCUtils {
 
     return COURSE_MODE.equals(course.getCourseMode());
   }
+
+  public static boolean isTestsFile(@NotNull Project project, @NotNull VirtualFile file) {
+    Course course = StudyTaskManager.getInstance(project).getCourse();
+    if (course == null) {
+      return false;
+    }
+    CCLanguageManager manager = getStudyLanguageManager(course);
+    if (manager == null) {
+      return false;
+    }
+    return manager.isTestFile(file);
+  }
 }
diff --git a/python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/CCVirtualFileListener.java b/python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/CCVirtualFileListener.java
new file mode 100644 (file)
index 0000000..6a7eda6
--- /dev/null
@@ -0,0 +1,48 @@
+package com.jetbrains.edu.coursecreator;
+
+import com.intellij.openapi.project.Project;
+import com.intellij.openapi.project.ProjectUtil;
+import com.intellij.openapi.vfs.VirtualFile;
+import com.intellij.openapi.vfs.VirtualFileAdapter;
+import com.intellij.openapi.vfs.VirtualFileEvent;
+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.courseFormat.TaskFile;
+import org.jetbrains.annotations.NotNull;
+
+public class CCVirtualFileListener extends VirtualFileAdapter {
+
+  @Override
+  public void fileCreated(@NotNull VirtualFileEvent event) {
+    VirtualFile createdFile = event.getFile();
+    Project project = ProjectUtil.guessProjectForContentFile(createdFile);
+    if (project == null) {
+      return;
+    }
+    Course course = StudyTaskManager.getInstance(project).getCourse();
+    if (course == null || !CCUtils.isCourseCreator(project)) {
+      return;
+    }
+    TaskFile taskFile = StudyUtils.getTaskFile(project, createdFile);
+    if (taskFile != null) {
+      return;
+    }
+
+    if (CCUtils.isTestsFile(project, createdFile) || EduNames.TASK_HTML.equals(createdFile.getName())) {
+      return;
+    }
+
+    VirtualFile taskVF = createdFile.getParent();
+    if (taskVF == null) {
+      return;
+    }
+    Task task = StudyUtils.getTask(project,taskVF);
+    if (task == null) {
+      return;
+    }
+    task.addTaskFile(createdFile.getName(), 1);
+  }
+}
index 0c053ab57c0bfd47a04b58109562e5d5d74580cf..844fc9ad379608d45a7b4fa8ea3b7c2a58c386c8 100644 (file)
@@ -10,7 +10,6 @@ import com.intellij.openapi.application.ApplicationManager;
 import com.intellij.openapi.diagnostic.Logger;
 import com.intellij.openapi.project.Project;
 import com.intellij.openapi.util.Ref;
-import com.intellij.openapi.util.io.FileUtil;
 import com.intellij.openapi.vfs.VirtualFile;
 import com.intellij.psi.PsiDirectory;
 import com.intellij.psi.PsiElement;
@@ -110,10 +109,6 @@ public class CCCreateTask extends CCCreateStudyItemActionBase {
           if (defaultExtension != null) {
             FileTemplate taskFileTemplate = manager.getTaskFileTemplateForExtension(project, defaultExtension);
             createFromTemplate(taskDirectory.get(), taskFileTemplate, view, true);
-            if (taskFileTemplate != null) {
-              String taskFileName = FileUtil.getNameWithoutExtension(taskFileTemplate.getName());
-              ((Task)item).addTaskFile(taskFileName + "." + defaultExtension, 1);
-            }
           }
         }
       }
index f683331174e0605e793d3c7d14e15a1b44aec70a..3db531be22d221a6c78d0e685d1479a6fc4a8ae4 100644 (file)
@@ -497,4 +497,21 @@ public class StudyUtils {
   public static boolean isStudyProject(Project project) {
     return StudyTaskManager.getInstance(project).getCourse() != null;
   }
+
+  @Nullable
+  public static Task getTask(@NotNull Project project, @NotNull VirtualFile taskVF) {
+    Course course = StudyTaskManager.getInstance(project).getCourse();
+    if (course == null) {
+      return null;
+    }
+    VirtualFile lessonVF = taskVF.getParent();
+    if (lessonVF == null) {
+      return null;
+    }
+    Lesson lesson = course.getLesson(lessonVF.getName());
+    if (lesson == null) {
+      return null;
+    }
+    return lesson.getTask(taskVF.getName());
+  }
 }
index f66ecff6ddd3f1f3e85986884762ccbdda20d516..8628bbdecc41700d2e10bd00e612fc6ca894eac2 100644 (file)
@@ -3,6 +3,7 @@ package com.jetbrains.edu.coursecreator;
 import com.intellij.ide.fileTemplates.FileTemplate;
 import com.intellij.ide.fileTemplates.FileTemplateManager;
 import com.intellij.openapi.project.Project;
+import com.intellij.openapi.vfs.VirtualFile;
 import com.jetbrains.edu.learning.core.EduNames;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
@@ -40,4 +41,9 @@ public class PyCCLanguageManager implements CCLanguageManager {
   private static FileTemplate getInternalTemplateByName(@NotNull final Project project, String name) {
     return FileTemplateManager.getInstance(project).getInternalTemplate(name);
   }
+
+  @Override
+  public boolean isTestFile(VirtualFile file) {
+    return EduNames.TESTS_FILE.equals(file.getName());
+  }
 }