summaryrefslogtreecommitdiff
path: root/python/edu/course-creator/src/org/jetbrains/plugins/coursecreator/CCEditorFactoryListener.java
diff options
context:
space:
mode:
Diffstat (limited to 'python/edu/course-creator/src/org/jetbrains/plugins/coursecreator/CCEditorFactoryListener.java')
-rw-r--r--python/edu/course-creator/src/org/jetbrains/plugins/coursecreator/CCEditorFactoryListener.java80
1 files changed, 80 insertions, 0 deletions
diff --git a/python/edu/course-creator/src/org/jetbrains/plugins/coursecreator/CCEditorFactoryListener.java b/python/edu/course-creator/src/org/jetbrains/plugins/coursecreator/CCEditorFactoryListener.java
new file mode 100644
index 000000000000..1eb8690aad22
--- /dev/null
+++ b/python/edu/course-creator/src/org/jetbrains/plugins/coursecreator/CCEditorFactoryListener.java
@@ -0,0 +1,80 @@
+package org.jetbrains.plugins.coursecreator;
+
+import com.intellij.openapi.editor.Document;
+import com.intellij.openapi.editor.Editor;
+import com.intellij.openapi.editor.event.EditorFactoryEvent;
+import com.intellij.openapi.editor.event.EditorFactoryListener;
+import com.intellij.openapi.fileEditor.FileDocumentManager;
+import com.intellij.openapi.project.Project;
+import com.intellij.openapi.vfs.VirtualFile;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.plugins.coursecreator.format.*;
+
+public class CCEditorFactoryListener implements EditorFactoryListener {
+ @Override
+ public void editorCreated(@NotNull EditorFactoryEvent event) {
+ Editor editor = event.getEditor();
+ Project project = editor.getProject();
+ if (project == null) {
+ return;
+ }
+ VirtualFile virtualFile = FileDocumentManager.getInstance().getFile(editor.getDocument());
+ if (virtualFile == null) {
+ return;
+ }
+ Course course = CCProjectService.getInstance(project).getCourse();
+ if (course == null) {
+ return;
+ }
+ final VirtualFile taskDir = virtualFile.getParent();
+ if (taskDir == null || !taskDir.getName().contains("task")) {
+ return;
+ }
+ final VirtualFile lessonDir = taskDir.getParent();
+ if (lessonDir == null) return;
+ final Lesson lesson = course.getLesson(lessonDir.getName());
+ final Task task = lesson.getTask(taskDir.getName());
+ final TaskFile taskFile = task.getTaskFile(virtualFile.getName());
+ TaskFileModificationListener listener = new TaskFileModificationListener(taskFile);
+ CCProjectService.addDocumentListener(editor.getDocument(), listener);
+ editor.getDocument().addDocumentListener(listener);
+ CCProjectService.drawTaskWindows(virtualFile, editor, course);
+ }
+
+ @Override
+ public void editorReleased(@NotNull EditorFactoryEvent event) {
+ Editor editor = event.getEditor();
+ Document document = editor.getDocument();
+ StudyDocumentListener listener = CCProjectService.getListener(document);
+ if (listener != null) {
+ document.removeDocumentListener(listener);
+ CCProjectService.removeListener(document);
+ }
+ editor.getMarkupModel().removeAllHighlighters();
+ editor.getSelectionModel().removeSelection();
+ }
+
+ private class TaskFileModificationListener extends StudyDocumentListener {
+
+ private final TaskFile myTaskFile;
+
+ public TaskFileModificationListener(TaskFile taskFile) {
+ super(taskFile);
+ myTaskFile = taskFile;
+ }
+
+ @Override
+ protected void updateTaskWindowLength(CharSequence fragment, TaskWindow taskWindow, int change) {
+ int newLength = taskWindow.getReplacementLength() + change;
+ taskWindow.setReplacementLength(newLength <= 0 ? 0 : newLength);
+ if (fragment.equals("\n")) {
+ taskWindow.setReplacementLength(taskWindow.getLength() + 1);
+ }
+ }
+
+ @Override
+ protected boolean needModify() {
+ return myTaskFile.isTrackChanges();
+ }
+ }
+}