From 7b07d0db40f26d46259d1b04c5358e7afb9de28a Mon Sep 17 00:00:00 2001 From: Eugene Kudelevsky Date: Fri, 25 Mar 2011 18:03:04 +0300 Subject: [PATCH] support gutter; do not repeat colors if their number less than tree depth --- .../HtmlTagTreeHighlightingPass.java | 106 +++++++++++++++--- 1 file changed, 92 insertions(+), 14 deletions(-) diff --git a/xml/impl/src/com/intellij/codeInsight/daemon/impl/tagTreeHighlighting/HtmlTagTreeHighlightingPass.java b/xml/impl/src/com/intellij/codeInsight/daemon/impl/tagTreeHighlighting/HtmlTagTreeHighlightingPass.java index 2a1f5bbc0fdb..332a822b0070 100644 --- a/xml/impl/src/com/intellij/codeInsight/daemon/impl/tagTreeHighlighting/HtmlTagTreeHighlightingPass.java +++ b/xml/impl/src/com/intellij/codeInsight/daemon/impl/tagTreeHighlighting/HtmlTagTreeHighlightingPass.java @@ -30,8 +30,10 @@ import com.intellij.openapi.editor.colors.EditorColorsManager; import com.intellij.openapi.editor.colors.EditorColorsScheme; import com.intellij.openapi.editor.colors.TextAttributesKey; import com.intellij.openapi.editor.ex.EditorEx; -import com.intellij.openapi.editor.markup.TextAttributes; +import com.intellij.openapi.editor.ex.MarkupModelEx; +import com.intellij.openapi.editor.markup.*; import com.intellij.openapi.progress.ProgressIndicator; +import com.intellij.openapi.util.Key; import com.intellij.openapi.util.Pair; import com.intellij.openapi.util.TextRange; import com.intellij.psi.FileViewProvider; @@ -51,6 +53,8 @@ import java.util.List; * @author Eugene.Kudelevsky */ public class HtmlTagTreeHighlightingPass extends TextEditorHighlightingPass { + private static final Key> TAG_TREE_HIGHLIGHTERS_IN_EDITOR_KEY = Key.create("TAG_TREE_HIGHLIGHTERS_IN_EDITOR_KEY"); + private static final HighlightInfoType TYPE = new HighlightInfoType.HighlightInfoTypeImpl(HighlightSeverity.INFORMATION, TextAttributesKey .createTextAttributesKey("TAG_TREE_HIGHLIGHTING_KEY")); @@ -149,24 +153,28 @@ public class HtmlTagTreeHighlightingPass extends TextEditorHighlightingPass { } public List getHighlights() { + removeOldLineMarkers(); + final int count = myPairsToHighlight.size(); final List highlightInfos = new ArrayList(count * 2); + final MarkupModel markupModel = myEditor.getMarkupModel(); - final Color[] colors = getColors(); + final Color[] baseColors = getBaseColors(); + final Color[] colorsForEditor = toColorsForEditor(baseColors); + final Color[] colorsForLineMarkers = toColorsForLineMarkers(baseColors); - assert colors.length > 0; + final List newHighlighters = new ArrayList(); - int colorIndex = 0; + assert colorsForEditor.length > 0; + + for (int i = 0, n = myPairsToHighlight.size(); i < n && i < baseColors.length; i++) { + Pair pair = myPairsToHighlight.get(i); - for (Pair pair : myPairsToHighlight) { if (pair == null || (pair.first == null && pair.second == null)) { continue; } - if (colorIndex >= colors.length) { - colorIndex = 0; - } - Color color = colors[colorIndex]; + Color color = colorsForEditor[i]; if (pair.first != null) { highlightInfos.add(createHighlightInfo(color, pair.first)); @@ -176,30 +184,100 @@ public class HtmlTagTreeHighlightingPass extends TextEditorHighlightingPass { highlightInfos.add(createHighlightInfo(color, pair.second)); } - colorIndex++; + final int start = pair.first != null ? pair.first.getStartOffset() : pair.second.getStartOffset(); + final int end = pair.second != null ? pair.second.getEndOffset() : pair.first.getEndOffset(); + + final RangeHighlighter highlighter = createHighlighter(markupModel, new TextRange(start, end), colorsForLineMarkers[i]); + newHighlighters.add(highlighter); } + myEditor.putUserData(TAG_TREE_HIGHLIGHTERS_IN_EDITOR_KEY, newHighlighters); + return highlightInfos; } + private void removeOldLineMarkers() { + final List oldHighlighters = myEditor.getUserData(TAG_TREE_HIGHLIGHTERS_IN_EDITOR_KEY); + + if (oldHighlighters != null) { + final MarkupModelEx markupModel = (MarkupModelEx)myEditor.getMarkupModel(); + + for (RangeHighlighter highlighter : oldHighlighters) { + if (markupModel.containsHighlighter(highlighter)) { + highlighter.dispose(); + } + } + myEditor.putUserData(TAG_TREE_HIGHLIGHTERS_IN_EDITOR_KEY, null); + } + } + @NotNull private static HighlightInfo createHighlightInfo(Color color, @NotNull TextRange range) { return new HighlightInfo(new TextAttributes(null, color, null, null, Font.PLAIN), TYPE, range.getStartOffset(), range.getEndOffset(), null, null, HighlightSeverity.INFORMATION, false, null, false); } - private Color[] getColors() { + @NotNull + private static RangeHighlighter createHighlighter(final MarkupModel mm, final @NotNull TextRange range, final Color color) { + final RangeHighlighter highlighter = + mm.addRangeHighlighter(range.getStartOffset(), range.getEndOffset(), 0, null, HighlighterTargetArea.LINES_IN_RANGE); + + highlighter.setLineMarkerRenderer(new LineMarkerRenderer() { + public void paint(Editor editor, Graphics g, Rectangle r) { + int height = r.height + editor.getLineHeight(); + g.setColor(color); + g.fillRect(r.x, r.y, 2, height); + } + }); + return highlighter; + } + + + private static Color[] getBaseColors() { final ColorKey[] colorKeys = HtmlTagTreeHighlightingColors.COLOR_KEYS; final Color[] colors = new Color[colorKeys.length]; + + final EditorColorsScheme colorsScheme = EditorColorsManager.getInstance().getGlobalScheme(); + + for (int i = 0; i < colors.length; i++) { + colors[i] = colorsScheme.getColor(colorKeys[i]); + } + + return colors; + } + + private static Color[] toColorsForLineMarkers(Color[] baseColors) { + final Color[] colors = new Color[baseColors.length]; + final Color tagBackground = new Color(239, 239, 239); + final double transparency = 0.4; + final double factor = 0.8; + + for (int i = 0; i < colors.length; i++) { + final Color color = baseColors[i]; + + int r = (int)(color.getRed() * factor); + int g = (int)(color.getGreen() * factor); + int b = (int)(color.getBlue() * factor); + + r = (int)(tagBackground.getRed() * (1 - transparency) + r * transparency); + g = (int)(tagBackground.getGreen() * (1 - transparency) + g * transparency); + b = (int)(tagBackground.getBlue() * (1 - transparency) + b * transparency); + + colors[i] = new Color(r, g, b); + } + + return colors; + } + + private Color[] toColorsForEditor(Color[] baseColors) { + final Color[] colors = new Color[baseColors.length]; final Color tagBackground = myEditor instanceof EditorEx ? ((EditorEx)myEditor).getBackgroundColor() : null; // todo: make configurable final double transparency = 0.1; - final EditorColorsScheme colorsScheme = EditorColorsManager.getInstance().getGlobalScheme(); - for (int i = 0; i < colors.length; i++) { - Color color = colorsScheme.getColor(colorKeys[i]); + final Color color = baseColors[i]; if (tagBackground == null) { colors[i] = color;