fix refresh actions for course creator projects
authorliana.bakradze <liana.bakradze@jetbrains.com>
Sun, 10 Apr 2016 13:33:47 +0000 (16:33 +0300)
committerliana.bakradze <liana.bakradze@jetbrains.com>
Sun, 10 Apr 2016 13:33:47 +0000 (16:33 +0300)
25 files changed:
python/educational-core/course-creator/resources/META-INF/plugin.xml
python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/CCStudyActionListener.java [moved from python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/CCStudyCheckActionListener.java with 78% similarity]
python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/CCVirtualFileListener.java
python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/actions/CCAddAnswerPlaceholder.java
python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/actions/CCDeleteAllAnswerPlaceholdersAction.java
python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/actions/CCFromCourseArchive.java
python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/actions/CCShowPreview.java
python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/ui/CCCreateAnswerPlaceholderDialog.java
python/educational-core/student/resources/META-INF/plugin.xml
python/educational-core/student/src/com/jetbrains/edu/learning/StudyActionListener.java [moved from python/educational-core/student/src/com/jetbrains/edu/learning/StudyCheckActionListener.java with 55% similarity]
python/educational-core/student/src/com/jetbrains/edu/learning/StudyAnswerPlaceholderExtendWordHandler.java
python/educational-core/student/src/com/jetbrains/edu/learning/StudyUtils.java
python/educational-core/student/src/com/jetbrains/edu/learning/actions/StudyCheckAction.java
python/educational-core/student/src/com/jetbrains/edu/learning/actions/StudyFillPlaceholdersAction.java
python/educational-core/student/src/com/jetbrains/edu/learning/actions/StudyRefreshAnswerPlaceholder.java
python/educational-core/student/src/com/jetbrains/edu/learning/actions/StudyRefreshTaskFileAction.java
python/educational-core/student/src/com/jetbrains/edu/learning/checker/StudyCheckUtils.java
python/educational-core/student/src/com/jetbrains/edu/learning/checker/StudySmartChecker.java
python/educational-core/student/src/com/jetbrains/edu/learning/core/EduAnswerPlaceholderPainter.java
python/educational-core/student/src/com/jetbrains/edu/learning/core/EduDocumentListener.java
python/educational-core/student/src/com/jetbrains/edu/learning/core/EduNames.java
python/educational-core/student/src/com/jetbrains/edu/learning/core/EduUtils.java
python/educational-core/student/src/com/jetbrains/edu/learning/courseFormat/AnswerPlaceholder.java
python/educational-core/student/src/com/jetbrains/edu/learning/courseFormat/TaskFile.java
python/educational-core/student/src/com/jetbrains/edu/learning/editor/StudyEditorFactoryListener.java

index 0cf48fbdd13e68eb7bfb874487cc052a021d34d7..def669884579cbe90fe401665d8e2a6bdfee6da4 100644 (file)
@@ -25,7 +25,7 @@
   </extensions>
   <extensions defaultExtensionNs="Edu">
     <studyActionsProvider implementation="com.jetbrains.edu.coursecreator.CCStudyActionsProvider"/>
-    <studyCheckActionListener implementation="com.jetbrains.edu.coursecreator.CCStudyCheckActionListener"/>
+    <studyActionListener implementation="com.jetbrains.edu.coursecreator.CCStudyActionListener"/>
   </extensions>
 
   <project-components>
similarity index 78%
rename from python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/CCStudyCheckActionListener.java
rename to python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/CCStudyActionListener.java
index a3a50fb127a1bc8114229773fbfd579f0af54c6e..921f976971155fe1abec46020a990b5504ed0660 100644 (file)
@@ -7,14 +7,16 @@ import com.intellij.openapi.fileEditor.FileDocumentManager;
 import com.intellij.openapi.project.Project;
 import com.intellij.openapi.vfs.VirtualFile;
 import com.intellij.util.DocumentUtil;
-import com.jetbrains.edu.learning.StudyCheckActionListener;
+import com.jetbrains.edu.learning.StudyActionListener;
 import com.jetbrains.edu.learning.StudyUtils;
+import com.jetbrains.edu.learning.core.EduUtils;
+import com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder;
 import com.jetbrains.edu.learning.courseFormat.Task;
 import com.jetbrains.edu.learning.courseFormat.TaskFile;
 
 import java.util.Map;
 
-public class CCStudyCheckActionListener implements StudyCheckActionListener {
+public class CCStudyActionListener implements StudyActionListener {
   @Override
   public void beforeCheck(AnActionEvent event) {
     Project project = event.getProject();
@@ -52,6 +54,12 @@ public class CCStudyCheckActionListener implements StudyCheckActionListener {
         patternDocument.replaceString(0, patternDocument.getTextLength(), document.getCharsSequence());
         FileDocumentManager.getInstance().saveDocument(patternDocument);
       });
+      TaskFile target = new TaskFile();
+      TaskFile.copy(taskFile, target);
+      for (AnswerPlaceholder placeholder : target.getAnswerPlaceholders()) {
+        placeholder.setUseLength(false);
+      }
+      EduUtils.createStudentDocument(project, target, child, patternDocument);
     }
   }
 }
index 0f1d76cf38f842ad19a855f725f79f3fe667bc7f..967d885cdf2e0c82178f63fc85c24ef7a792b03f 100644 (file)
@@ -41,7 +41,8 @@ public class CCVirtualFileListener extends VirtualFileAdapter {
     if (CCUtils.isTestsFile(project, createdFile)
         || EduNames.TASK_HTML.equals(name)
         || name.contains(EduNames.WINDOW_POSTFIX)
-        || name.contains(EduNames.WINDOWS_POSTFIX)) {
+        || name.contains(EduNames.WINDOWS_POSTFIX)
+        || name.contains(EduNames.ANSWERS_POSTFIX)) {
       return;
     }
 
index d7835cb5db042ca523d8cc6fc90f0e8bc6954fe4..5eae370909ee4d5cbce19b63cc5c9601eb835032 100644 (file)
@@ -5,15 +5,18 @@ import com.intellij.openapi.actionSystem.Presentation;
 import com.intellij.openapi.editor.Document;
 import com.intellij.openapi.editor.Editor;
 import com.intellij.openapi.editor.SelectionModel;
+import com.intellij.openapi.fileEditor.FileDocumentManager;
 import com.intellij.openapi.project.Project;
 import com.intellij.openapi.ui.DialogWrapper;
 import com.intellij.psi.PsiDocumentManager;
 import com.intellij.psi.PsiFile;
 import com.intellij.ui.JBColor;
+import com.intellij.util.DocumentUtil;
 import com.jetbrains.edu.coursecreator.ui.CCCreateAnswerPlaceholderDialog;
 import com.jetbrains.edu.learning.StudyUtils;
 import com.jetbrains.edu.learning.core.EduAnswerPlaceholderPainter;
 import com.jetbrains.edu.learning.core.EduNames;
+import com.jetbrains.edu.learning.core.EduUtils;
 import com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder;
 import com.jetbrains.edu.learning.courseFormat.TaskFile;
 import org.jetbrains.annotations.NotNull;
@@ -57,6 +60,7 @@ public class CCAddAnswerPlaceholder extends CCAnswerPlaceholderAction {
     final AnswerPlaceholder answerPlaceholder = new AnswerPlaceholder();
     answerPlaceholder.setLine(lineNumber);
     answerPlaceholder.setStart(realStart);
+    answerPlaceholder.setUseLength(false);
     String selectedText = model.getSelectedText();
     answerPlaceholder.setPossibleAnswer(selectedText);
 
@@ -70,9 +74,37 @@ public class CCAddAnswerPlaceholder extends CCAnswerPlaceholderAction {
     int index = taskFile.getAnswerPlaceholders().size() + 1;
     answerPlaceholder.setIndex(index);
     taskFile.addAnswerPlaceholder(answerPlaceholder);
+    answerPlaceholder.setTaskFile(taskFile);
     taskFile.sortAnswerPlaceholders();
-    EduAnswerPlaceholderPainter.drawAnswerPlaceholder(editor, answerPlaceholder, false, JBColor.BLUE);
-    EduAnswerPlaceholderPainter.createGuardedBlocks(editor, answerPlaceholder, false);
+
+
+    computeInitialState(project, file, taskFile, document);
+
+    EduAnswerPlaceholderPainter.drawAnswerPlaceholder(editor, answerPlaceholder, JBColor.BLUE);
+    EduAnswerPlaceholderPainter.createGuardedBlocks(editor, answerPlaceholder);
+  }
+
+  private static void computeInitialState(Project project, PsiFile file, TaskFile taskFile, Document document) {
+    Document patternDocument = StudyUtils.getPatternDocument(taskFile, file.getName());
+    if (patternDocument == null) {
+      return;
+    }
+    DocumentUtil.writeInRunUndoTransparentAction(() -> {
+      patternDocument.replaceString(0, patternDocument.getTextLength(), document.getCharsSequence());
+      FileDocumentManager.getInstance().saveDocument(patternDocument);
+    });
+    TaskFile target = new TaskFile();
+    TaskFile.copy(taskFile, target);
+    List<AnswerPlaceholder> placeholders = target.getAnswerPlaceholders();
+    for (AnswerPlaceholder placeholder : placeholders) {
+      placeholder.setUseLength(false);
+    }
+    EduUtils.createStudentDocument(project, target, file.getVirtualFile(), patternDocument);
+
+    for (int i = 0; i < placeholders.size(); i++) {
+      AnswerPlaceholder fromPlaceholder = placeholders.get(i);
+      taskFile.getAnswerPlaceholders().get(i).setInitialState(fromPlaceholder);
+    }
   }
 
   @Override
@@ -98,8 +130,8 @@ public class CCAddAnswerPlaceholder extends CCAnswerPlaceholderAction {
       answerPlaceholders.remove(answerPlaceholder);
       final Editor editor = state.getEditor();
       editor.getMarkupModel().removeAllHighlighters();
-      StudyUtils.drawAllWindows(editor, taskFile, false);
-      EduAnswerPlaceholderPainter.createGuardedBlocks(editor, taskFile, false);
+      StudyUtils.drawAllWindows(editor, taskFile);
+      EduAnswerPlaceholderPainter.createGuardedBlocks(editor, taskFile);
     }
   }
 
index aaabc9777eca99228c1ada2104d58d943d01d375..0916e1b678f45bca515cb6eab0c2ae873240e4c3 100644 (file)
@@ -76,7 +76,7 @@ public class CCDeleteAllAnswerPlaceholdersAction extends DumbAwareAction {
   private static void updateView(@NotNull final Editor editor,
                                  @NotNull final TaskFile taskFile) {
     editor.getMarkupModel().removeAllHighlighters();
-    StudyUtils.drawAllWindows(editor, taskFile, false);
+    StudyUtils.drawAllWindows(editor, taskFile);
   }
 
   @Override
index 7c45e53a920e0c834609fccf75061be2971192b1..97fdb1ec2e574c320da835d07d587956255737bb 100644 (file)
@@ -184,7 +184,7 @@ public class CCFromCourseArchive extends DumbAwareAction {
         ApplicationManager.getApplication().runWriteAction(new Runnable() {
           @Override
           public void run() {
-            final String text = document.getText(TextRange.create(offset, offset + answerPlaceholder.getLength()));
+            final String text = document.getText(TextRange.create(offset, offset + answerPlaceholder.getRealLength()));
             answerPlaceholder.setTaskText(text);
             answerPlaceholder.init();
             final VirtualFile hints = project.getBaseDir().findChild(EduNames.HINTS);
@@ -200,7 +200,7 @@ public class CCFromCourseArchive extends DumbAwareAction {
               }
             }
 
-            document.replaceString(offset, offset + answerPlaceholder.getLength(), answerPlaceholder.getPossibleAnswer());
+            document.replaceString(offset, offset + answerPlaceholder.getRealLength(), answerPlaceholder.getPossibleAnswer());
             FileDocumentManager.getInstance().saveDocument(document);
           }
         });
index dd97b6f7f3377f7a266a50beb39034d13079a30e..39a2ce7f4e0da85110312324fa9f05cb79e681a0 100644 (file)
@@ -148,7 +148,7 @@ public class CCShowPreview extends DumbAwareAction {
       }
     });
     for (AnswerPlaceholder answerPlaceholder : taskFileCopy.getAnswerPlaceholders()) {
-      EduAnswerPlaceholderPainter.drawAnswerPlaceholder(createdEditor, answerPlaceholder, true, JBColor.BLUE);
+      EduAnswerPlaceholderPainter.drawAnswerPlaceholder(createdEditor, answerPlaceholder, JBColor.BLUE);
     }
     JPanel header = new JPanel();
     header.setLayout(new BoxLayout(header, BoxLayout.Y_AXIS));
index 68470f1e40dff66f63eb2223c1882e3b059734a3..5f984acf1d95b40fc0308e99912dcf95a62cdf39 100644 (file)
@@ -53,8 +53,6 @@ public class CCCreateAnswerPlaceholderDialog extends DialogWrapper {
     myAnswerPlaceholder.setTaskText(StringUtil.notNullize(answerPlaceholderText));
     myAnswerPlaceholder.setLength(StringUtil.notNullize(answerPlaceholderText).length());
     myAnswerPlaceholder.setHint(myPanel.getHintText());
-    AnswerPlaceholder.MyInitialState initialState = new AnswerPlaceholder.MyInitialState(myAnswerPlaceholder.getLine(), answerPlaceholderText.length(), myAnswerPlaceholder.getStart());
-    myAnswerPlaceholder.setInitialState(initialState);
     super.doOKAction();
   }
 
index 09972c848d738ab8b6945a99be1eb3bda3e4e822..ff7ddbab656abfe12f28dc34a2549244917fa4f6 100644 (file)
@@ -38,7 +38,7 @@
     <extensionPoint qualifiedName="Edu.studyPluginConfigurator" interface="com.jetbrains.edu.learning.StudyPluginConfigurator"/>
     <extensionPoint qualifiedName="Edu.studyActionsProvider" interface="com.jetbrains.edu.learning.StudyActionsProvider"/>
     <extensionPoint qualifiedName="Edu.studyTwitterPluginConfigurator" interface="com.jetbrains.edu.learning.StudyTwitterPluginConfigurator"/>
-    <extensionPoint qualifiedName="Edu.studyCheckActionListener" interface="com.jetbrains.edu.learning.StudyCheckActionListener"/>
+    <extensionPoint qualifiedName="Edu.studyActionListener" interface="com.jetbrains.edu.learning.StudyActionListener"/>
   </extensionPoints>
 
   <actions>
similarity index 55%
rename from python/educational-core/student/src/com/jetbrains/edu/learning/StudyCheckActionListener.java
rename to python/educational-core/student/src/com/jetbrains/edu/learning/StudyActionListener.java
index 14b3b502d014e01a4936bc02435f901500f419a8..525adc2eddd309efb46d4d38dbdc7cb6dcd363ae 100644 (file)
@@ -3,8 +3,8 @@ package com.jetbrains.edu.learning;
 import com.intellij.openapi.actionSystem.AnActionEvent;
 import com.intellij.openapi.extensions.ExtensionPointName;
 
-public interface StudyCheckActionListener {
-  ExtensionPointName<StudyCheckActionListener> EP_NAME = ExtensionPointName.create("Edu.studyCheckActionListener");
+public interface StudyActionListener {
+  ExtensionPointName<StudyActionListener> EP_NAME = ExtensionPointName.create("Edu.studyActionListener");
 
   void beforeCheck(AnActionEvent event);
 }
index 8ebc8f4dadd65bd1bcd2338cb924b18f3b5016fb..ad364dfc02f293ddf32c7d7fe69d661190a12ab5 100644 (file)
@@ -59,6 +59,6 @@ public class StudyAnswerPlaceholderExtendWordHandler implements ExtendWordSelect
     Document document = FileDocumentManager.getInstance().getDocument(file);
     assert document != null;
     int startOffset = placeholder.getRealStartOffset(document);
-    return Collections.singletonList(new TextRange(startOffset, startOffset + placeholder.getLength()));
+    return Collections.singletonList(new TextRange(startOffset, startOffset + placeholder.getRealLength()));
   }
 }
index 5c64769542d6c9da3d45f83364ceac20f8f1be0e..590bf97c79aa250b2995e548c33a6a6c37248997 100644 (file)
@@ -301,22 +301,18 @@ public class StudyUtils {
   }
 
   public static void drawAllWindows(Editor editor, TaskFile taskFile) {
-    drawAllWindows(editor, taskFile, true);
-  }
-
-  public static void drawAllWindows(Editor editor, TaskFile taskFile, boolean useLength) {
     editor.getMarkupModel().removeAllHighlighters();
     final Project project = editor.getProject();
     if (project == null) return;
     final StudyTaskManager taskManager = StudyTaskManager.getInstance(project);
     for (AnswerPlaceholder answerPlaceholder : taskFile.getAnswerPlaceholders()) {
       final JBColor color = taskManager.getColor(answerPlaceholder);
-      EduAnswerPlaceholderPainter.drawAnswerPlaceholder(editor, answerPlaceholder, useLength, color);
+      EduAnswerPlaceholderPainter.drawAnswerPlaceholder(editor, answerPlaceholder, color);
     }
     final Document document = editor.getDocument();
     EditorActionManager.getInstance()
       .setReadonlyFragmentModificationHandler(document, new EduAnswerPlaceholderDeleteHandler(editor));
-    EduAnswerPlaceholderPainter.createGuardedBlocks(editor, taskFile, useLength);
+    EduAnswerPlaceholderPainter.createGuardedBlocks(editor, taskFile);
     editor.getColorsScheme().setColor(EditorColors.READONLY_FRAGMENT_BACKGROUND_COLOR, null);
   }
 
index a3b0295dbe4139ac5ce866f520dbac0d00b44138..623c7326972a2c9fec64785b2bcd65ccd0ba472c 100644 (file)
@@ -9,7 +9,7 @@ import com.intellij.openapi.project.DumbService;
 import com.intellij.openapi.project.Project;
 import com.intellij.openapi.ui.MessageType;
 import com.intellij.openapi.util.Ref;
-import com.jetbrains.edu.learning.StudyCheckActionListener;
+import com.jetbrains.edu.learning.StudyActionListener;
 import com.jetbrains.edu.learning.StudyUtils;
 import com.jetbrains.edu.learning.checker.StudyCheckUtils;
 import icons.InteractiveLearningIcons;
@@ -38,7 +38,7 @@ public abstract class StudyCheckAction extends StudyActionWithShortcut {
       StudyCheckUtils.showTestResultPopUp("Checking is not available while indexing is in progress", MessageType.WARNING.getPopupBackground(), project);
       return;
     }
-    for (StudyCheckActionListener listener : Extensions.getExtensions(StudyCheckActionListener.EP_NAME)) {
+    for (StudyActionListener listener : Extensions.getExtensions(StudyActionListener.EP_NAME)) {
       listener.beforeCheck(e);
     }
     check(project);
index e65c8a02bd6d85e96aa3e1e51f7c8afc1a352a47..54c8e2c4ac08a2ef999413288d4ae26117f9bc57 100644 (file)
@@ -36,7 +36,7 @@ public class StudyFillPlaceholdersAction extends AnAction {
                   continue;
                 }
                 int offset = placeholder.getRealStartOffset(document);
-                document.deleteString(offset, offset + placeholder.getLength());
+                document.deleteString(offset, offset + placeholder.getRealLength());
                 document.insertString(offset, answer);
               }
             }
index 42a7674c384bd40afdb5011bdc0c58dde3457ee6..7daa54735398c499dfc9c9064f846a9cf916a73b 100644 (file)
@@ -6,9 +6,11 @@ import com.intellij.openapi.application.ApplicationManager;
 import com.intellij.openapi.command.CommandProcessor;
 import com.intellij.openapi.editor.Document;
 import com.intellij.openapi.editor.Editor;
+import com.intellij.openapi.extensions.Extensions;
 import com.intellij.openapi.project.DumbAwareAction;
 import com.intellij.openapi.project.Project;
 import com.intellij.openapi.util.TextRange;
+import com.jetbrains.edu.learning.StudyActionListener;
 import com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder;
 import com.jetbrains.edu.learning.courseFormat.TaskFile;
 import com.jetbrains.edu.learning.StudyState;
@@ -30,6 +32,9 @@ public class StudyRefreshAnswerPlaceholder extends DumbAwareAction {
     if (project == null) {
       return;
     }
+    for (StudyActionListener listener : Extensions.getExtensions(StudyActionListener.EP_NAME)) {
+      listener.beforeCheck(e);
+    }
     final AnswerPlaceholder answerPlaceholder = getAnswerPlaceholder(e);
     if (answerPlaceholder == null) {
       return;
@@ -51,7 +56,7 @@ public class StudyRefreshAnswerPlaceholder extends DumbAwareAction {
           public void run() {
             Document document = studyState.getEditor().getDocument();
             int offset = answerPlaceholder.getRealStartOffset(document);
-            document.deleteString(offset, offset + answerPlaceholder.getLength());
+            document.deleteString(offset, offset + answerPlaceholder.getRealLength());
             document.insertString(offset, text);
           }
         });
index 2a421abdb13157685310a6d14082484a87178cad..fc8acd144103f261fdaef2474297def0563bd87e 100644 (file)
@@ -9,6 +9,7 @@ import com.intellij.openapi.command.CommandProcessor;
 import com.intellij.openapi.diagnostic.Logger;
 import com.intellij.openapi.editor.Document;
 import com.intellij.openapi.editor.Editor;
+import com.intellij.openapi.extensions.Extensions;
 import com.intellij.openapi.keymap.KeymapUtil;
 import com.intellij.openapi.project.Project;
 import com.intellij.openapi.ui.MessageType;
@@ -19,13 +20,14 @@ import com.intellij.openapi.ui.popup.JBPopupFactory;
 import com.intellij.openapi.util.Disposer;
 import com.intellij.openapi.wm.IdeFocusManager;
 import com.intellij.problems.WolfTheProblemSolver;
+import com.jetbrains.edu.learning.StudyActionListener;
+import com.jetbrains.edu.learning.StudyState;
+import com.jetbrains.edu.learning.StudyTaskManager;
+import com.jetbrains.edu.learning.StudyUtils;
 import com.jetbrains.edu.learning.core.EduAnswerPlaceholderPainter;
 import com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder;
 import com.jetbrains.edu.learning.courseFormat.StudyStatus;
 import com.jetbrains.edu.learning.courseFormat.TaskFile;
-import com.jetbrains.edu.learning.StudyState;
-import com.jetbrains.edu.learning.StudyTaskManager;
-import com.jetbrains.edu.learning.StudyUtils;
 import com.jetbrains.edu.learning.editor.StudyEditor;
 import com.jetbrains.edu.learning.navigation.StudyNavigator;
 import org.jetbrains.annotations.NotNull;
@@ -73,7 +75,7 @@ public class StudyRefreshTaskFileAction extends StudyActionWithShortcut {
     WolfTheProblemSolver.getInstance(project).clearProblems(studyState.getVirtualFile());
     taskFile.setHighlightErrors(false);
     StudyUtils.drawAllWindows(editor, taskFile);
-    EduAnswerPlaceholderPainter.createGuardedBlocks(editor, taskFile, true);
+    EduAnswerPlaceholderPainter.createGuardedBlocks(editor, taskFile);
     ApplicationManager.getApplication().invokeLater(new Runnable() {
       @Override
       public void run() {
@@ -147,6 +149,9 @@ public class StudyRefreshTaskFileAction extends StudyActionWithShortcut {
   public void actionPerformed(@NotNull AnActionEvent event) {
     final Project project = event.getProject();
     if (project != null) {
+      for (StudyActionListener listener : Extensions.getExtensions(StudyActionListener.EP_NAME)) {
+        listener.beforeCheck(event);
+      }
       refresh(project);
     }
   }
index 9fe0ae84ae6bf61c7a2f188cdff3853e07a69c9d..33c626a05bcb5e191afc515167ef0d59d9378412 100644 (file)
@@ -21,6 +21,7 @@ import com.intellij.openapi.wm.WindowManager;
 import com.intellij.openapi.wm.ex.StatusBarEx;
 import com.intellij.openapi.wm.ex.WindowManagerEx;
 import com.jetbrains.edu.learning.core.EduDocumentListener;
+import com.jetbrains.edu.learning.core.EduNames;
 import com.jetbrains.edu.learning.core.EduUtils;
 import com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder;
 import com.jetbrains.edu.learning.courseFormat.Task;
@@ -38,7 +39,6 @@ import java.util.List;
 import java.util.Map;
 
 public class StudyCheckUtils {
-  private static final String ANSWERS_POSTFIX = "_answers";
   private static final Logger LOG = Logger.getInstance(StudyCheckUtils.class);
 
   private StudyCheckUtils() {
@@ -143,7 +143,7 @@ public class StudyCheckUtils {
     VirtualFile copy = null;
     try {
 
-      copy = file.copy(taskDir, taskDir, file.getNameWithoutExtension() + ANSWERS_POSTFIX + "." + file.getExtension());
+      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) {
@@ -155,7 +155,7 @@ public class StudyCheckUtils {
             continue;
           }
           final int start = answerPlaceholder.getRealStartOffset(document);
-          final int end = start + answerPlaceholder.getLength();
+          final int end = start + answerPlaceholder.getRealLength();
           final String text = answerPlaceholder.getPossibleAnswer();
           document.replaceString(start, end, text);
         }
index 7cd660635d8424315146297de30d4d0f777c52b8..14870203abac9279d8565aa9cb28ebc6d84c0c1a 100644 (file)
@@ -54,10 +54,10 @@ public class StudySmartChecker {
         EduDocumentListener listener = new EduDocumentListener(windowTaskFile);
         windowDocument.addDocumentListener(listener);
         int start = placeholder.getRealStartOffset(windowDocument);
-        int end = start + placeholder.getLength();
+        int end = start + placeholder.getRealLength();
         final AnswerPlaceholder userAnswerPlaceholder = usersTaskFile.getAnswerPlaceholders().get(placeholder.getIndex());
         int userStart = userAnswerPlaceholder.getRealStartOffset(usersDocument);
-        int userEnd = userStart + userAnswerPlaceholder.getLength();
+        int userEnd = userStart + userAnswerPlaceholder.getRealLength();
         String text = usersDocument.getText(new TextRange(userStart, userEnd));
         windowDocument.replaceString(start, end, text);
         ApplicationManager.getApplication().runWriteAction(() -> {
index 528b58e26102e2da4e61e10408afd0510be0e637..81fcf85fb989d219a8c8a02e360a74f1f127439d 100644 (file)
@@ -26,9 +26,9 @@ public class EduAnswerPlaceholderPainter {
   }
 
   public static void drawAnswerPlaceholder(@NotNull final Editor editor, @NotNull final AnswerPlaceholder placeholder,
-                                           boolean useLength, @NotNull final JBColor color) {
+                                           @NotNull final JBColor color) {
     final Document document = editor.getDocument();
-    if (useLength && !placeholder.isValid(document)) {
+    if (!placeholder.isValid(document)) {
       return;
     }
     EditorColorsScheme scheme = EditorColorsManager.getInstance().getGlobalScheme();
@@ -37,10 +37,8 @@ public class EduAnswerPlaceholderPainter {
     final Project project = editor.getProject();
     assert project != null;
     final int startOffset = placeholder.getRealStartOffset(document);
-    final int length = placeholder.getLength();
-    final int replacementLength = placeholder.getPossibleAnswerLength();
-    int highlighterLength = useLength ? length : replacementLength;
-    final int endOffset = startOffset + highlighterLength;
+    final int length = placeholder.getRealLength();
+    final int endOffset = startOffset + length;
     textAttributes.setEffectColor(color);
     RangeHighlighter
       highlighter = editor.getMarkupModel().addRangeHighlighter(startOffset, endOffset, PLACEHOLDERS_LAYER,
@@ -56,20 +54,20 @@ public class EduAnswerPlaceholderPainter {
   }
 
 
-  public static void createGuardedBlocks(@NotNull final Editor editor, TaskFile taskFile, boolean useLength) {
+  public static void createGuardedBlocks(@NotNull final Editor editor, TaskFile taskFile) {
     for (AnswerPlaceholder answerPlaceholder : taskFile.getAnswerPlaceholders()) {
-      createGuardedBlocks(editor, answerPlaceholder, useLength);
+      createGuardedBlocks(editor, answerPlaceholder);
     }
   }
 
-  public static void createGuardedBlocks(@NotNull final Editor editor, AnswerPlaceholder placeholder, boolean useLength) {
+  public static void createGuardedBlocks(@NotNull final Editor editor, AnswerPlaceholder placeholder) {
     Document document = editor.getDocument();
     if (document instanceof DocumentImpl) {
       DocumentImpl documentImpl = (DocumentImpl)document;
       List<RangeMarker> blocks = documentImpl.getGuardedBlocks();
-      if (useLength && !placeholder.isValid(document)) return;
+      if (!placeholder.isValid(document)) return;
       int start = placeholder.getRealStartOffset(document);
-      final int length = useLength ? placeholder.getLength() : placeholder.getPossibleAnswerLength();
+      final int length = placeholder.getRealLength();
       int end = start + length;
       if (start != 0) {
         createGuardedBlock(editor, blocks, start - 1, start);
index 77e02bb151540e409a20aab1899ac831c8325a5d..8e43bf57b26b55bca2c39011e2da909bfdc8a918 100644 (file)
@@ -18,29 +18,19 @@ import java.util.List;
 public class EduDocumentListener extends DocumentAdapter {
   private final TaskFile myTaskFile;
   private final boolean myTrackLength;
-  private final boolean usePossibleAnswerLength;
   private final List<AnswerPlaceholderWrapper> myAnswerPlaceholders = new ArrayList<AnswerPlaceholderWrapper>();
 
 
   public EduDocumentListener(TaskFile taskFile) {
     myTaskFile = taskFile;
     myTrackLength = true;
-    usePossibleAnswerLength = false;
   }
 
   public EduDocumentListener(TaskFile taskFile, boolean trackLength) {
     myTaskFile = taskFile;
     myTrackLength = trackLength;
-    usePossibleAnswerLength = false;
   }
 
-  public EduDocumentListener(TaskFile taskFile, boolean trackLength, boolean usePossibleAnswerLength) {
-    myTaskFile = taskFile;
-    myTrackLength = trackLength;
-    this.usePossibleAnswerLength = usePossibleAnswerLength;
-  }
-
-
   //remembering old end before document change because of problems
   // with fragments containing "\n"
   @Override
@@ -53,7 +43,7 @@ public class EduDocumentListener extends DocumentAdapter {
     myAnswerPlaceholders.clear();
     for (AnswerPlaceholder answerPlaceholder : myTaskFile.getAnswerPlaceholders()) {
       int twStart = answerPlaceholder.getRealStartOffset(document);
-      int length = usePossibleAnswerLength ? answerPlaceholder.getPossibleAnswerLength() : answerPlaceholder.getLength();
+      int length = answerPlaceholder.getRealLength();
       int twEnd = twStart + length;
       myAnswerPlaceholders.add(new AnswerPlaceholderWrapper(answerPlaceholder, twStart, twEnd));
     }
@@ -85,7 +75,7 @@ public class EduDocumentListener extends DocumentAdapter {
         int length = twEnd - twStart;
         answerPlaceholder.setLine(line);
         answerPlaceholder.setStart(start);
-        if (usePossibleAnswerLength) {
+        if (!answerPlaceholder.getUseLength()) {
           answerPlaceholder.setPossibleAnswer(document.getText(TextRange.create(twStart, twStart + length)));
         }
         else if (myTrackLength) {
index e076fdd922127cc972a3e3b711222010f9b41e06..a6062cb25339e767b06d7420d9984a4afc9eef42 100644 (file)
@@ -32,6 +32,7 @@ public class EduNames {
   public static final String USER_TEST_OUTPUT = "output";
   public static final String WINDOW_POSTFIX = "_window.";
   public static final String WINDOWS_POSTFIX = "_windows";
+  public static final String ANSWERS_POSTFIX = "_answers";
   public static final String USER_TESTS = "userTests";
   public static final String TESTS_FILE = "tests.py";
   public static final String TEST_HELPER = "test_helper.py";
index 4b4870673a23af97a98b5a34bdcc20c7c53c174c..b095bc94815e2b2313d8e877d17ff977e1b432f5 100644 (file)
@@ -95,7 +95,7 @@ public class EduUtils {
         fileWindows = taskDir.createChildData(taskFile, name);
         printWriter = new PrintWriter(new FileOutputStream(fileWindows.getPath()));
         for (AnswerPlaceholder answerPlaceholder : taskFile.getAnswerPlaceholders()) {
-          int length = useLength ? answerPlaceholder.getLength() : answerPlaceholder.getPossibleAnswerLength();
+          int length = answerPlaceholder.getRealLength();
           if (!answerPlaceholder.isValid(document, length)) {
             printWriter.println("#educational_plugin_window = ");
             continue;
@@ -192,6 +192,13 @@ public class EduUtils {
         });
       }
     }, "Create Student File", "Create Student File");
+    createStudentDocument(project, taskFile, file, document);
+  }
+
+  public static void createStudentDocument(@NotNull Project project,
+                                           @NotNull TaskFile taskFile,
+                                           VirtualFile file,
+                                           final Document document) {
     EduDocumentListener listener = new EduDocumentListener(taskFile, false);
     document.addDocumentListener(listener);
     taskFile.sortAnswerPlaceholders();
index d4bbf56287f774dc87963e0fd0676e22ea94f350..72f2b422b7f8be655d2dfd899686e0cc5b02108a 100644 (file)
@@ -25,6 +25,7 @@ public class AnswerPlaceholder {
   private MyInitialState myInitialState;
   private StudyStatus myStatus = StudyStatus.Uninitialized;
   private boolean mySelected = false;
+  private boolean myUseLength = true;
 
   @Transient private TaskFile myTaskFile;
 
@@ -45,6 +46,9 @@ public class AnswerPlaceholder {
     myIndex = index;
   }
 
+  /**
+   * in actions {@link AnswerPlaceholder#getRealLength()} should be used
+   */
   public int getLength() {
     return length;
   }
@@ -138,6 +142,9 @@ public class AnswerPlaceholder {
     line = myInitialState.myLine;
     start = myInitialState.myStart;
     length = myInitialState.myLength;
+    if (!getUseLength()) {
+      possibleAnswer = myTaskText;
+    }
   }
 
   public StudyStatus getStatus() {
@@ -157,7 +164,26 @@ public class AnswerPlaceholder {
   }
 
   public void init() {
-    setInitialState(new MyInitialState(line, possibleAnswer.length(), start));
+    setInitialState(new MyInitialState(line, myTaskText.length(), start));
+  }
+
+  public boolean getUseLength() {
+    return myUseLength;
+  }
+
+  /**
+   * @return length or possible answer length
+   */
+  public int getRealLength() {
+    return myUseLength ? getLength() : getPossibleAnswerLength();
+  }
+
+  public void setUseLength(boolean useLength) {
+    myUseLength = useLength;
+  }
+
+  public void setInitialState(AnswerPlaceholder placeholder) {
+    setInitialState(new MyInitialState(placeholder.line, placeholder.length, placeholder.start));
   }
 
   public static class MyInitialState {
index ceb8dd92ac2ce2121db04be9ed461b0151a8441a..0bd0fd22cca13d834edee0ba6b66fc6cbed5e4d8 100644 (file)
@@ -95,7 +95,7 @@ public class TaskFile {
     for (AnswerPlaceholder placeholder : myAnswerPlaceholders) {
       if (placeholder.getLine() <= line) {
         int realStartOffset = placeholder.getRealStartOffset(document);
-        int placeholderLength = useAnswerLength ? placeholder.getPossibleAnswerLength() : placeholder.getLength();
+        int placeholderLength = placeholder.getRealLength();
         final int length = placeholderLength > 0 ? placeholderLength : 0;
         int endOffset = realStartOffset + length;
         if (realStartOffset <= offset && offset <= endOffset) {
index 55a784a534cb46541d74d78a78c3435b6a050071..d345ddf22086c3e15fb52f3cd431c3262a83f347 100644 (file)
@@ -16,12 +16,12 @@ import com.intellij.openapi.wm.ToolWindow;
 import com.intellij.openapi.wm.ToolWindowManager;
 import com.intellij.problems.WolfTheProblemSolver;
 import com.jetbrains.edu.learning.StudyTaskManager;
+import com.jetbrains.edu.learning.StudyUtils;
 import com.jetbrains.edu.learning.core.EduDocumentListener;
 import com.jetbrains.edu.learning.core.EduNames;
 import com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder;
 import com.jetbrains.edu.learning.courseFormat.Course;
 import com.jetbrains.edu.learning.courseFormat.TaskFile;
-import com.jetbrains.edu.learning.StudyUtils;
 import com.jetbrains.edu.learning.navigation.StudyNavigator;
 import com.jetbrains.edu.learning.ui.StudyToolWindowFactory;
 import org.jetbrains.annotations.NotNull;
@@ -48,7 +48,7 @@ public class StudyEditorFactoryListener implements EditorFactoryListener {
         return;
       }
       int startOffset = answerPlaceholder.getRealStartOffset(editor.getDocument());
-      editor.getSelectionModel().setSelection(startOffset, startOffset + answerPlaceholder.getLength());
+      editor.getSelectionModel().setSelection(startOffset, startOffset + answerPlaceholder.getRealLength());
       answerPlaceholder.setSelected(true);
     }
   }
@@ -84,12 +84,12 @@ public class StudyEditorFactoryListener implements EditorFactoryListener {
                     return;
                   }
 
+                  StudyEditor.addDocumentListener(document, new EduDocumentListener(taskFile, true));
+
                   if (!taskFile.getAnswerPlaceholders().isEmpty()) {
                     StudyNavigator.navigateToFirstAnswerPlaceholder(editor, taskFile);
                     boolean isStudyProject = EduNames.STUDY.equals(course.getCourseType());
-                    StudyEditor.addDocumentListener(document, new EduDocumentListener(taskFile, true,
-                                                                                      !isStudyProject));
-                    StudyUtils.drawAllWindows(editor, taskFile, isStudyProject);
+                    StudyUtils.drawAllWindows(editor, taskFile);
                     if (isStudyProject) {
                       editor.addEditorMouseListener(new WindowSelectionListener(taskFile));
                     }