do not allow to edit text outside of placeholders in tasks with subtasks
authorLiana.Bakradze <liana.bakradze@jetbrains.com>
Tue, 4 Oct 2016 14:30:45 +0000 (17:30 +0300)
committerliana.bakradze <liana.bakradze@jetbrains.com>
Thu, 17 Nov 2016 14:08:27 +0000 (17:08 +0300)
python/educational-core/student/resources/META-INF/plugin.xml
python/educational-core/student/src/com/jetbrains/edu/learning/StudyTypeHandlerDelegate.java [new file with mode: 0644]

index 6f842f044c9fc546f131af4a7846063338383c9a..3058bd39f93790e498f055488848290a27e2862e 100644 (file)
     <applicationService serviceImplementation="com.jetbrains.edu.learning.statistics.EduUsagesCollector"/>
     <applicationService serviceImplementation="com.jetbrains.edu.learning.stepic.StepicUpdateSettings"/>
     <editorTabTitleProvider implementation="com.jetbrains.edu.learning.StudyTabTitleProvider"/>
+    <typedHandler implementation="com.jetbrains.edu.learning.StudyTypeHandlerDelegate" order="first, before completionAutoPopup"/>
   </extensions>
 
   <extensions defaultExtensionNs="Edu">
diff --git a/python/educational-core/student/src/com/jetbrains/edu/learning/StudyTypeHandlerDelegate.java b/python/educational-core/student/src/com/jetbrains/edu/learning/StudyTypeHandlerDelegate.java
new file mode 100644 (file)
index 0000000..27f7593
--- /dev/null
@@ -0,0 +1,44 @@
+package com.jetbrains.edu.learning;
+
+import com.intellij.codeInsight.editorActions.TypedHandlerDelegate;
+import com.intellij.codeInsight.hint.HintManager;
+import com.intellij.openapi.editor.Editor;
+import com.intellij.openapi.fileTypes.FileType;
+import com.intellij.openapi.project.Project;
+import com.intellij.psi.PsiFile;
+import com.jetbrains.edu.learning.courseFormat.TaskFile;
+import org.jetbrains.annotations.NotNull;
+
+public class StudyTypeHandlerDelegate extends TypedHandlerDelegate {
+
+  @Override
+  public Result checkAutoPopup(char charTyped, Project project, Editor editor, PsiFile file) {
+    return handleTyping(project, editor, file);
+  }
+
+  @Override
+  public Result beforeCharTyped(char c,
+                                Project project,
+                                Editor editor,
+                                PsiFile file,
+                                FileType fileType) {
+    return handleTyping(project, editor, file);
+  }
+
+  @NotNull
+  private static Result handleTyping(Project project, Editor editor, PsiFile file) {
+    if (!StudyUtils.isStudyProject(project)) {
+      return Result.CONTINUE;
+    }
+    TaskFile taskFile = StudyUtils.getTaskFile(project, file.getVirtualFile());
+    if (taskFile == null || !taskFile.getTask().hasSubtasks()) {
+      return Result.CONTINUE;
+    }
+    int offset = editor.getCaretModel().getOffset();
+    boolean insidePlaceholder = taskFile.getAnswerPlaceholder(offset) != null;
+    if (!insidePlaceholder) {
+      HintManager.getInstance().showInformationHint(editor, "Text outside of placeholders is not editable in this task");
+    }
+    return insidePlaceholder ? Result.CONTINUE : Result.STOP;
+  }
+}