Added highlighting for local variables.

This commit is contained in:
Evgeny Gerashchenko
2012-03-30 15:24:48 +04:00
parent f8618fb9b1
commit 696e6828ea
3 changed files with 30 additions and 13 deletions
@@ -129,7 +129,9 @@ public class JetColorSettingsPage implements ColorSettingsPage {
new AttributesDescriptor("Trait", JetHighlightingColors.TRAIT), new AttributesDescriptor("Trait", JetHighlightingColors.TRAIT),
new AttributesDescriptor("Annotation", JetHighlightingColors.ANNOTATION), new AttributesDescriptor("Annotation", JetHighlightingColors.ANNOTATION),
new AttributesDescriptor("Wrapped into ref", JetHighlightingColors.WRAPPED_INTO_REF), new AttributesDescriptor("Closure or anonymous object bound variable", JetHighlightingColors.WRAPPED_INTO_REF),
new AttributesDescriptor("Read-only local variable (val)", JetHighlightingColors.LOCAL_VAL),
new AttributesDescriptor("Mutable local variable (var)", JetHighlightingColors.LOCAL_VAR),
new AttributesDescriptor("Instance property with backing field", JetHighlightingColors.INSTANCE_PROPERTY_WITH_BACKING_FIELD), new AttributesDescriptor("Instance property with backing field", JetHighlightingColors.INSTANCE_PROPERTY_WITH_BACKING_FIELD),
new AttributesDescriptor("Instance property backing field access", JetHighlightingColors.INSTANCE_BACKING_FIELD_ACCESS), new AttributesDescriptor("Instance property backing field access", JetHighlightingColors.INSTANCE_BACKING_FIELD_ACCESS),
@@ -161,10 +161,19 @@ public class JetHighlightingColors {
CodeInsightColors.ANNOTATION_NAME_ATTRIBUTES.getDefaultAttributes() CodeInsightColors.ANNOTATION_NAME_ATTRIBUTES.getDefaultAttributes()
); );
// TODO review: is it needed? public static final TextAttributesKey LOCAL_VAL = TextAttributesKey.createTextAttributesKey(
"KOTLIN_LOCAL_VAL",
CodeInsightColors.LOCAL_VARIABLE_ATTRIBUTES.getDefaultAttributes()
);
public static final TextAttributesKey LOCAL_VAR = TextAttributesKey.createTextAttributesKey(
"KOTLIN_LOCAL_VAR",
CodeInsightColors.REASSIGNED_LOCAL_VARIABLE_ATTRIBUTES.getDefaultAttributes()
);
public static final TextAttributesKey WRAPPED_INTO_REF = TextAttributesKey.createTextAttributesKey( public static final TextAttributesKey WRAPPED_INTO_REF = TextAttributesKey.createTextAttributesKey(
"KOTLIN_WRAPPED_INTO_REF", "KOTLIN_WRAPPED_INTO_REF",
new TextAttributes(null, null, Color.BLACK, EffectType.LINE_UNDERSCORE, Font.PLAIN) CodeInsightColors.IMPLICIT_ANONYMOUS_CLASS_PARAMETER_ATTRIBUTES.getDefaultAttributes()
); );
public static final TextAttributesKey INSTANCE_PROPERTY_WITH_BACKING_FIELD = TextAttributesKey.createTextAttributesKey( public static final TextAttributesKey INSTANCE_PROPERTY_WITH_BACKING_FIELD = TextAttributesKey.createTextAttributesKey(
@@ -16,14 +16,17 @@
package org.jetbrains.jet.plugin.highlighter; package org.jetbrains.jet.plugin.highlighter;
import com.intellij.lang.ASTNode;
import com.intellij.lang.annotation.AnnotationHolder; import com.intellij.lang.annotation.AnnotationHolder;
import com.intellij.psi.PsiElement; import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor; import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
import org.jetbrains.jet.lang.descriptors.LocalVariableDescriptor;
import org.jetbrains.jet.lang.descriptors.ValueParameterDescriptor; import org.jetbrains.jet.lang.descriptors.ValueParameterDescriptor;
import org.jetbrains.jet.lang.descriptors.VariableDescriptor; import org.jetbrains.jet.lang.descriptors.VariableDescriptor;
import org.jetbrains.jet.lang.psi.*; import org.jetbrains.jet.lang.psi.JetElement;
import org.jetbrains.jet.lang.psi.JetExpression;
import org.jetbrains.jet.lang.psi.JetProperty;
import org.jetbrains.jet.lang.psi.JetSimpleNameExpression;
import org.jetbrains.jet.lang.resolve.BindingContext; import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.types.JetType; import org.jetbrains.jet.lang.types.JetType;
@@ -45,18 +48,21 @@ class VariablesHighlightingVisitor extends AfterAnalysisHighlightingVisitor {
} }
} }
markVariableAsWrappedIfNeeded(expression.getNode(), target); highlightVariable(expression, target);
super.visitSimpleNameExpression(expression); super.visitSimpleNameExpression(expression);
} }
private void markVariableAsWrappedIfNeeded(@NotNull ASTNode node, DeclarationDescriptor target) { private void highlightVariable(@NotNull PsiElement elementToHighlight, DeclarationDescriptor descriptor) {
if (target instanceof VariableDescriptor) { if (descriptor instanceof LocalVariableDescriptor) {
VariableDescriptor variableDescriptor = (VariableDescriptor) target; LocalVariableDescriptor variableDescriptor = (LocalVariableDescriptor) descriptor;
if (Boolean.TRUE.equals(bindingContext.get(MUST_BE_WRAPPED_IN_A_REF, variableDescriptor))) { if (Boolean.TRUE.equals(bindingContext.get(MUST_BE_WRAPPED_IN_A_REF, variableDescriptor))) {
holder.createInfoAnnotation(node, "Wrapped into a ref-object to be modifier when captured in a closure").setTextAttributes( holder.createInfoAnnotation(elementToHighlight, "Wrapped into a ref-object to be modifier when captured in a closure").setTextAttributes(
JetHighlightingColors.WRAPPED_INTO_REF); JetHighlightingColors.WRAPPED_INTO_REF);
} }
holder.createInfoAnnotation(elementToHighlight, null).setTextAttributes(
variableDescriptor.isVar() ?
JetHighlightingColors.LOCAL_VAR :
JetHighlightingColors.LOCAL_VAL);
} }
} }
@@ -65,7 +71,7 @@ class VariablesHighlightingVisitor extends AfterAnalysisHighlightingVisitor {
DeclarationDescriptor declarationDescriptor = bindingContext.get(DECLARATION_TO_DESCRIPTOR, property); DeclarationDescriptor declarationDescriptor = bindingContext.get(DECLARATION_TO_DESCRIPTOR, property);
PsiElement nameIdentifier = property.getNameIdentifier(); PsiElement nameIdentifier = property.getNameIdentifier();
if (nameIdentifier != null) { if (nameIdentifier != null) {
markVariableAsWrappedIfNeeded(nameIdentifier.getNode(), declarationDescriptor); highlightVariable(nameIdentifier, declarationDescriptor);
} }
super.visitProperty(property); super.visitProperty(property);
} }
@@ -77,7 +83,7 @@ class VariablesHighlightingVisitor extends AfterAnalysisHighlightingVisitor {
holder.createInfoAnnotation(expression, "Automatically cast to " + autoCast).setTextAttributes( holder.createInfoAnnotation(expression, "Automatically cast to " + autoCast).setTextAttributes(
JetHighlightingColors.AUTO_CASTED_VALUE); JetHighlightingColors.AUTO_CASTED_VALUE);
} }
expression.acceptChildren(this); super.visitExpression(expression);
} }
@Override @Override