Search in sources :

Example 6 with EduDocumentListener

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;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) TaskFile(com.jetbrains.edu.learning.courseFormat.TaskFile) AnswerPlaceholder(com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder) FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager) IOException(java.io.IOException) Document(com.intellij.openapi.editor.Document) EduDocumentListener(com.jetbrains.edu.learning.core.EduDocumentListener)

Aggregations

EduDocumentListener (com.jetbrains.edu.learning.core.EduDocumentListener)6 Document (com.intellij.openapi.editor.Document)5 VirtualFile (com.intellij.openapi.vfs.VirtualFile)5 TaskFile (com.jetbrains.edu.learning.courseFormat.TaskFile)3 FileDocumentManager (com.intellij.openapi.fileEditor.FileDocumentManager)2 AnswerPlaceholder (com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder)2 Course (com.jetbrains.edu.learning.courseFormat.Course)2 IOException (java.io.IOException)2 ExecutionException (com.intellij.execution.ExecutionException)1 CapturingProcessHandler (com.intellij.execution.process.CapturingProcessHandler)1 ProcessOutput (com.intellij.execution.process.ProcessOutput)1 Editor (com.intellij.openapi.editor.Editor)1 Project (com.intellij.openapi.project.Project)1 TextRange (com.intellij.openapi.util.TextRange)1 ToolWindow (com.intellij.openapi.wm.ToolWindow)1 File (java.io.File)1 Map (java.util.Map)1