use of com.jetbrains.edu.learning.core.EduDocumentListener in project intellij-community by JetBrains.
the class StudyCheckUtils method getCopyWithAnswers.
private static Pair<VirtualFile, TaskFile> getCopyWithAnswers(@NotNull final VirtualFile taskDir, @NotNull final VirtualFile file, @NotNull final TaskFile source) {
try {
VirtualFile answerFile = file.copy(taskDir, taskDir, file.getNameWithoutExtension() + EduNames.ANSWERS_POSTFIX + "." + file.getExtension());
final FileDocumentManager documentManager = FileDocumentManager.getInstance();
final Document document = documentManager.getDocument(answerFile);
if (document != null) {
TaskFile answerTaskFile = source.getTask().copy().getTaskFile(StudyUtils.pathRelativeToTask(file));
if (answerTaskFile == null) {
return null;
}
EduDocumentListener listener = new EduDocumentListener(answerTaskFile);
document.addDocumentListener(listener);
for (AnswerPlaceholder answerPlaceholder : answerTaskFile.getActivePlaceholders()) {
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));
return Pair.create(answerFile, answerTaskFile);
}
} catch (IOException e) {
LOG.error(e);
}
return null;
}
Aggregations