CPP-778 Support for semantic per-variable highlighting (this, self and etc. are not params)

This commit is contained in:
Alexey Utkin
2016-06-06 20:29:37 +03:00
parent cb071c57a6
commit 7469f78ea6
3 changed files with 37 additions and 21 deletions
@@ -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<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"))
);
}
}
@@ -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;
}
@@ -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)