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.EditorColorsManager;
7 import com.intellij.openapi.editor.colors.EditorColorsScheme;
8 import com.intellij.openapi.editor.impl.DocumentImpl;
9 import com.intellij.openapi.editor.markup.*;
10 import com.intellij.openapi.project.Project;
11 import com.intellij.ui.JBColor;
12 import com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder;
13 import com.jetbrains.edu.learning.courseFormat.TaskFile;
14 import org.jetbrains.annotations.NotNull;
17 import java.util.List;
19 public class EduAnswerPlaceholderPainter {
21 //it should be the lowest highlighting layer, otherwise selection and other effects are not visible
22 public static final int PLACEHOLDERS_LAYER = 0;
24 private EduAnswerPlaceholderPainter() {
28 public static void drawAnswerPlaceholder(@NotNull final Editor editor, @NotNull final AnswerPlaceholder placeholder,
29 @NotNull final JBColor color) {
30 final Document document = editor.getDocument();
31 if (!placeholder.isValid(document)) {
34 EditorColorsScheme scheme = EditorColorsManager.getInstance().getGlobalScheme();
35 final TextAttributes textAttributes = new TextAttributes(scheme.getDefaultForeground(), scheme.getDefaultBackground(), null,
36 EffectType.BOXED, Font.PLAIN);
37 final Project project = editor.getProject();
38 assert project != null;
39 final int startOffset = placeholder.getOffset();
40 if (startOffset == - 1) {
43 final int length = placeholder.getRealLength();
44 final int endOffset = startOffset + length;
45 textAttributes.setEffectColor(color);
47 highlighter = editor.getMarkupModel().addRangeHighlighter(startOffset, endOffset, PLACEHOLDERS_LAYER,
48 textAttributes, HighlighterTargetArea.EXACT_RANGE);
49 highlighter.setGreedyToLeft(true);
50 highlighter.setGreedyToRight(true);
53 public static void createGuardedBlock(Editor editor, List<RangeMarker> blocks, int start, int end) {
54 RangeHighlighter rh = editor.getMarkupModel()
55 .addRangeHighlighter(start, end, PLACEHOLDERS_LAYER, null, HighlighterTargetArea.EXACT_RANGE);
60 public static void createGuardedBlocks(@NotNull final Editor editor, TaskFile taskFile) {
61 for (AnswerPlaceholder answerPlaceholder : taskFile.getAnswerPlaceholders()) {
62 createGuardedBlocks(editor, answerPlaceholder);
66 public static void createGuardedBlocks(@NotNull final Editor editor, AnswerPlaceholder placeholder) {
67 Document document = editor.getDocument();
68 if (document instanceof DocumentImpl) {
69 DocumentImpl documentImpl = (DocumentImpl)document;
70 List<RangeMarker> blocks = documentImpl.getGuardedBlocks();
71 if (!placeholder.isValid(document)) return;
72 int start = placeholder.getOffset();
73 final int length = placeholder.getRealLength();
74 int end = start + length;
76 createGuardedBlock(editor, blocks, start - 1, start);
78 if (end != document.getTextLength()) {
79 createGuardedBlock(editor, blocks, end, end + 1);