do not allow to edit text outside of placeholders in tasks with subtasks
[idea/community.git] / python / educational-core / student / src / com / jetbrains / edu / learning / StudyTypeHandlerDelegate.java
1 package com.jetbrains.edu.learning;
2
3 import com.intellij.codeInsight.editorActions.TypedHandlerDelegate;
4 import com.intellij.codeInsight.hint.HintManager;
5 import com.intellij.openapi.editor.Editor;
6 import com.intellij.openapi.fileTypes.FileType;
7 import com.intellij.openapi.project.Project;
8 import com.intellij.psi.PsiFile;
9 import com.jetbrains.edu.learning.courseFormat.TaskFile;
10 import org.jetbrains.annotations.NotNull;
11
12 public class StudyTypeHandlerDelegate extends TypedHandlerDelegate {
13
14   @Override
15   public Result checkAutoPopup(char charTyped, Project project, Editor editor, PsiFile file) {
16     return handleTyping(project, editor, file);
17   }
18
19   @Override
20   public Result beforeCharTyped(char c,
21                                 Project project,
22                                 Editor editor,
23                                 PsiFile file,
24                                 FileType fileType) {
25     return handleTyping(project, editor, file);
26   }
27
28   @NotNull
29   private static Result handleTyping(Project project, Editor editor, PsiFile file) {
30     if (!StudyUtils.isStudyProject(project)) {
31       return Result.CONTINUE;
32     }
33     TaskFile taskFile = StudyUtils.getTaskFile(project, file.getVirtualFile());
34     if (taskFile == null || !taskFile.getTask().hasSubtasks()) {
35       return Result.CONTINUE;
36     }
37     int offset = editor.getCaretModel().getOffset();
38     boolean insidePlaceholder = taskFile.getAnswerPlaceholder(offset) != null;
39     if (!insidePlaceholder) {
40       HintManager.getInstance().showInformationHint(editor, "Text outside of placeholders is not editable in this task");
41     }
42     return insidePlaceholder ? Result.CONTINUE : Result.STOP;
43   }
44 }