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 com.intellij.lang.annotation.AnnotationHolder;
import org.jetbrains.kotlin.resolve.BindingContext; import org.jetbrains.kotlin.resolve.BindingContext;
public abstract class AfterAnalysisHighlightingVisitor extends HighlightingVisitor { abstract class AfterAnalysisHighlightingVisitor extends HighlightingVisitor {
protected BindingContext bindingContext; protected BindingContext bindingContext;
protected AfterAnalysisHighlightingVisitor(AnnotationHolder holder, 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.model.VariableAsFunctionResolvedCall
import org.jetbrains.kotlin.resolve.calls.tasks.isDynamic import org.jetbrains.kotlin.resolve.calls.tasks.isDynamic
class FunctionsHighlightingVisitor(holder: AnnotationHolder, bindingContext: BindingContext) : internal class FunctionsHighlightingVisitor(holder: AnnotationHolder, bindingContext: BindingContext) :
AfterAnalysisHighlightingVisitor(holder, bindingContext) { AfterAnalysisHighlightingVisitor(holder, bindingContext) {
override fun visitNamedFunction(function: KtNamedFunction) { override fun visitNamedFunction(function: KtNamedFunction) {
@@ -14,92 +14,79 @@
* limitations under the License. * 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.lang.annotation.AnnotationHolder
import com.intellij.psi.PsiElement; import com.intellij.psi.PsiElement
import org.jetbrains.annotations.NotNull; import org.jetbrains.kotlin.descriptors.PropertyDescriptor
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor; import org.jetbrains.kotlin.descriptors.impl.SyntheticFieldDescriptor
import org.jetbrains.kotlin.descriptors.PropertyDescriptor; import org.jetbrains.kotlin.psi.KtParameter
import org.jetbrains.kotlin.descriptors.VariableDescriptor; import org.jetbrains.kotlin.psi.KtProperty
import org.jetbrains.kotlin.descriptors.impl.SyntheticFieldDescriptor; import org.jetbrains.kotlin.psi.KtSimpleNameExpression
import org.jetbrains.kotlin.psi.KtParameter; import org.jetbrains.kotlin.psi.KtThisExpression
import org.jetbrains.kotlin.psi.KtProperty; import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.psi.KtSimpleNameExpression; import org.jetbrains.kotlin.resolve.DescriptorUtils
import org.jetbrains.kotlin.psi.KtThisExpression; import org.jetbrains.kotlin.resolve.calls.tasks.isDynamic
import org.jetbrains.kotlin.resolve.BindingContext;
import org.jetbrains.kotlin.resolve.DescriptorUtils;
import org.jetbrains.kotlin.resolve.calls.tasks.DynamicCallsKt;
class PropertiesHighlightingVisitor extends AfterAnalysisHighlightingVisitor { internal class PropertiesHighlightingVisitor(holder: AnnotationHolder, bindingContext: BindingContext)
PropertiesHighlightingVisitor(AnnotationHolder holder, BindingContext bindingContext) { : AfterAnalysisHighlightingVisitor(holder, bindingContext) {
super(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 override fun visitProperty(property: KtProperty) {
public void visitSimpleNameExpression(@NotNull KtSimpleNameExpression expression) { val nameIdentifier = property.nameIdentifier ?: return
if (expression.getParent() instanceof KtThisExpression) { val propertyDescriptor = bindingContext.get(BindingContext.VARIABLE, property)
return; if (propertyDescriptor is PropertyDescriptor) {
} val backingFieldRequired = bindingContext.get(BindingContext.BACKING_FIELD_REQUIRED, propertyDescriptor as PropertyDescriptor?)
DeclarationDescriptor target = bindingContext.get(BindingContext.REFERENCE_TARGET, expression); highlightProperty(nameIdentifier, (propertyDescriptor as PropertyDescriptor?)!!, java.lang.Boolean.TRUE == backingFieldRequired)
if (target instanceof SyntheticFieldDescriptor) {
NameHighlighter.highlightName(holder, expression, KotlinHighlightingColors.BACKING_FIELD_VARIABLE);
return;
}
if (!(target instanceof PropertyDescriptor)) {
return;
} }
highlightProperty(expression, (PropertyDescriptor) target, false); super.visitProperty(property)
} }
@Override override fun visitParameter(parameter: KtParameter) {
public void visitProperty(@NotNull KtProperty property) { val nameIdentifier = parameter.nameIdentifier ?: return
PsiElement nameIdentifier = property.getNameIdentifier(); val propertyDescriptor = bindingContext.get(BindingContext.PRIMARY_CONSTRUCTOR_PARAMETER, parameter)
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);
if (propertyDescriptor != null) { if (propertyDescriptor != null) {
Boolean backingFieldRequired = bindingContext.get(BindingContext.BACKING_FIELD_REQUIRED, propertyDescriptor); val backingFieldRequired = bindingContext.get(BindingContext.BACKING_FIELD_REQUIRED, propertyDescriptor)
highlightProperty(nameIdentifier, propertyDescriptor, Boolean.TRUE.equals(backingFieldRequired)); highlightProperty(nameIdentifier, propertyDescriptor, java.lang.Boolean.TRUE == backingFieldRequired)
} }
super.visitParameter(parameter); super.visitParameter(parameter)
} }
private void highlightProperty( private fun highlightProperty(
@NotNull PsiElement elementToHighlight, elementToHighlight: PsiElement,
@NotNull PropertyDescriptor descriptor, descriptor: PropertyDescriptor,
boolean withBackingField withBackingField: Boolean) {
) { if (descriptor.isDynamic()) {
if (DynamicCallsKt.isDynamic(descriptor)) { NameHighlighter.highlightName(holder, elementToHighlight, KotlinHighlightingColors.DYNAMIC_PROPERTY_CALL)
NameHighlighter.highlightName(holder, elementToHighlight, KotlinHighlightingColors.DYNAMIC_PROPERTY_CALL); return
return;
} }
boolean isStatic = DescriptorUtils.isStaticDeclaration(descriptor); val isStatic = DescriptorUtils.isStaticDeclaration(descriptor)
NameHighlighter.highlightName( NameHighlighter.highlightName(
holder, elementToHighlight, holder, elementToHighlight,
isStatic ? KotlinHighlightingColors.PACKAGE_PROPERTY : KotlinHighlightingColors.INSTANCE_PROPERTY if (isStatic) KotlinHighlightingColors.PACKAGE_PROPERTY else KotlinHighlightingColors.INSTANCE_PROPERTY)
); if (descriptor.extensionReceiverParameter != null) {
if (descriptor.getExtensionReceiverParameter() != null) { NameHighlighter.highlightName(holder, elementToHighlight, KotlinHighlightingColors.EXTENSION_PROPERTY)
NameHighlighter.highlightName(holder, elementToHighlight, KotlinHighlightingColors.EXTENSION_PROPERTY);
} }
if (withBackingField) { if (withBackingField) {
holder.createInfoAnnotation(elementToHighlight, "This property has a backing field") holder.createInfoAnnotation(elementToHighlight, "This property has a backing field").textAttributes = KotlinHighlightingColors.PROPERTY_WITH_BACKING_FIELD
.setTextAttributes(KotlinHighlightingColors.PROPERTY_WITH_BACKING_FIELD);
} }
} }
} }