VariablesHighlightingVisitor.java 4.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * Copyright 2010-2012 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.jetbrains.jet.plugin.highlighter;

import com.intellij.lang.annotation.AnnotationHolder;
import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;
22
import org.jetbrains.jet.lang.descriptors.*;
23
import org.jetbrains.jet.lang.psi.*;
24 25
import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.types.JetType;
26
import org.jetbrains.jet.resolve.DescriptorRenderer;
27 28 29 30 31 32 33 34 35 36 37

import static org.jetbrains.jet.lang.resolve.BindingContext.*;

class VariablesHighlightingVisitor extends AfterAnalysisHighlightingVisitor {
    VariablesHighlightingVisitor(AnnotationHolder holder, BindingContext bindingContext) {
        super(holder, bindingContext);
    }

    @Override
    public void visitSimpleNameExpression(@NotNull JetSimpleNameExpression expression) {
        DeclarationDescriptor target = bindingContext.get(REFERENCE_TARGET, expression);
38 39 40
        if (target == null) {
            return;
        }
41 42 43 44 45 46 47 48
        if (target instanceof ValueParameterDescriptor) {
            ValueParameterDescriptor parameterDescriptor = (ValueParameterDescriptor) target;
            if (Boolean.TRUE.equals(bindingContext.get(AUTO_CREATED_IT, parameterDescriptor))) {
                holder.createInfoAnnotation(expression, "Automatically declared based on the expected type").setTextAttributes(
                    JetHighlightingColors.FUNCTION_LITERAL_DEFAULT_PARAMETER);
            }
        }

49
        highlightVariable(expression, target);
50 51 52 53 54
        super.visitSimpleNameExpression(expression);
    }

    @Override
    public void visitProperty(@NotNull JetProperty property) {
55
        visitVariableDeclaration(property);
56 57 58
        super.visitProperty(property);
    }

59 60 61 62 63 64
    @Override
    public void visitParameter(JetParameter parameter) {
        visitVariableDeclaration(parameter);
        super.visitParameter(parameter);
    }

65 66 67 68
    @Override
    public void visitExpression(@NotNull JetExpression expression) {
        JetType autoCast = bindingContext.get(AUTOCAST, expression);
        if (autoCast != null) {
69
            holder.createInfoAnnotation(expression, "Automatically cast to " + DescriptorRenderer.TEXT.renderType(autoCast)).setTextAttributes(
70 71
                JetHighlightingColors.AUTO_CASTED_VALUE);
        }
72
        super.visitExpression(expression);
73 74
    }

75 76 77 78 79 80 81 82 83 84
    private void visitVariableDeclaration(JetNamedDeclaration declaration) {
        DeclarationDescriptor declarationDescriptor = bindingContext.get(DECLARATION_TO_DESCRIPTOR, declaration);
        PsiElement nameIdentifier = declaration.getNameIdentifier();
        if (nameIdentifier != null && declarationDescriptor != null) {
            highlightVariable(nameIdentifier, declarationDescriptor);
        }
    }

    private void highlightVariable(@NotNull PsiElement elementToHighlight, @NotNull DeclarationDescriptor descriptor) {
        if (descriptor instanceof VariableDescriptor) {
85 86
            VariableDescriptor variableDescriptor = (VariableDescriptor) descriptor;
            if (variableDescriptor.isVar()) {
87
                JetPsiChecker.highlightName(holder, elementToHighlight, JetHighlightingColors.MUTABLE_VARIABLE);
88
            }
89

90
            if (Boolean.TRUE.equals(bindingContext.get(CAPTURED_IN_CLOSURE, variableDescriptor))) {
91 92 93 94
                String msg = ((VariableDescriptor) descriptor).isVar()
                             ? "Wrapped into a reference object to be modified when captured in a closure"
                             : "Value captured in a closure";
                holder.createInfoAnnotation(elementToHighlight, msg).setTextAttributes(
95 96
                    JetHighlightingColors.WRAPPED_INTO_REF);
            }
97 98

            if (descriptor instanceof LocalVariableDescriptor) {
99
                JetPsiChecker.highlightName(holder, elementToHighlight, JetHighlightingColors.LOCAL_VARIABLE);
100 101 102
            }

            if (descriptor instanceof ValueParameterDescriptor) {
103
                JetPsiChecker.highlightName(holder, elementToHighlight, JetHighlightingColors.PARAMETER);
104
            }
105
        }
106 107
    }
}