extracted method to use in language plugins
authorLiana.Bakradze <liana.bakradze@jetbrains.com>
Wed, 12 Oct 2016 12:37:42 +0000 (15:37 +0300)
committerliana.bakradze <liana.bakradze@jetbrains.com>
Thu, 17 Nov 2016 14:08:28 +0000 (17:08 +0300)
python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/CCUtils.java
python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/actions/CCCreateTask.java

index 28443bc23e5b551aea8b36ca42086460dccc5616..cf5af7a8398d6e8c229653b56329b8a0032d02f2 100644 (file)
@@ -2,6 +2,11 @@ package com.jetbrains.edu.coursecreator;
 
 import com.google.common.base.Predicate;
 import com.google.common.collect.Collections2;
+import com.intellij.ide.IdeView;
+import com.intellij.ide.fileTemplates.FileTemplate;
+import com.intellij.ide.fileTemplates.FileTemplateManager;
+import com.intellij.ide.fileTemplates.FileTemplateUtil;
+import com.intellij.ide.util.EditorHelper;
 import com.intellij.lang.Language;
 import com.intellij.openapi.actionSystem.AnActionEvent;
 import com.intellij.openapi.actionSystem.Presentation;
@@ -16,8 +21,11 @@ import com.intellij.openapi.util.io.FileUtil;
 import com.intellij.openapi.vfs.LocalFileSystem;
 import com.intellij.openapi.vfs.VirtualFile;
 import com.intellij.psi.PsiDirectory;
+import com.intellij.psi.PsiElement;
 import com.intellij.util.Function;
+import com.jetbrains.edu.coursecreator.settings.CCSettings;
 import com.jetbrains.edu.learning.StudyTaskManager;
+import com.jetbrains.edu.learning.StudyUtils;
 import com.jetbrains.edu.learning.core.EduUtils;
 import com.jetbrains.edu.learning.courseFormat.Course;
 import com.jetbrains.edu.learning.courseFormat.StudyItem;
@@ -241,4 +249,40 @@ public class CCUtils {
     Project project = e.getProject();
     presentation.setEnabledAndVisible(project != null && isCourseCreator(project));
   }
+
+  private static void createFromTemplate(@NotNull final PsiDirectory taskDirectory,
+                                         @Nullable final FileTemplate template,
+                                         @Nullable IdeView view, boolean open) {
+    if (template == null) {
+      return;
+    }
+    try {
+      final PsiElement file = FileTemplateUtil.createFromTemplate(template, template.getName(), null, taskDirectory);
+      if (view != null && open) {
+        EditorHelper.openInEditor(file, false);
+        view.selectElement(file);
+      }
+    }
+    catch (Exception e) {
+      LOG.error(e);
+    }
+  }
+
+  public static void createTaskContent(@NotNull Project project,
+                                   @Nullable IdeView view,
+                                   @NotNull Course course,
+                                   PsiDirectory taskDirectory) {
+    CCLanguageManager manager = getStudyLanguageManager(course);
+    if (manager == null) {
+      return;
+    }
+    createFromTemplate(taskDirectory, manager.getTestsTemplate(project), view, false);
+    createFromTemplate(taskDirectory, FileTemplateManager.getInstance(project)
+      .getInternalTemplate(StudyUtils.getTaskDescriptionFileName(CCSettings.getInstance().useHtmlAsDefaultTaskFormat())), view, false);
+    String defaultExtension = manager.getDefaultTaskFileExtension();
+    if (defaultExtension != null) {
+      FileTemplate taskFileTemplate = manager.getTaskFileTemplateForExtension(project, defaultExtension);
+      createFromTemplate(taskDirectory, taskFileTemplate, view, true);
+    }
+  }
 }
index fbbe0d95fc67dc980058cd1d80db91c5341df17f..6b4201b2245266b4588ebf87b413a20d72b5e0b0 100644 (file)
@@ -1,23 +1,14 @@
 package com.jetbrains.edu.coursecreator.actions;
 
 import com.intellij.ide.IdeView;
-import com.intellij.ide.fileTemplates.FileTemplate;
-import com.intellij.ide.fileTemplates.FileTemplateManager;
-import com.intellij.ide.fileTemplates.FileTemplateUtil;
 import com.intellij.ide.util.DirectoryUtil;
-import com.intellij.ide.util.EditorHelper;
 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.vfs.VirtualFile;
 import com.intellij.psi.PsiDirectory;
-import com.intellij.psi.PsiElement;
 import com.intellij.util.Function;
-import com.jetbrains.edu.coursecreator.CCLanguageManager;
 import com.jetbrains.edu.coursecreator.CCUtils;
-import com.jetbrains.edu.coursecreator.settings.CCSettings;
-import com.jetbrains.edu.learning.StudyUtils;
 import com.jetbrains.edu.learning.core.EduNames;
 import com.jetbrains.edu.learning.core.EduUtils;
 import com.jetbrains.edu.learning.courseFormat.Course;
@@ -32,7 +23,6 @@ import java.util.Collections;
 import java.util.List;
 
 public class CCCreateTask extends CCCreateStudyItemActionBase {
-  private static final Logger LOG = Logger.getInstance(CCCreateTask.class.getName());
   public static final String TITLE = "Create New " + EduNames.TASK_TITLED;
 
   public CCCreateTask() {
@@ -40,23 +30,7 @@ public class CCCreateTask extends CCCreateStudyItemActionBase {
   }
 
 
-  private static void createFromTemplate(@NotNull final PsiDirectory taskDirectory,
-                                         @Nullable final FileTemplate template,
-                                         @Nullable IdeView view, boolean open) {
-    if (template == null) {
-      return;
-    }
-    try {
-      final PsiElement file = FileTemplateUtil.createFromTemplate(template, template.getName(), null, taskDirectory);
-      if (view != null && open) {
-        EditorHelper.openInEditor(file, false);
-        view.selectElement(file);
-      }
-    }
-    catch (Exception e) {
-      LOG.error(e);
-    }
-  }
+
 
   @Nullable
   @Override
@@ -96,18 +70,7 @@ public class CCCreateTask extends CCCreateStudyItemActionBase {
       String taskDirName = EduNames.TASK + item.getIndex();
       taskDirectory.set(DirectoryUtil.createSubdirectories(taskDirName, parentDirectory, "\\/"));
       if (taskDirectory.get() != null) {
-        CCLanguageManager manager = CCUtils.getStudyLanguageManager(course);
-        if (manager == null) {
-          return;
-        }
-        createFromTemplate(taskDirectory.get(), manager.getTestsTemplate(project), view, false);
-        createFromTemplate(taskDirectory.get(), FileTemplateManager.getInstance(project)
-          .getInternalTemplate(StudyUtils.getTaskDescriptionFileName(CCSettings.getInstance().useHtmlAsDefaultTaskFormat())), view, false);
-        String defaultExtension = manager.getDefaultTaskFileExtension();
-        if (defaultExtension != null) {
-          FileTemplate taskFileTemplate = manager.getTaskFileTemplateForExtension(project, defaultExtension);
-          createFromTemplate(taskDirectory.get(), taskFileTemplate, view, true);
-        }
+        CCUtils.createTaskContent(project, view, course, taskDirectory.get());
       }
     });
     return taskDirectory.get();