open proper test file when switching between subtasks
authorLiana.Bakradze <liana.bakradze@jetbrains.com>
Thu, 10 Nov 2016 15:29:11 +0000 (18:29 +0300)
committerliana.bakradze <liana.bakradze@jetbrains.com>
Thu, 17 Nov 2016 14:08:42 +0000 (17:08 +0300)
python/educational-core/course-creator/resources/META-INF/plugin.xml
python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/CCSubtaskChangeListener.java [new file with mode: 0644]
python/educational-core/student/resources/META-INF/plugin.xml
python/educational-core/student/src/com/jetbrains/edu/learning/StudySubtaskChangeListener.java [new file with mode: 0644]
python/educational-core/student/src/com/jetbrains/edu/learning/StudySubtaskUtils.java

index 37c7704f87e150a5bacf260dc9a432e57aef71d0..2491e1cde81cb601624d4aef11eaa1e5f0744b13 100644 (file)
@@ -33,6 +33,7 @@
     <studyActionsProvider implementation="com.jetbrains.edu.coursecreator.CCStudyActionsProvider"/>
     <studyActionListener implementation="com.jetbrains.edu.coursecreator.CCStudyActionListener"/>
     <optionsProvider instance="com.jetbrains.edu.coursecreator.settings.CCOptions"/>
+    <studySubtaskChangeListener implementation="com.jetbrains.edu.coursecreator.CCSubtaskChangeListener"/>
   </extensions>
 
   <project-components>
diff --git a/python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/CCSubtaskChangeListener.java b/python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/CCSubtaskChangeListener.java
new file mode 100644 (file)
index 0000000..5b93704
--- /dev/null
@@ -0,0 +1,56 @@
+package com.jetbrains.edu.coursecreator;
+
+import com.intellij.openapi.fileEditor.FileEditorManager;
+import com.intellij.openapi.project.Project;
+import com.intellij.openapi.util.io.FileUtil;
+import com.intellij.openapi.util.io.FileUtilRt;
+import com.intellij.openapi.vfs.VirtualFile;
+import com.intellij.util.containers.ContainerUtil;
+import com.jetbrains.edu.learning.StudyLanguageManager;
+import com.jetbrains.edu.learning.StudySubtaskChangeListener;
+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 org.jetbrains.annotations.NotNull;
+
+import java.util.List;
+
+public class CCSubtaskChangeListener implements StudySubtaskChangeListener {
+  @Override
+  public void subtaskChanged(@NotNull Project project, @NotNull Task task, int oldSubtaskNumber, int newSubtaskNumber) {
+    VirtualFile taskDir = task.getTaskDir(project);
+    if (taskDir == null) {
+      return;
+    }
+    Course course = StudyTaskManager.getInstance(project).getCourse();
+    if (course == null) {
+      return;
+    }
+    StudyLanguageManager manager = StudyUtils.getLanguageManager(course);
+    if (manager == null) {
+      return;
+    }
+    String testFileName = manager.getTestFileName();
+    if (newSubtaskNumber != 0) {
+      String nameWithoutExtension = FileUtil.getNameWithoutExtension(testFileName);
+      String extension = FileUtilRt.getExtension(testFileName);
+      testFileName = nameWithoutExtension + EduNames.SUBTASK_MARKER + newSubtaskNumber + "." + extension;
+    }
+    VirtualFile newTestFile = taskDir.findChild(testFileName);
+    if (newTestFile == null) {
+      return;
+    }
+    FileEditorManager editorManager = FileEditorManager.getInstance(project);
+    List<VirtualFile> testFiles =
+      ContainerUtil.filter(taskDir.getChildren(), file -> CCUtils.isTestsFile(project, file) && editorManager.isFileOpen(file));
+    if (testFiles.isEmpty()) {
+      return;
+    }
+    for (VirtualFile testFile : testFiles) {
+      editorManager.closeFile(testFile);
+    }
+    editorManager.openFile(newTestFile, false);
+  }
+}
index 3058bd39f93790e498f055488848290a27e2862e..f8de6c5cf790789c37012c9ebfb991bef217513c 100644 (file)
@@ -45,7 +45,8 @@
     <extensionPoint qualifiedName="Edu.studyActionsProvider" interface="com.jetbrains.edu.learning.StudyActionsProvider"/>
     <extensionPoint qualifiedName="Edu.studyTwitterPluginConfigurator" interface="com.jetbrains.edu.learning.StudyTwitterPluginConfigurator"/>
     <extensionPoint qualifiedName="Edu.studyActionListener" interface="com.jetbrains.edu.learning.StudyActionListener"/>
-    <extensionPoint qualifiedName="Edu.optionsProvider" beanClass="com.jetbrains.edu.learning.settings.StudyOptionsProviderEP">>
+    <extensionPoint qualifiedName="Edu.studySubtaskChangeListener" interface="com.jetbrains.edu.learning.StudySubtaskChangeListener"/>
+    <extensionPoint qualifiedName="Edu.optionsProvider" beanClass="com.jetbrains.edu.learning.settings.StudyOptionsProviderEP">
       <with attribute="instance" implements="com.jetbrains.edu.learning.settings.StudyOptionsProvider"/>
     </extensionPoint>
   </extensionPoints>
diff --git a/python/educational-core/student/src/com/jetbrains/edu/learning/StudySubtaskChangeListener.java b/python/educational-core/student/src/com/jetbrains/edu/learning/StudySubtaskChangeListener.java
new file mode 100644 (file)
index 0000000..8ea064f
--- /dev/null
@@ -0,0 +1,12 @@
+package com.jetbrains.edu.learning;
+
+import com.intellij.openapi.extensions.ExtensionPointName;
+import com.intellij.openapi.project.Project;
+import com.jetbrains.edu.learning.courseFormat.Task;
+import org.jetbrains.annotations.NotNull;
+
+public interface StudySubtaskChangeListener {
+  ExtensionPointName<StudySubtaskChangeListener> EP_NAME = ExtensionPointName.create("Edu.studySubtaskChangeListener");
+
+  void subtaskChanged(@NotNull Project project, @NotNull Task task, int oldSubtaskNumber, int newSubtaskNumber);
+}
index 2f2c2c2111929d96f2f25aac1ff2ec1c60b71848..f595eaaa7183cc7f6d93ade07656e04146e0dcad 100644 (file)
@@ -5,6 +5,7 @@ import com.intellij.openapi.application.ApplicationManager;
 import com.intellij.openapi.diagnostic.Logger;
 import com.intellij.openapi.editor.Document;
 import com.intellij.openapi.editor.Editor;
+import com.intellij.openapi.extensions.Extensions;
 import com.intellij.openapi.fileEditor.FileDocumentManager;
 import com.intellij.openapi.project.Project;
 import com.intellij.openapi.util.io.FileUtil;
@@ -65,6 +66,10 @@ public class StudySubtaskUtils {
     transformTestFile(project, toSubtaskIndex, taskDir);
     task.setActiveSubtaskIndex(toSubtaskIndex);
     updateUI(project, task, taskDir);
+
+    for (StudySubtaskChangeListener listener : Extensions.getExtensions(StudySubtaskChangeListener.EP_NAME)) {
+      listener.subtaskChanged(project, task, fromSubtaskIndex, toSubtaskIndex);
+    }
   }
 
   private static void transformTestFile(@NotNull Project project, int toSubtaskIndex, VirtualFile taskDir) {