1 package com.jetbrains.edu.learning.core;
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;
19 import java.util.List;
21 public class EduAnswerPlaceholderPainter {
23 //it should be the lowest highlighting layer, otherwise selection and other effects are not visible
24 public static final int PLACEHOLDERS_LAYER = 0;
26 private EduAnswerPlaceholderPainter() {
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);
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) {
49 final int length = placeholder.getRealLength();
50 final int endOffset = startOffset + length;
52 highlighter = editor.getMarkupModel().addRangeHighlighter(startOffset, endOffset, placeholdersLayer,
53 textAttributes, HighlighterTargetArea.EXACT_RANGE);
54 highlighter.setGreedyToLeft(true);
55 highlighter.setGreedyToRight(true);
58 public static void drawAnswerPlaceholderFromPrevStep(@NotNull Editor editor, @NotNull AnswerPlaceholder placeholder) {
59 if (placeholder.getUseLength()) {
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());
68 drawAnswerPlaceholder(editor, placeholder, textAttributes, HighlighterLayer.LAST);
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);
78 public static void createGuardedBlocks(@NotNull final Editor editor, TaskFile taskFile) {
79 for (AnswerPlaceholder answerPlaceholder : taskFile.getAnswerPlaceholders()) {
80 createGuardedBlocks(editor, answerPlaceholder);
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;
93 createGuardedBlock(editor, blocks, start - 1, start);
95 if (end != document.getTextLength()) {
96 createGuardedBlock(editor, blocks, end, end + 1);