From 6ed0347b984f7902aa9b3536ece8d5e52cf34353 Mon Sep 17 00:00:00 2001 From: "Liana.Bakradze" Date: Tue, 4 Oct 2016 17:30:45 +0300 Subject: [PATCH] do not allow to edit text outside of placeholders in tasks with subtasks --- .../student/resources/META-INF/plugin.xml | 1 + .../learning/StudyTypeHandlerDelegate.java | 44 +++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 python/educational-core/student/src/com/jetbrains/edu/learning/StudyTypeHandlerDelegate.java diff --git a/python/educational-core/student/resources/META-INF/plugin.xml b/python/educational-core/student/resources/META-INF/plugin.xml index 6f842f044c9f..3058bd39f937 100644 --- a/python/educational-core/student/resources/META-INF/plugin.xml +++ b/python/educational-core/student/resources/META-INF/plugin.xml @@ -107,6 +107,7 @@ + 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 index 000000000000..27f7593b3ba7 --- /dev/null +++ b/python/educational-core/student/src/com/jetbrains/edu/learning/StudyTypeHandlerDelegate.java @@ -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; + } +} -- 2.23.3