use of com.jetbrains.edu.learning.core.EduDocumentListener in project intellij-community by JetBrains.
the class StudySmartChecker method smartCheck.
public static void smartCheck(@NotNull final AnswerPlaceholder placeholder, @NotNull final Project project, @NotNull final VirtualFile answerFile, @NotNull final TaskFile answerTaskFile, @NotNull final TaskFile usersTaskFile, @NotNull final StudyTestRunner testRunner, @NotNull final VirtualFile virtualFile, @NotNull final Document usersDocument) {
VirtualFile fileWindows = null;
File resourceFile = null;
VirtualFile windowCopy = null;
try {
final int index = placeholder.getIndex();
String windowCopyName = answerFile.getNameWithoutExtension() + index + EduNames.WINDOW_POSTFIX + answerFile.getExtension();
windowCopy = answerFile.copy(project, answerFile.getParent(), windowCopyName);
final FileDocumentManager documentManager = FileDocumentManager.getInstance();
final Document windowDocument = documentManager.getDocument(windowCopy);
if (windowDocument != null) {
resourceFile = StudyUtils.copyResourceFile(virtualFile.getName(), windowCopy.getName(), project, usersTaskFile.getTask());
TaskFile windowTaskFile = answerTaskFile.getTask().copy().getTaskFile(StudyUtils.pathRelativeToTask(virtualFile));
if (windowTaskFile == null) {
return;
}
EduDocumentListener listener = new EduDocumentListener(windowTaskFile);
windowDocument.addDocumentListener(listener);
int start = placeholder.getOffset();
int end = start + placeholder.getRealLength();
final AnswerPlaceholder userAnswerPlaceholder = usersTaskFile.getAnswerPlaceholders().get(placeholder.getIndex());
int userStart = userAnswerPlaceholder.getOffset();
int userEnd = userStart + userAnswerPlaceholder.getRealLength();
String text = usersDocument.getText(new TextRange(userStart, userEnd));
windowDocument.replaceString(start, end, text);
ApplicationManager.getApplication().runWriteAction(() -> documentManager.saveDocument(windowDocument));
fileWindows = EduUtils.flushWindows(windowTaskFile, windowCopy);
Process smartTestProcess = testRunner.createCheckProcess(project, windowCopy.getPath());
final CapturingProcessHandler handler = new CapturingProcessHandler(smartTestProcess, null, windowCopy.getPath());
final ProcessOutput output = handler.runProcess();
final Course course = StudyTaskManager.getInstance(project).getCourse();
if (course != null) {
boolean res = StudyTestsOutputParser.getTestsOutput(output, course.isAdaptive()).isSuccess();
StudyTaskManager.getInstance(project).setStatus(userAnswerPlaceholder, res ? StudyStatus.Solved : StudyStatus.Failed);
}
}
} catch (ExecutionException | IOException e) {
LOG.error(e);
} finally {
StudyUtils.deleteFile(windowCopy);
StudyUtils.deleteFile(fileWindows);
if (resourceFile != null && resourceFile.exists() && !resourceFile.delete()) {
LOG.error("failed to delete", resourceFile.getPath());
}
}
}
use of com.jetbrains.edu.learning.core.EduDocumentListener in project intellij-community by JetBrains.
the class StudySubtaskUtils method switchStep.
/***
* @param toSubtaskIndex from 0 to subtaskNum - 1
*/
public static void switchStep(@NotNull Project project, @NotNull Task task, int toSubtaskIndex, boolean navigateToTask) {
if (toSubtaskIndex == task.getActiveSubtaskIndex()) {
return;
}
VirtualFile taskDir = task.getTaskDir(project);
if (taskDir == null) {
return;
}
VirtualFile srcDir = taskDir.findChild(EduNames.SRC);
if (srcDir != null) {
taskDir = srcDir;
}
int fromSubtaskIndex = task.getActiveSubtaskIndex();
for (Map.Entry<String, TaskFile> entry : task.getTaskFiles().entrySet()) {
String name = entry.getKey();
VirtualFile virtualFile = taskDir.findFileByRelativePath(name);
if (virtualFile == null) {
continue;
}
TaskFile taskFile = entry.getValue();
Document document = FileDocumentManager.getInstance().getDocument(virtualFile);
if (document == null) {
continue;
}
EduDocumentListener listener = null;
if (!FileEditorManager.getInstance(project).isFileOpen(virtualFile)) {
listener = new EduDocumentListener(taskFile, true);
document.addDocumentListener(listener);
}
updatePlaceholderTexts(document, taskFile, fromSubtaskIndex, toSubtaskIndex);
if (listener != null) {
document.removeDocumentListener(listener);
}
UndoManager.getInstance(project).nonundoableActionPerformed(DocumentReferenceManager.getInstance().create(document), false);
EditorNotifications.getInstance(project).updateNotifications(virtualFile);
if (StudyUtils.isStudentProject(project)) {
WolfTheProblemSolver.getInstance(project).clearProblems(virtualFile);
taskFile.setHighlightErrors(false);
}
}
transformTestFile(project, toSubtaskIndex, taskDir);
task.setActiveSubtaskIndex(toSubtaskIndex);
updateUI(project, task, taskDir, navigateToTask);
for (StudySubtaskChangeListener listener : Extensions.getExtensions(StudySubtaskChangeListener.EP_NAME)) {
listener.subtaskChanged(project, task, fromSubtaskIndex, toSubtaskIndex);
}
}
use of com.jetbrains.edu.learning.core.EduDocumentListener in project intellij-community by JetBrains.
the class CCFromCourseArchive method createAnswerFile.
public static void createAnswerFile(@NotNull final Project project, @NotNull final VirtualFile userFileDir, @NotNull final Map.Entry<String, TaskFile> taskFileEntry) {
final String name = taskFileEntry.getKey();
final TaskFile taskFile = taskFileEntry.getValue();
VirtualFile file = userFileDir.findFileByRelativePath(name);
assert file != null;
final Document originDocument = FileDocumentManager.getInstance().getDocument(file);
if (originDocument == null) {
return;
}
final Document document = FileDocumentManager.getInstance().getDocument(file);
if (document == null)
return;
CommandProcessor.getInstance().executeCommand(project, () -> ApplicationManager.getApplication().runWriteAction(() -> document.replaceString(0, document.getTextLength(), originDocument.getCharsSequence())), "Create answer document", "Create answer document");
EduDocumentListener listener = new EduDocumentListener(taskFile, false);
document.addDocumentListener(listener);
taskFile.sortAnswerPlaceholders();
for (int i = taskFile.getActivePlaceholders().size() - 1; i >= 0; i--) {
final AnswerPlaceholder answerPlaceholder = taskFile.getAnswerPlaceholders().get(i);
replaceAnswerPlaceholder(project, document, answerPlaceholder);
}
CommandProcessor.getInstance().executeCommand(project, () -> ApplicationManager.getApplication().runWriteAction(() -> FileDocumentManager.getInstance().saveDocument(document)), "x", "qwe");
document.removeDocumentListener(listener);
}
use of com.jetbrains.edu.learning.core.EduDocumentListener in project intellij-community by JetBrains.
the class StudyEditor method removeListener.
public static void removeListener(Document document) {
final EduDocumentListener listener = myDocumentListeners.get(document);
if (listener != null) {
document.removeDocumentListener(listener);
}
myDocumentListeners.remove(document);
}
use of com.jetbrains.edu.learning.core.EduDocumentListener in project intellij-community by JetBrains.
the class StudyEditorFactoryListener method editorCreated.
@Override
public void editorCreated(@NotNull final EditorFactoryEvent event) {
final Editor editor = event.getEditor();
final Project project = editor.getProject();
if (project == null) {
return;
}
final Document document = editor.getDocument();
final VirtualFile openedFile = FileDocumentManager.getInstance().getFile(document);
if (openedFile != null) {
final TaskFile taskFile = StudyUtils.getTaskFile(project, openedFile);
if (taskFile != null) {
WolfTheProblemSolver.getInstance(project).clearProblems(openedFile);
final ToolWindow studyToolWindow = ToolWindowManager.getInstance(project).getToolWindow(StudyToolWindowFactory.STUDY_TOOL_WINDOW);
if (studyToolWindow != null) {
StudyUtils.updateToolWindows(project);
studyToolWindow.show(null);
}
Course course = StudyTaskManager.getInstance(project).getCourse();
if (course == null) {
return;
}
StudyEditor.addDocumentListener(document, new EduDocumentListener(taskFile, true));
if (!taskFile.getAnswerPlaceholders().isEmpty()) {
StudyNavigator.navigateToFirstAnswerPlaceholder(editor, taskFile);
boolean isStudyProject = EduNames.STUDY.equals(course.getCourseMode());
StudyUtils.drawAllAnswerPlaceholders(editor, taskFile);
if (isStudyProject) {
editor.addEditorMouseListener(new WindowSelectionListener(taskFile));
}
}
}
}
}
Aggregations