public static void runSmartTestProcess( @NotNull final VirtualFile taskDir, @NotNull final StudyTestRunner testRunner, final String taskFileName, @NotNull final TaskFile taskFile, @NotNull final Project project) { final TaskFile answerTaskFile = new TaskFile(); answerTaskFile.name = taskFileName; final VirtualFile virtualFile = taskDir.findChild(taskFileName); if (virtualFile == null) { return; } final VirtualFile answerFile = getCopyWithAnswers(taskDir, virtualFile, taskFile, answerTaskFile); for (final AnswerPlaceholder answerPlaceholder : answerTaskFile.getAnswerPlaceholders()) { final Document document = FileDocumentManager.getInstance().getDocument(virtualFile); if (document == null) { continue; } StudySmartChecker.smartCheck( answerPlaceholder, project, answerFile, answerTaskFile, taskFile, testRunner, virtualFile, document); } StudyUtils.deleteFile(answerFile); }
@Nullable private static AnswerPlaceholder getAnswerPlaceholder(AnActionEvent e) { final Project project = e.getProject(); if (project == null) { return null; } StudyEditor studyEditor = StudyUtils.getSelectedStudyEditor(project); final StudyState studyState = new StudyState(studyEditor); if (studyEditor == null || !studyState.isValid()) { return null; } final Editor editor = studyState.getEditor(); final TaskFile taskFile = studyState.getTaskFile(); return taskFile.getAnswerPlaceholder(editor.getCaretModel().getOffset()); }
private static VirtualFile getCopyWithAnswers( @NotNull final VirtualFile taskDir, @NotNull final VirtualFile file, @NotNull final TaskFile source, @NotNull final TaskFile target) { VirtualFile copy = null; try { copy = file.copy( taskDir, taskDir, file.getNameWithoutExtension() + EduNames.ANSWERS_POSTFIX + "." + file.getExtension()); final FileDocumentManager documentManager = FileDocumentManager.getInstance(); final Document document = documentManager.getDocument(copy); if (document != null) { TaskFile.copy(source, target); EduDocumentListener listener = new EduDocumentListener(target); document.addDocumentListener(listener); for (AnswerPlaceholder answerPlaceholder : target.getAnswerPlaceholders()) { final int start = answerPlaceholder.getOffset(); final int end = start + answerPlaceholder.getRealLength(); final String text = answerPlaceholder.getPossibleAnswer(); document.replaceString(start, end, text); } ApplicationManager.getApplication() .runWriteAction(() -> documentManager.saveDocument(document)); } } catch (IOException e) { LOG.error(e); } return copy; }