EDU-683 Do not show placeholders from previous steps in student mode
[idea/community.git] / python / educational-core / student / src / com / jetbrains / edu / learning / core / EduAnswerPlaceholderPainter.java
1 package com.jetbrains.edu.learning.core;
2
3 import com.intellij.openapi.editor.Document;
4 import com.intellij.openapi.editor.Editor;
5 import com.intellij.openapi.editor.RangeMarker;
6 import com.intellij.openapi.editor.colors.EditorColors;
7 import com.intellij.openapi.editor.colors.EditorColorsManager;
8 import com.intellij.openapi.editor.colors.EditorColorsScheme;
9 import com.intellij.openapi.editor.impl.DocumentImpl;
10 import com.intellij.openapi.editor.markup.*;
11 import com.intellij.openapi.project.Project;
12 import com.intellij.ui.JBColor;
13 import com.intellij.ui.SimpleTextAttributes;
14 import com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder;
15 import com.jetbrains.edu.learning.courseFormat.TaskFile;
16 import org.jetbrains.annotations.NotNull;
17
18 import java.awt.*;
19 import java.util.List;
20
21 public class EduAnswerPlaceholderPainter {
22
23   //it should be the lowest highlighting layer, otherwise selection and other effects are not visible
24   public static final int PLACEHOLDERS_LAYER = 0;
25
26   private EduAnswerPlaceholderPainter() {
27
28   }
29
30   public static void drawAnswerPlaceholder(@NotNull final Editor editor, @NotNull final AnswerPlaceholder placeholder,
31                                            @NotNull final JBColor color) {
32     EditorColorsScheme scheme = EditorColorsManager.getInstance().getGlobalScheme();
33     final TextAttributes textAttributes = new TextAttributes(scheme.getDefaultForeground(), scheme.getDefaultBackground(), null,
34                                                                     EffectType.BOXED, Font.PLAIN);
35     textAttributes.setEffectColor(color);
36     drawAnswerPlaceholder(editor, placeholder, textAttributes, PLACEHOLDERS_LAYER);
37   }
38
39   public static void drawAnswerPlaceholder(@NotNull Editor editor,
40                                            @NotNull AnswerPlaceholder placeholder,
41                                            TextAttributes textAttributes,
42                                            int placeholdersLayer) {
43     final Project project = editor.getProject();
44     assert project != null;
45     final int startOffset = placeholder.getOffset();
46     if (startOffset == - 1) {
47       return;
48     }
49     final int length = placeholder.getRealLength();
50     final int endOffset = startOffset + length;
51     RangeHighlighter
52       highlighter = editor.getMarkupModel().addRangeHighlighter(startOffset, endOffset, placeholdersLayer,
53                                                                 textAttributes, HighlighterTargetArea.EXACT_RANGE);
54     highlighter.setGreedyToLeft(true);
55     highlighter.setGreedyToRight(true);
56   }
57
58   public static void drawAnswerPlaceholderFromPrevStep(@NotNull Editor editor, @NotNull AnswerPlaceholder placeholder) {
59     if (placeholder.getUseLength()) {
60       return;
61     }
62     EditorColorsScheme scheme = EditorColorsManager.getInstance().getGlobalScheme();
63     Color color = scheme.getColor(EditorColors.TEARLINE_COLOR);
64     SimpleTextAttributes attributes = SimpleTextAttributes.GRAY_ATTRIBUTES;
65     final TextAttributes textAttributes = new TextAttributes(attributes.getFgColor(), color, null,
66                                                              null, attributes.getFontStyle());
67
68     drawAnswerPlaceholder(editor, placeholder, textAttributes, HighlighterLayer.LAST);
69   }
70
71   public static void createGuardedBlock(Editor editor, List<RangeMarker> blocks, int start, int end) {
72     RangeHighlighter rh = editor.getMarkupModel()
73       .addRangeHighlighter(start, end, PLACEHOLDERS_LAYER, null, HighlighterTargetArea.EXACT_RANGE);
74     blocks.add(rh);
75   }
76
77
78   public static void createGuardedBlocks(@NotNull final Editor editor, TaskFile taskFile) {
79     for (AnswerPlaceholder answerPlaceholder : taskFile.getAnswerPlaceholders()) {
80       createGuardedBlocks(editor, answerPlaceholder);
81     }
82   }
83
84   public static void createGuardedBlocks(@NotNull final Editor editor, AnswerPlaceholder placeholder) {
85     Document document = editor.getDocument();
86     if (document instanceof DocumentImpl) {
87       DocumentImpl documentImpl = (DocumentImpl)document;
88       List<RangeMarker> blocks = documentImpl.getGuardedBlocks();
89       int start = placeholder.getOffset();
90       final int length = placeholder.getRealLength();
91       int end = start + length;
92       if (start != 0) {
93         createGuardedBlock(editor, blocks, start - 1, start);
94       }
95       if (end != document.getTextLength()) {
96         createGuardedBlock(editor, blocks, end, end + 1);
97       }
98     }
99   }
100
101 }