EDU-316 Select Next Answer should depend on caret position
authorLiana Bakradze <liana.bakradze@jetbrains.com>
Thu, 6 Aug 2015 10:50:18 +0000 (13:50 +0300)
committerLiana Bakradze <liana.bakradze@jetbrains.com>
Thu, 6 Aug 2015 10:50:18 +0000 (13:50 +0300)
python/educational/interactive-learning/src/com/jetbrains/edu/learning/actions/StudyWindowNavigationAction.java
python/educational/interactive-learning/src/com/jetbrains/edu/learning/navigation/StudyNavigator.java
python/educational/src/com/jetbrains/edu/courseFormat/TaskFile.java

index 7b907496cd1802748dfef5358546b86530649a2d..7c79d3b01060cef3f5aff7580ebbd933c92ff266 100644 (file)
@@ -2,6 +2,7 @@ package com.jetbrains.edu.learning.actions;
 
 import com.intellij.openapi.actionSystem.AnActionEvent;
 import com.intellij.openapi.editor.Editor;
+import com.intellij.openapi.editor.LogicalPosition;
 import com.intellij.openapi.fileEditor.FileDocumentManager;
 import com.intellij.openapi.project.DumbAwareAction;
 import com.intellij.openapi.project.Project;
@@ -29,7 +30,7 @@ abstract public class StudyWindowNavigationAction extends DumbAwareAction {
         if (openedFile != null) {
           final TaskFile selectedTaskFile = StudyUtils.getTaskFile(project, openedFile);
           if (selectedTaskFile != null) {
-            final AnswerPlaceholder selectedAnswerPlaceholder = selectedTaskFile.getSelectedAnswerPlaceholder();
+            final AnswerPlaceholder selectedAnswerPlaceholder = getSelectedAnswerPlaceholder(selectedEditor, selectedTaskFile);
             if (selectedAnswerPlaceholder == null) {
               return;
             }
@@ -39,12 +40,17 @@ abstract public class StudyWindowNavigationAction extends DumbAwareAction {
             }
             StudyNavigator.navigateToAnswerPlaceholder(selectedEditor, nextAnswerPlaceholder, selectedTaskFile);
             selectedEditor.getSelectionModel().removeSelection();
-            selectedTaskFile.setSelectedAnswerPlaceholder(nextAnswerPlaceholder);
             }
           }
         }
       }
 
+  @Nullable
+  private static AnswerPlaceholder getSelectedAnswerPlaceholder(@NotNull final Editor editor, @NotNull final TaskFile file) {
+    LogicalPosition position = editor.getCaretModel().getLogicalPosition();
+    return file.getAnswerPlaceholder(editor.getDocument(), position);
+  }
+
   @Nullable
   protected abstract AnswerPlaceholder getNextAnswerPlaceholder(@NotNull final AnswerPlaceholder window);
 
index 0f84f67c19c38185b638c9bfb65f4c624bf10375..1476fd0ba37201b8b3902ab0bec1dce6930408d0 100644 (file)
@@ -81,7 +81,6 @@ public class StudyNavigator {
     if (!answerPlaceholder.isValid(editor.getDocument())) {
       return;
     }
-    taskFile.setSelectedAnswerPlaceholder(answerPlaceholder);
     LogicalPosition placeholderStart = new LogicalPosition(answerPlaceholder.getLine(), answerPlaceholder.getStart());
     editor.getCaretModel().moveToLogicalPosition(placeholderStart);
   }
index 4e5d7544115ee7877d78294ab870720e01b15b8d..12084a909affb1313cb3ae42a074173edda9f069 100644 (file)
@@ -28,8 +28,6 @@ public class TaskFile implements StudyOrderable {
   @Expose
   public String text;
   @Transient private Task myTask;
-  @Transient
-  private AnswerPlaceholder mySelectedAnswerPlaceholder = null;
   private boolean myUserCreated = false;
   private boolean myTrackChanges = true;
   private boolean myHighlightErrors = false;
@@ -46,21 +44,6 @@ public class TaskFile implements StudyOrderable {
     }
   }
 
-  @Nullable
-  @Transient
-  public AnswerPlaceholder getSelectedAnswerPlaceholder() {
-    return mySelectedAnswerPlaceholder;
-  }
-
-  public void setSelectedAnswerPlaceholder(@NotNull final AnswerPlaceholder selectedAnswerPlaceholder) {
-    if (selectedAnswerPlaceholder.getTaskFile() == this) {
-      mySelectedAnswerPlaceholder = selectedAnswerPlaceholder;
-    }
-    else {
-      throw new IllegalArgumentException("Window may be set as selected only in task file which it belongs to");
-    }
-  }
-
   public List<AnswerPlaceholder> getAnswerPlaceholders() {
     return myAnswerPlaceholders;
   }