PropertiesHighlightingVisitor: j2K

This commit is contained in:
Dmitry Jemerov
2016-05-24 21:06:24 +02:00
parent 41cd55b281
commit b76585e03f
3 changed files with 56 additions and 69 deletions
@@ -19,7 +19,7 @@ package org.jetbrains.kotlin.idea.highlighter;
import com.intellij.lang.annotation.AnnotationHolder;
import org.jetbrains.kotlin.resolve.BindingContext;
public abstract class AfterAnalysisHighlightingVisitor extends HighlightingVisitor {
abstract class AfterAnalysisHighlightingVisitor extends HighlightingVisitor {
protected BindingContext bindingContext;
protected AfterAnalysisHighlightingVisitor(AnnotationHolder holder, BindingContext bindingContext) {
@@ -29,7 +29,7 @@ import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
import org.jetbrains.kotlin.resolve.calls.model.VariableAsFunctionResolvedCall
import org.jetbrains.kotlin.resolve.calls.tasks.isDynamic
class FunctionsHighlightingVisitor(holder: AnnotationHolder, bindingContext: BindingContext) :
internal class FunctionsHighlightingVisitor(holder: AnnotationHolder, bindingContext: BindingContext) :
AfterAnalysisHighlightingVisitor(holder, bindingContext) {
override fun visitNamedFunction(function: KtNamedFunction) {
@@ -14,92 +14,79 @@
* limitations under the License.
*/
package org.jetbrains.kotlin.idea.highlighter;
package org.jetbrains.kotlin.idea.highlighter
import com.intellij.lang.annotation.AnnotationHolder;
import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor;
import org.jetbrains.kotlin.descriptors.PropertyDescriptor;
import org.jetbrains.kotlin.descriptors.VariableDescriptor;
import org.jetbrains.kotlin.descriptors.impl.SyntheticFieldDescriptor;
import org.jetbrains.kotlin.psi.KtParameter;
import org.jetbrains.kotlin.psi.KtProperty;
import org.jetbrains.kotlin.psi.KtSimpleNameExpression;
import org.jetbrains.kotlin.psi.KtThisExpression;
import org.jetbrains.kotlin.resolve.BindingContext;
import org.jetbrains.kotlin.resolve.DescriptorUtils;
import org.jetbrains.kotlin.resolve.calls.tasks.DynamicCallsKt;
import com.intellij.lang.annotation.AnnotationHolder
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.descriptors.PropertyDescriptor
import org.jetbrains.kotlin.descriptors.impl.SyntheticFieldDescriptor
import org.jetbrains.kotlin.psi.KtParameter
import org.jetbrains.kotlin.psi.KtProperty
import org.jetbrains.kotlin.psi.KtSimpleNameExpression
import org.jetbrains.kotlin.psi.KtThisExpression
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.DescriptorUtils
import org.jetbrains.kotlin.resolve.calls.tasks.isDynamic
class PropertiesHighlightingVisitor extends AfterAnalysisHighlightingVisitor {
PropertiesHighlightingVisitor(AnnotationHolder holder, BindingContext bindingContext) {
super(holder, bindingContext);
internal class PropertiesHighlightingVisitor(holder: AnnotationHolder, bindingContext: BindingContext)
: AfterAnalysisHighlightingVisitor(holder, bindingContext) {
override fun visitSimpleNameExpression(expression: KtSimpleNameExpression) {
if (expression.parent is KtThisExpression) {
return
}
val target = bindingContext.get(BindingContext.REFERENCE_TARGET, expression)
if (target is SyntheticFieldDescriptor) {
NameHighlighter.highlightName(holder, expression, KotlinHighlightingColors.BACKING_FIELD_VARIABLE)
return
}
if (target !is PropertyDescriptor) {
return
}
highlightProperty(expression, (target as PropertyDescriptor?)!!, false)
}
@Override
public void visitSimpleNameExpression(@NotNull KtSimpleNameExpression expression) {
if (expression.getParent() instanceof KtThisExpression) {
return;
}
DeclarationDescriptor target = bindingContext.get(BindingContext.REFERENCE_TARGET, expression);
if (target instanceof SyntheticFieldDescriptor) {
NameHighlighter.highlightName(holder, expression, KotlinHighlightingColors.BACKING_FIELD_VARIABLE);
return;
}
if (!(target instanceof PropertyDescriptor)) {
return;
override fun visitProperty(property: KtProperty) {
val nameIdentifier = property.nameIdentifier ?: return
val propertyDescriptor = bindingContext.get(BindingContext.VARIABLE, property)
if (propertyDescriptor is PropertyDescriptor) {
val backingFieldRequired = bindingContext.get(BindingContext.BACKING_FIELD_REQUIRED, propertyDescriptor as PropertyDescriptor?)
highlightProperty(nameIdentifier, (propertyDescriptor as PropertyDescriptor?)!!, java.lang.Boolean.TRUE == backingFieldRequired)
}
highlightProperty(expression, (PropertyDescriptor) target, false);
super.visitProperty(property)
}
@Override
public void visitProperty(@NotNull KtProperty property) {
PsiElement nameIdentifier = property.getNameIdentifier();
if (nameIdentifier == null) return;
VariableDescriptor propertyDescriptor = bindingContext.get(BindingContext.VARIABLE, property);
if (propertyDescriptor instanceof PropertyDescriptor) {
Boolean backingFieldRequired = bindingContext.get(BindingContext.BACKING_FIELD_REQUIRED, (PropertyDescriptor)propertyDescriptor);
highlightProperty(nameIdentifier, (PropertyDescriptor) propertyDescriptor, Boolean.TRUE.equals(backingFieldRequired));
}
super.visitProperty(property);
}
@Override
public void visitParameter(@NotNull KtParameter parameter) {
PsiElement nameIdentifier = parameter.getNameIdentifier();
if (nameIdentifier == null) return;
PropertyDescriptor propertyDescriptor = bindingContext.get(BindingContext.PRIMARY_CONSTRUCTOR_PARAMETER, parameter);
override fun visitParameter(parameter: KtParameter) {
val nameIdentifier = parameter.nameIdentifier ?: return
val propertyDescriptor = bindingContext.get(BindingContext.PRIMARY_CONSTRUCTOR_PARAMETER, parameter)
if (propertyDescriptor != null) {
Boolean backingFieldRequired = bindingContext.get(BindingContext.BACKING_FIELD_REQUIRED, propertyDescriptor);
highlightProperty(nameIdentifier, propertyDescriptor, Boolean.TRUE.equals(backingFieldRequired));
val backingFieldRequired = bindingContext.get(BindingContext.BACKING_FIELD_REQUIRED, propertyDescriptor)
highlightProperty(nameIdentifier, propertyDescriptor, java.lang.Boolean.TRUE == backingFieldRequired)
}
super.visitParameter(parameter);
super.visitParameter(parameter)
}
private void highlightProperty(
@NotNull PsiElement elementToHighlight,
@NotNull PropertyDescriptor descriptor,
boolean withBackingField
) {
if (DynamicCallsKt.isDynamic(descriptor)) {
NameHighlighter.highlightName(holder, elementToHighlight, KotlinHighlightingColors.DYNAMIC_PROPERTY_CALL);
return;
private fun highlightProperty(
elementToHighlight: PsiElement,
descriptor: PropertyDescriptor,
withBackingField: Boolean) {
if (descriptor.isDynamic()) {
NameHighlighter.highlightName(holder, elementToHighlight, KotlinHighlightingColors.DYNAMIC_PROPERTY_CALL)
return
}
boolean isStatic = DescriptorUtils.isStaticDeclaration(descriptor);
val isStatic = DescriptorUtils.isStaticDeclaration(descriptor)
NameHighlighter.highlightName(
holder, elementToHighlight,
isStatic ? KotlinHighlightingColors.PACKAGE_PROPERTY : KotlinHighlightingColors.INSTANCE_PROPERTY
);
if (descriptor.getExtensionReceiverParameter() != null) {
NameHighlighter.highlightName(holder, elementToHighlight, KotlinHighlightingColors.EXTENSION_PROPERTY);
if (isStatic) KotlinHighlightingColors.PACKAGE_PROPERTY else KotlinHighlightingColors.INSTANCE_PROPERTY)
if (descriptor.extensionReceiverParameter != null) {
NameHighlighter.highlightName(holder, elementToHighlight, KotlinHighlightingColors.EXTENSION_PROPERTY)
}
if (withBackingField) {
holder.createInfoAnnotation(elementToHighlight, "This property has a backing field")
.setTextAttributes(KotlinHighlightingColors.PROPERTY_WITH_BACKING_FIELD);
holder.createInfoAnnotation(elementToHighlight, "This property has a backing field").textAttributes = KotlinHighlightingColors.PROPERTY_WITH_BACKING_FIELD
}
}
}