From 7469f78ea6cdc9145f883bc61a4dacb05f4eafed Mon Sep 17 00:00:00 2001 From: Alexey Utkin Date: Mon, 6 Jun 2016 20:16:37 +0300 Subject: [PATCH] CPP-778 Support for semantic per-variable highlighting (this, self and etc. are not params) --- .../codeHighlighting/RainbowHighlighter.java | 29 ++++++++++++------- .../daemon/impl/HighlightInfo.java | 9 +++--- .../RainbowIdentifierHighlighterPass.java | 20 ++++++++----- 3 files changed, 37 insertions(+), 21 deletions(-) diff --git a/platform/analysis-impl/src/com/intellij/codeHighlighting/RainbowHighlighter.java b/platform/analysis-impl/src/com/intellij/codeHighlighting/RainbowHighlighter.java index aec8793a65d3..16eb577f0924 100644 --- a/platform/analysis-impl/src/com/intellij/codeHighlighting/RainbowHighlighter.java +++ b/platform/analysis-impl/src/com/intellij/codeHighlighting/RainbowHighlighter.java @@ -16,24 +16,26 @@ package com.intellij.codeHighlighting; import com.intellij.codeInsight.daemon.impl.HighlightInfoType; +import com.intellij.lang.Language; import com.intellij.lang.annotation.HighlightSeverity; import com.intellij.openapi.editor.DefaultLanguageHighlighterColors; import com.intellij.openapi.editor.colors.TextAttributesScheme; import com.intellij.openapi.editor.markup.TextAttributes; import com.intellij.openapi.util.registry.Registry; import com.intellij.openapi.util.text.StringHash; +import com.intellij.util.containers.hash.HashSet; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import javax.swing.*; import java.awt.*; public class RainbowHighlighter { private final float[] myFloats; - private TextAttributes myColorsSchemeAttributes; + private static final HashSet BY_PASS_LANGUAGES = new HashSet(); public RainbowHighlighter(@NotNull TextAttributesScheme colorsScheme) { - myColorsSchemeAttributes = colorsScheme.getAttributes(DefaultLanguageHighlighterColors.CONSTANT); - float[] components = myColorsSchemeAttributes.getForegroundColor().getRGBColorComponents(null); + float[] components = colorsScheme.getAttributes(DefaultLanguageHighlighterColors.CONSTANT).getForegroundColor().getRGBColorComponents(null); myFloats = Color.RGBtoHSB((int)(255 * components[0]), (int)(255 * components[0]), (int)(255 * components[0]), null); } @@ -44,20 +46,27 @@ public class RainbowHighlighter { return Registry.is("editor.rainbow.identifiers", false); } + public static void registerByPassLanguage(@NotNull Language language) { + BY_PASS_LANGUAGES.add(language); + } + + public static boolean isByPassLanguage(@Nullable Language language) { + return BY_PASS_LANGUAGES.contains(language); + } + @NotNull - public TextAttributes getAttributes(@NotNull String name) { + public TextAttributes getAttributes(@NotNull String name, @NotNull TextAttributes origin) { int hash = StringHash.murmur(name, 0); final float colors = 36.0f; final float v = Math.round(Math.abs(colors * hash) / Integer.MAX_VALUE) / colors; //System.out.println("name = " + name + " \tv=" + v); final Color color = Color.getHSBColor(v, 0.7f, myFloats[2] + .3f); - final TextAttributes attributes = TextAttributes.fromFlyweight(myColorsSchemeAttributes - .getFlyweight() - .withForeground(color) + return TextAttributes.fromFlyweight(origin + .getFlyweight() + .withForeground(color) //fixme: uta: foreground color is not activated for local variables without background color reset - .withBackground(UIManager.getColor("EditorPane.background"))); - - return attributes; + .withBackground(UIManager.getColor("EditorPane.background")) + ); } } diff --git a/platform/analysis-impl/src/com/intellij/codeInsight/daemon/impl/HighlightInfo.java b/platform/analysis-impl/src/com/intellij/codeInsight/daemon/impl/HighlightInfo.java index 248d9d4f716a..6e1dcccbb4f2 100644 --- a/platform/analysis-impl/src/com/intellij/codeInsight/daemon/impl/HighlightInfo.java +++ b/platform/analysis-impl/src/com/intellij/codeInsight/daemon/impl/HighlightInfo.java @@ -180,12 +180,13 @@ public class HighlightInfo implements Segment { } TextAttributes attributes = getAttributesByType(element, type, colorsScheme); - if (RainbowHighlighter.isRainbowEnabled() && - isLikeVariable(type.getAttributesKey()) && element != null) { + if (element != null && + RainbowHighlighter.isRainbowEnabled() && + !RainbowHighlighter.isByPassLanguage(element.getLanguage()) && + isLikeVariable(type.getAttributesKey())) { String text = element.getContainingFile().getText(); String name = text.substring(startOffset, endOffset); - TextAttributes rainAttributes = new RainbowHighlighter(colorsScheme).getAttributes(name); - attributes = TextAttributes.merge(attributes, rainAttributes); + attributes = new RainbowHighlighter(colorsScheme).getAttributes(name, attributes); } return attributes; } diff --git a/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/RainbowIdentifierHighlighterPass.java b/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/RainbowIdentifierHighlighterPass.java index 91debbcdec35..d4f5f6b0a968 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/RainbowIdentifierHighlighterPass.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/RainbowIdentifierHighlighterPass.java @@ -18,12 +18,14 @@ package com.intellij.codeInsight.daemon.impl; import com.intellij.codeHighlighting.RainbowHighlighter; import com.intellij.codeHighlighting.TextEditorHighlightingPass; +import com.intellij.openapi.editor.DefaultLanguageHighlighterColors; import com.intellij.openapi.editor.Editor; +import com.intellij.openapi.editor.colors.EditorColorsScheme; +import com.intellij.openapi.editor.colors.TextAttributesKey; import com.intellij.openapi.editor.markup.TextAttributes; import com.intellij.openapi.progress.ProgressIndicator; import com.intellij.openapi.util.text.StringUtil; import com.intellij.psi.*; -import com.intellij.ui.JBColor; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -34,11 +36,13 @@ public class RainbowIdentifierHighlighterPass extends TextEditorHighlightingPass protected final PsiFile myFile; protected final RainbowHighlighter myRainbowHighlighter; protected List toHighlight; + protected final EditorColorsScheme myEditorColorsScheme; protected RainbowIdentifierHighlighterPass(@NotNull PsiFile file, @NotNull Editor editor) { super(file.getProject(), editor.getDocument(), false); myFile = file; - myRainbowHighlighter = new RainbowHighlighter(editor.getColorsScheme()); + myEditorColorsScheme = editor.getColorsScheme(); + myRainbowHighlighter = new RainbowHighlighter(myEditorColorsScheme); } @Override @@ -50,11 +54,11 @@ public class RainbowIdentifierHighlighterPass extends TextEditorHighlightingPass public void visitElement(PsiElement e) { final HighlightInfo attrs; if (e instanceof PsiReference) { - attrs = getInfo(e.getText(), e); + attrs = getInfo(e.getText(), e, null); } else if (e instanceof PsiNameIdentifierOwner) { PsiNameIdentifierOwner identifierOwner = (PsiNameIdentifierOwner)e; - attrs = getInfo(identifierOwner.getName(), identifierOwner.getNameIdentifier()); + attrs = getInfo(identifierOwner.getName(), identifierOwner.getNameIdentifier(), null); } else { attrs = null; @@ -74,9 +78,11 @@ public class RainbowIdentifierHighlighterPass extends TextEditorHighlightingPass UpdateHighlightersUtil.setHighlightersToEditor(myProject, myDocument, 0, myFile.getTextLength(), toHighlight, getColorsScheme(), getId()); } - protected HighlightInfo getInfo(@Nullable String colorKey, @Nullable PsiElement id) { - if (id == null || colorKey == null || StringUtil.isEmpty(colorKey)) return null; - final TextAttributes attributes = myRainbowHighlighter.getAttributes(colorKey); + protected HighlightInfo getInfo(@Nullable String nameKey, @Nullable PsiElement id, @Nullable TextAttributesKey colorKey) { + if (id == null || nameKey == null || StringUtil.isEmpty(nameKey)) return null; + if (colorKey == null) colorKey = DefaultLanguageHighlighterColors.LOCAL_VARIABLE; + final TextAttributes attributes = myRainbowHighlighter.getAttributes(nameKey, + myEditorColorsScheme.getAttributes(colorKey)); return HighlightInfo .newHighlightInfo(RainbowHighlighter.RAINBOW_ELEMENT) .textAttributes(attributes)