CPP-778 Support for semantic per-variable highlighting (this, self and etc. are not...
authorAlexey Utkin <alexey.utkin@jetbrains.com>
Mon, 6 Jun 2016 17:16:37 +0000 (20:16 +0300)
committerAlexey Utkin <alexey.utkin@jetbrains.com>
Mon, 6 Jun 2016 17:29:37 +0000 (20:29 +0300)
platform/analysis-impl/src/com/intellij/codeHighlighting/RainbowHighlighter.java
platform/analysis-impl/src/com/intellij/codeInsight/daemon/impl/HighlightInfo.java
platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/RainbowIdentifierHighlighterPass.java

index aec8793a65d35acb46128ed8d3f4bbbc48fb8238..16eb577f0924fd864cf3909dd32f2a222807d572 100644 (file)
 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<Language> BY_PASS_LANGUAGES = new HashSet<Language>();
 
   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"))
+    );
   }
 }
index 248d9d4f716a670e30a85e754c5b7c796784a738..6e1dcccbb4f29e002e3f640aa3b839bb54838216 100644 (file)
@@ -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;
   }
index 91debbcdec35393053fd94a9b47229ce7570253d..d4f5f6b0a968fdecfe3b90a6e82712863d941814 100644 (file)
@@ -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<HighlightInfo> 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)