mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
move task creation from util method to new configurator (EDU-895)
This commit is contained in:
-11
@@ -1,27 +1,16 @@
|
||||
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 com.jetbrains.edu.learning.courseFormat.Task;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public interface CCLanguageManager {
|
||||
LanguageExtension<CCLanguageManager> INSTANCE = new LanguageExtension<>("Edu.CCLanguageManager");
|
||||
|
||||
@Nullable
|
||||
String getDefaultTaskFileExtension();
|
||||
|
||||
@Nullable
|
||||
FileTemplate getTaskFileTemplateForExtension(@NotNull final Project project, String extension);
|
||||
|
||||
@Nullable
|
||||
FileTemplate getTestsTemplate(@NotNull final Project project);
|
||||
|
||||
boolean doNotPackFile(File pathname);
|
||||
|
||||
default boolean isTestFile(VirtualFile file) {
|
||||
|
||||
-43
@@ -2,11 +2,6 @@ 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;
|
||||
@@ -23,9 +18,7 @@ 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.EduNames;
|
||||
@@ -281,40 +274,4 @@ 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+4
@@ -38,4 +38,8 @@ public interface EduPluginConfigurator {
|
||||
PsiDirectory createTask(@NotNull final Project project, @NotNull final StudyItem item,
|
||||
@Nullable final IdeView view, @NotNull final PsiDirectory parentDirectory,
|
||||
@NotNull final Course course);
|
||||
|
||||
void createTaskContent(@NotNull Project project,
|
||||
@Nullable IdeView view,
|
||||
PsiDirectory taskDirectory);
|
||||
}
|
||||
|
||||
@@ -3,6 +3,11 @@ package com.jetbrains.edu.learning;
|
||||
import com.intellij.execution.RunContentExecutor;
|
||||
import com.intellij.execution.configurations.GeneralCommandLine;
|
||||
import com.intellij.execution.process.ProcessHandler;
|
||||
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.CommonDataKeys;
|
||||
@@ -200,7 +205,7 @@ public class StudyUtils {
|
||||
@Nullable
|
||||
public static StudyToolWindow getStudyToolWindow(@NotNull final Project project) {
|
||||
if (project.isDisposed()) return null;
|
||||
|
||||
|
||||
ToolWindow toolWindow = ToolWindowManager.getInstance(project).getToolWindow(StudyToolWindowFactory.STUDY_TOOL_WINDOW);
|
||||
if (toolWindow != null) {
|
||||
Content[] contents = toolWindow.getContentManager().getContents();
|
||||
@@ -527,9 +532,9 @@ public class StudyUtils {
|
||||
if (taskDirectory == null) return null;
|
||||
final String taskFileNameMd = constructTaskTextFilename(task, EduNames.TASK_MD);
|
||||
final String taskFileNameHtml = constructTaskTextFilename(task, EduNames.TASK_HTML);
|
||||
|
||||
|
||||
VirtualFile taskTextFile = ObjectUtils.chooseNotNull(taskDirectory.findChild(taskFileNameMd), taskDirectory.findChild(taskFileNameHtml));
|
||||
|
||||
|
||||
if (taskTextFile == null) {
|
||||
VirtualFile srcDir = taskDirectory.findChild(EduNames.SRC);
|
||||
if (srcDir != null) {
|
||||
@@ -594,7 +599,7 @@ public class StudyUtils {
|
||||
}
|
||||
return taskFile;
|
||||
}
|
||||
|
||||
|
||||
@Nullable
|
||||
public static Task getCurrentTask(@NotNull final Project project) {
|
||||
final TaskFile taskFile = getSelectedTaskFile(project);
|
||||
@@ -713,10 +718,10 @@ public class StudyUtils {
|
||||
ArrayList<String> lines = ContainerUtil.newArrayList(content.split("\n|\r|\r\n"));
|
||||
MarkdownUtil.replaceHeaders(lines);
|
||||
MarkdownUtil.replaceCodeBlock(lines);
|
||||
|
||||
|
||||
return new MarkdownProcessor().markdown(StringUtil.join(lines, "\n"));
|
||||
}
|
||||
|
||||
|
||||
public static boolean isTaskDescriptionFile(@NotNull final String fileName) {
|
||||
if (EduNames.TASK_HTML.equals(fileName) || EduNames.TASK_MD.equals(fileName)) {
|
||||
return true;
|
||||
@@ -727,21 +732,21 @@ public class StudyUtils {
|
||||
}
|
||||
return fileName.contains(EduNames.TASK) && fileName.contains(EduNames.SUBTASK_MARKER);
|
||||
}
|
||||
|
||||
|
||||
@Nullable
|
||||
public static VirtualFile findTaskDescriptionVirtualFile(@NotNull Project project, @NotNull VirtualFile taskDir) {
|
||||
Task task = getTask(project, taskDir.getName().contains(EduNames.TASK) ? taskDir: taskDir.getParent());
|
||||
if (task == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
return ObjectUtils.chooseNotNull(taskDir.findChild(constructTaskTextFilename(task, EduNames.TASK_HTML)),
|
||||
taskDir.findChild(constructTaskTextFilename(task, EduNames.TASK_MD)));
|
||||
}
|
||||
|
||||
|
||||
@NotNull
|
||||
public static String getTaskDescriptionFileName(final boolean useHtml) {
|
||||
return useHtml ? EduNames.TASK_HTML : EduNames.TASK_MD;
|
||||
return useHtml ? EduNames.TASK_HTML : EduNames.TASK_MD;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@@ -829,7 +834,7 @@ public class StudyUtils {
|
||||
final int endOffset = startOffset + length + delta;
|
||||
return Pair.create(startOffset, endOffset);
|
||||
}
|
||||
|
||||
|
||||
public static boolean isCourseValid(@Nullable Course course) {
|
||||
if (course == null) return false;
|
||||
if (course.isAdaptive()) {
|
||||
@@ -840,4 +845,26 @@ public class StudyUtils {
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static void createFromTemplate(@NotNull Project project,
|
||||
@NotNull PsiDirectory taskDirectory,
|
||||
@NotNull String name,
|
||||
@Nullable IdeView view,
|
||||
boolean open) {
|
||||
FileTemplate template = FileTemplateManager.getInstance(project).getInternalTemplate(name);
|
||||
if (template == null) {
|
||||
LOG.info("Template " + name + " wasn't found");
|
||||
return;
|
||||
}
|
||||
try {
|
||||
final PsiElement file = FileTemplateUtil.createFromTemplate(template, name, null, taskDirectory);
|
||||
if (view != null && open) {
|
||||
EditorHelper.openInEditor(file, false);
|
||||
view.selectElement(file);
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
LOG.error(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+3
-27
@@ -11,47 +11,22 @@ import com.intellij.openapi.util.io.FileUtilRt;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.psi.PsiDirectory;
|
||||
import com.intellij.psi.PsiManager;
|
||||
import com.jetbrains.edu.learning.StudyUtils;
|
||||
import com.jetbrains.edu.learning.core.EduNames;
|
||||
import com.jetbrains.edu.learning.courseFormat.Task;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public class PyCCLanguageManager implements CCLanguageManager {
|
||||
private static final Logger LOG = Logger.getInstance(PyCCLanguageManager.class);
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public String getDefaultTaskFileExtension() {
|
||||
return "py";
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public FileTemplate getTaskFileTemplateForExtension(@NotNull final Project project, String extension) {
|
||||
if (!extension.equals("py")) {
|
||||
return null;
|
||||
}
|
||||
return getInternalTemplateByName(project, "task.py");
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public FileTemplate getTestsTemplate(@NotNull final Project project) {
|
||||
return getInternalTemplateByName(project, EduNames.TESTS_FILE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean doNotPackFile(File pathname) {
|
||||
String name = pathname.getName();
|
||||
return name.contains("__pycache__") || name.contains(".pyc");
|
||||
}
|
||||
|
||||
private static FileTemplate getInternalTemplateByName(@NotNull final Project project, String name) {
|
||||
return FileTemplateManager.getInstance(project).getInternalTemplate(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isTestFile(VirtualFile file) {
|
||||
String name = file.getName();
|
||||
@@ -72,11 +47,12 @@ public class PyCCLanguageManager implements CCLanguageManager {
|
||||
ApplicationManager.getApplication().runWriteAction(() -> {
|
||||
try {
|
||||
PsiDirectory taskPsiDir = PsiManager.getInstance(project).findDirectory(taskDir);
|
||||
FileTemplate testsTemplate = getTestsTemplate(project);
|
||||
FileTemplate testsTemplate = FileTemplateManager.getInstance(project).getInternalTemplate(EduNames.TESTS_FILE);
|
||||
if (taskPsiDir == null || testsTemplate == null) {
|
||||
return;
|
||||
}
|
||||
FileTemplateUtil.createFromTemplate(testsTemplate, nextSubtaskTestsFileName, null, taskPsiDir);
|
||||
StudyUtils.createFromTemplate(project, taskPsiDir, nextSubtaskTestsFileName, null, false);
|
||||
}
|
||||
catch (Exception e) {
|
||||
LOG.error(e);
|
||||
|
||||
+15
-3
@@ -6,7 +6,7 @@ import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.Ref;
|
||||
import com.intellij.psi.PsiDirectory;
|
||||
import com.jetbrains.edu.coursecreator.CCUtils;
|
||||
import com.jetbrains.edu.coursecreator.settings.CCSettings;
|
||||
import com.jetbrains.edu.learning.core.EduNames;
|
||||
import com.jetbrains.edu.learning.courseFormat.Course;
|
||||
import com.jetbrains.edu.learning.courseFormat.StudyItem;
|
||||
@@ -16,11 +16,12 @@ import org.jetbrains.annotations.Nullable;
|
||||
public class PyEduPluginConfigurator implements EduPluginConfigurator {
|
||||
public static final String PYTHON_3 = "3.x";
|
||||
public static final String PYTHON_2 = "2.x";
|
||||
private static final String TESTS_PY = "tests.py";
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getTestFileName() {
|
||||
return "tests.py";
|
||||
return TESTS_PY;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -34,9 +35,20 @@ public class PyEduPluginConfigurator implements EduPluginConfigurator {
|
||||
String taskDirName = EduNames.TASK + item.getIndex();
|
||||
taskDirectory.set(DirectoryUtil.createSubdirectories(taskDirName, parentDirectory, "\\/"));
|
||||
if (taskDirectory.get() != null) {
|
||||
CCUtils.createTaskContent(project, view, course, taskDirectory.get());
|
||||
createTaskContent(project, view, taskDirectory.get());
|
||||
}
|
||||
});
|
||||
return taskDirectory.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void createTaskContent(@NotNull Project project,
|
||||
@Nullable IdeView view,
|
||||
PsiDirectory taskDirectory) {
|
||||
StudyUtils.createFromTemplate(project, taskDirectory, "task.py", view, false);
|
||||
StudyUtils.createFromTemplate(project, taskDirectory, TESTS_PY, view, false);
|
||||
StudyUtils.createFromTemplate(project, taskDirectory,
|
||||
StudyUtils.getTaskDescriptionFileName(CCSettings.getInstance().useHtmlAsDefaultTaskFormat()), view,
|
||||
false);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user