import com.intellij.ui.JBColor;
import com.intellij.ui.awt.RelativePoint;
import com.intellij.ui.content.Content;
+ import com.intellij.util.TimeoutUtil;
+import com.intellij.util.ObjectUtils;
+import com.intellij.util.containers.ContainerUtil;
+import com.intellij.util.text.MarkdownUtil;
import com.intellij.util.ui.UIUtil;
import com.jetbrains.edu.learning.checker.StudyExecutor;
import com.jetbrains.edu.learning.checker.StudyTestRunner;
import javax.swing.*;
import java.awt.*;
import java.io.*;
+import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
+ import java.util.concurrent.Callable;
+ import java.util.concurrent.ExecutionException;
+ import java.util.concurrent.Future;
public class StudyUtils {
private StudyUtils() {
return null;
}
+ // supposd to be called under progress
+ @Nullable
+ public static <T> T execCancelable(@NotNull final Callable<T> callable) {
+ final Future<T> future = ApplicationManager.getApplication().executeOnPooledThread(callable);
+
+ while (!future.isCancelled() && !future.isDone()) {
+ ProgressManager.checkCanceled();
+ TimeoutUtil.sleep(500);
+ }
+ T result = null;
+ try {
+ result = future.get();
+ }
+ catch (InterruptedException | ExecutionException e) {
+ LOG.warn(e.getMessage());
+ }
+ return result;
+ }
+
+ @Nullable
+ public static Task getTaskFromSelectedEditor(Project project) {
+ final StudyEditor editor = getSelectedStudyEditor(project);
+ Task task = null;
+ if (editor != null) {
+ final TaskFile file = editor.getTaskFile();
+ task = file.getTask();
+ }
+ return task;
+ }
++
+ private static String convertToHtml(@NotNull final String content) {
+ 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) {
+ return EduNames.TASK_HTML.equals(fileName) || EduNames.TASK_MD.equals(fileName);
+ }
+
+ @Nullable
+ public static VirtualFile findTaskDescriptionVirtualFile(@NotNull final VirtualFile parent) {
+ return ObjectUtils.chooseNotNull(parent.findChild(EduNames.TASK_HTML), parent.findChild(EduNames.TASK_MD));
+ }
+
+ @NotNull
+ public static String getTaskDescriptionFileName(final boolean useHtml) {
+ return useHtml ? EduNames.TASK_HTML : EduNames.TASK_MD;
+ }
+
+ @Nullable
+ public static File createTaskDescriptionFile(@NotNull final File parent) {
+ if(new File(parent, EduNames.TASK_HTML).exists()) {
+ return new File(parent, EduNames.TASK_HTML);
+ }
+ else {
+ return new File(parent, EduNames.TASK_MD);
+ }
+ }
}
return authors;
}
- public static String getAuthorsString(@NotNull List<CourseInfo.Author> authors) {
+ public static String getAuthorsString(@NotNull List<StepicUser> authors) {
- return StringUtil.join(authors, new Function<StepicUser, String>() {
- @Override
- public String fun(StepicUser author) {
- return author.getName();
- }
- }, ", ");
+ return StringUtil.join(authors, author -> author.getName(), ", ");
}
public void setAuthors(String[] authors) {
public static void createCourse(@NotNull final Course course, @NotNull final VirtualFile baseDir, @NotNull final File resourceRoot,
@NotNull final Project project) {
- try {
- final List<Lesson> lessons = course.getLessons();
- for (int i = 1; i <= lessons.size(); i++) {
- Lesson lesson = lessons.get(i - 1);
- lesson.setIndex(i);
- createLesson(lesson, baseDir, resourceRoot, project);
- }
- baseDir.createChildDirectory(project, EduNames.SANDBOX_DIR);
- File[] files = resourceRoot.listFiles(
- (dir, name) -> !name.contains(EduNames.LESSON) && !name.equals(EduNames.COURSE_META_FILE) && !name.equals(EduNames.HINTS));
- for (File file : files) {
- File dir = new File(baseDir.getPath(), file.getName());
- if (file.isDirectory()) {
- FileUtil.copyDir(file, dir);
- continue;
- }
-
- FileUtil.copy(file, dir);
-
- }
- }
- catch (IOException e) {
- LOG.error(e);
- }
+ try {
+ final List<Lesson> lessons = course.getLessons();
+ for (int i = 1; i <= lessons.size(); i++) {
+ Lesson lesson = lessons.get(i - 1);
+ lesson.setIndex(i);
+ createLesson(lesson, baseDir, resourceRoot, project);
+ }
+ baseDir.createChildDirectory(project, EduNames.SANDBOX_DIR);
- File[] files = resourceRoot.listFiles(new FilenameFilter() {
- @Override
- public boolean accept(File dir, String name) {
- return !name.contains(EduNames.LESSON) && !name.equals(EduNames.COURSE_META_FILE) && !name.equals(EduNames.HINTS);
- }
- });
++ File[] files = resourceRoot.listFiles(
++ (dir, name) -> !name.contains(EduNames.LESSON) && !name.equals(EduNames.COURSE_META_FILE) && !name.equals(EduNames.HINTS));
+ for (File file : files) {
+ File dir = new File(baseDir.getPath(), file.getName());
+ if (file.isDirectory()) {
+ FileUtil.copyDir(file, dir);
+ continue;
+ }
-
++
+ FileUtil.copy(file, dir);
++
+ }
+ }
+ catch (IOException e) {
+ LOG.error(e);
+ }
}
+
}
return null;
}
-
public static List<Lesson> getLessons(int sectionId) throws IOException {
- final SectionContainer sectionContainer = getFromStepic("sections/" + String.valueOf(sectionId), SectionContainer.class);
+ final StepicWrappers.SectionContainer
+ sectionContainer = getFromStepic("sections/" + String.valueOf(sectionId), StepicWrappers.SectionContainer.class);
List<Integer> unitIds = sectionContainer.sections.get(0).units;
final List<Lesson> lessons = new ArrayList<Lesson>();
for (Integer unitId : unitIds) {
final HttpPost request = new HttpPost(stepicApiUrl + "step-sources");
setHeaders(request, "application/json");
final Gson gson = new GsonBuilder().setPrettyPrinting().excludeFieldsWithoutExposeAnnotation().create();
- ApplicationManager.getApplication().invokeLater(new Runnable() {
- @Override
- public void run() {
- final String requestBody = gson.toJson(new StepicWrappers.StepSourceWrapper(project, task, lessonId));
- request.setEntity(new StringEntity(requestBody, ContentType.APPLICATION_JSON));
+ ApplicationManager.getApplication().invokeLater(() -> {
- final String requestBody = gson.toJson(new StepSourceWrapper(project, task, lessonId));
++ final String requestBody = gson.toJson(new StepicWrappers.StepSourceWrapper(project, task, lessonId));
+ request.setEntity(new StringEntity(requestBody, ContentType.APPLICATION_JSON));
- try {
- final CloseableHttpResponse response = ourClient.execute(request);
- final StatusLine line = response.getStatusLine();
- if (line.getStatusCode() != HttpStatus.SC_CREATED) {
- final HttpEntity responseEntity = response.getEntity();
- final String responseString = responseEntity != null ? EntityUtils.toString(responseEntity) : "";
- LOG.error("Failed to push " + responseString);
- }
- }
- catch (IOException e) {
- LOG.error(e.getMessage());
+ try {
+ final CloseableHttpResponse response = ourClient.execute(request);
+ final StatusLine line = response.getStatusLine();
+ if (line.getStatusCode() != HttpStatus.SC_CREATED) {
+ final HttpEntity responseEntity = response.getEntity();
+ final String responseString = responseEntity != null ? EntityUtils.toString(responseEntity) : "";
+ LOG.error("Failed to push " + responseString);
}
}
+ catch (IOException e) {
+ LOG.error(e.getMessage());
+ }
});
}
import com.intellij.ui.OnePixelSplitter;
import com.intellij.util.ui.JBUI;
import com.jetbrains.edu.learning.*;
-import com.jetbrains.edu.learning.core.EduNames;
import com.jetbrains.edu.learning.courseFormat.Course;
import com.jetbrains.edu.learning.courseFormat.TaskFile;
+ import com.jetbrains.edu.learning.stepic.StepicAdaptiveReactionsPanel;
import org.jetbrains.annotations.NotNull;
import javax.swing.*;
<SOURCES />
</library>
</orderEntry>
+ <orderEntry type="module" module-name="platform-api" />
+ <orderEntry type="module" module-name="python-community" />
<orderEntry type="module" module-name="xml" />
+ <orderEntry type="library" name="markdownj" level="project" />
</component>
</module>
public static final String ACTION_ID = "PyCheckAction";
public void check(@NotNull Project project) {
- ApplicationManager.getApplication().runWriteAction(() -> {
- CommandProcessor.getInstance().runUndoTransparentAction(() -> {
- final StudyEditor selectedEditor = StudyUtils.getSelectedStudyEditor(project);
- if (selectedEditor == null) return;
- final StudyState studyState = new StudyState(selectedEditor);
- if (!studyState.isValid()) {
- LOG.info("StudyCheckAction was invoked outside study editor");
- return;
- }
- if (StudyCheckUtils.hasBackgroundProcesses(project)) return;
+ ApplicationManager.getApplication().runWriteAction(() -> CommandProcessor.getInstance().runUndoTransparentAction(() -> {
+ final StudyEditor selectedEditor = StudyUtils.getSelectedStudyEditor(project);
+ if (selectedEditor == null) return;
+ final StudyState studyState = new StudyState(selectedEditor);
+ if (!studyState.isValid()) {
+ LOG.info("StudyCheckAction was invoked outside study editor");
+ return;
+ }
+ if (StudyCheckUtils.hasBackgroundProcesses(project)) return;
-
- if (!runTask(project)) return;
- final Course course = StudyTaskManager.getInstance(project).getCourse();
- if (course != null && !course.isAdaptive() && !runTask(project)) return;
++ final Course course = StudyTaskManager.getInstance(project).getCourse();
++ if (course != null && !course.isAdaptive() && !runTask(project)) return;
- final Task task = studyState.getTask();
- final VirtualFile taskDir = studyState.getTaskDir();
- StudyCheckUtils.flushWindows(task, taskDir);
+ final Task task = studyState.getTask();
+ final VirtualFile taskDir = studyState.getTaskDir();
+ StudyCheckUtils.flushWindows(task, taskDir);
- ApplicationManager.getApplication().invokeLater(
- () -> IdeFocusManager.getInstance(project).requestFocus(studyState.getEditor().getComponent(), true));
+ ApplicationManager.getApplication().invokeLater(
+ () -> IdeFocusManager.getInstance(project).requestFocus(studyState.getEditor().getComponent(), true));
- final StudyTestRunner testRunner = StudyUtils.getTestRunner(task, taskDir);
- Process testProcess = null;
- String commandLine = "";
- try {
- final VirtualFile executablePath = getTaskVirtualFile(studyState, task, taskDir);
- if (executablePath != null) {
- commandLine = executablePath.getPath();
- testProcess = testRunner.createCheckProcess(project, commandLine);
- }
- }
- catch (ExecutionException e) {
- LOG.error(e);
- }
- if (testProcess == null) {
- return;
+ final StudyTestRunner testRunner = StudyUtils.getTestRunner(task, taskDir);
+ Process testProcess = null;
+ String commandLine = "";
+ try {
+ final VirtualFile executablePath = getTaskVirtualFile(studyState, task, taskDir);
+ if (executablePath != null) {
+ commandLine = executablePath.getPath();
+ testProcess = testRunner.createCheckProcess(project, commandLine);
}
- myCheckInProgress.set(true);
- StudyCheckTask checkTask = getCheckTask(project, studyState, testRunner, testProcess, commandLine);
- ProgressManager.getInstance().run(checkTask);
- });
- });
+ }
+ catch (ExecutionException e) {
+ LOG.error(e);
+ }
+ if (testProcess == null) {
+ return;
+ }
+ myCheckInProgress.set(true);
+ StudyCheckTask checkTask = getCheckTask(project, studyState, testRunner, testProcess, commandLine);
+ ProgressManager.getInstance().run(checkTask);
+ }));
}
private static boolean runTask(@NotNull Project project) {