Added highlighting for local variables.
This commit is contained in:
@@ -129,7 +129,9 @@ public class JetColorSettingsPage implements ColorSettingsPage {
|
||||
new AttributesDescriptor("Trait", JetHighlightingColors.TRAIT),
|
||||
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 backing field access", JetHighlightingColors.INSTANCE_BACKING_FIELD_ACCESS),
|
||||
|
||||
@@ -161,10 +161,19 @@ public class JetHighlightingColors {
|
||||
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(
|
||||
"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(
|
||||
|
||||
@@ -16,14 +16,17 @@
|
||||
|
||||
package org.jetbrains.jet.plugin.highlighter;
|
||||
|
||||
import com.intellij.lang.ASTNode;
|
||||
import com.intellij.lang.annotation.AnnotationHolder;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
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.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.types.JetType;
|
||||
|
||||
@@ -45,18 +48,21 @@ class VariablesHighlightingVisitor extends AfterAnalysisHighlightingVisitor {
|
||||
}
|
||||
}
|
||||
|
||||
markVariableAsWrappedIfNeeded(expression.getNode(), target);
|
||||
highlightVariable(expression, target);
|
||||
super.visitSimpleNameExpression(expression);
|
||||
}
|
||||
|
||||
private void markVariableAsWrappedIfNeeded(@NotNull ASTNode node, DeclarationDescriptor target) {
|
||||
if (target instanceof VariableDescriptor) {
|
||||
VariableDescriptor variableDescriptor = (VariableDescriptor) target;
|
||||
private void highlightVariable(@NotNull PsiElement elementToHighlight, DeclarationDescriptor descriptor) {
|
||||
if (descriptor instanceof LocalVariableDescriptor) {
|
||||
LocalVariableDescriptor variableDescriptor = (LocalVariableDescriptor) descriptor;
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
PsiElement nameIdentifier = property.getNameIdentifier();
|
||||
if (nameIdentifier != null) {
|
||||
markVariableAsWrappedIfNeeded(nameIdentifier.getNode(), declarationDescriptor);
|
||||
highlightVariable(nameIdentifier, declarationDescriptor);
|
||||
}
|
||||
super.visitProperty(property);
|
||||
}
|
||||
@@ -77,7 +83,7 @@ class VariablesHighlightingVisitor extends AfterAnalysisHighlightingVisitor {
|
||||
holder.createInfoAnnotation(expression, "Automatically cast to " + autoCast).setTextAttributes(
|
||||
JetHighlightingColors.AUTO_CASTED_VALUE);
|
||||
}
|
||||
expression.acceptChildren(this);
|
||||
super.visitExpression(expression);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user