Dsl highlighting: support highlighting properties

Fix properties not being highlighted
Fix test infrastructure to verify actual output of highlighting visitors
This commit is contained in:
Pavel V. Talanov
2018-08-01 18:43:54 +02:00
parent c250f7fca4
commit e9a9f2a1aa
8 changed files with 107 additions and 31 deletions
@@ -19,6 +19,6 @@ package org.jetbrains.kotlin.idea.highlighter
import com.intellij.lang.annotation.AnnotationHolder
import org.jetbrains.kotlin.resolve.BindingContext
internal abstract class AfterAnalysisHighlightingVisitor protected constructor(
abstract class AfterAnalysisHighlightingVisitor protected constructor(
holder: AnnotationHolder, protected var bindingContext: BindingContext
) : HighlightingVisitor(holder)
@@ -48,16 +48,8 @@ abstract class HighlightingVisitor protected constructor(
}
}
protected fun applyHighlighterExtensions(element: PsiElement, descriptor: DeclarationDescriptor): Boolean {
if (!NameHighlighter.namesHighlightingEnabled) return false
protected fun attributeKeyForDeclarationFromExtensions(element: PsiElement, descriptor: DeclarationDescriptor) =
Extensions.getExtensions(HighlighterExtension.EP_NAME).firstNotNullResult { extension ->
extension.highlightDeclaration(element, descriptor)
}?.let { key ->
highlightName(element, key)
return true
}
return false
}
}
@@ -97,7 +97,7 @@ open class KotlinPsiChecker : Annotator, HighlightRangeExtension {
}
companion object {
private fun getAfterAnalysisVisitor(holder: AnnotationHolder, bindingContext: BindingContext) = arrayOf(
fun getAfterAnalysisVisitor(holder: AnnotationHolder, bindingContext: BindingContext) = arrayOf(
PropertiesHighlightingVisitor(holder, bindingContext),
FunctionsHighlightingVisitor(holder, bindingContext),
VariablesHighlightingVisitor(holder, bindingContext),
@@ -6,6 +6,8 @@
package org.jetbrains.kotlin.idea.highlighter
import com.intellij.lang.annotation.AnnotationHolder
import com.intellij.openapi.editor.colors.TextAttributesKey
import com.intellij.openapi.extensions.Extensions
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.descriptors.PropertyDescriptor
import org.jetbrains.kotlin.descriptors.impl.SyntheticFieldDescriptor
@@ -16,8 +18,10 @@ 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.callUtil.getResolvedCall
import org.jetbrains.kotlin.resolve.calls.tasks.isDynamic
import org.jetbrains.kotlin.resolve.calls.tower.isSynthesized
import org.jetbrains.kotlin.utils.addToStdlib.firstNotNullResult
internal class PropertiesHighlightingVisitor(holder: AnnotationHolder, bindingContext: BindingContext)
: AfterAnalysisHighlightingVisitor(holder, bindingContext) {
@@ -35,14 +39,23 @@ internal class PropertiesHighlightingVisitor(holder: AnnotationHolder, bindingCo
return
}
highlightProperty(expression, target)
val resolvedCall = expression.getResolvedCall(bindingContext)
val attributesKey = resolvedCall?.let { call ->
Extensions.getExtensions(HighlighterExtension.EP_NAME).firstNotNullResult { extension ->
extension.highlightCall(expression, call)
}
} ?: attributeKeyByPropertyType(target)
highlightName(expression, attributesKey)
}
override fun visitProperty(property: KtProperty) {
val nameIdentifier = property.nameIdentifier ?: return
val propertyDescriptor = bindingContext.get(BindingContext.VARIABLE, property)
if (propertyDescriptor is PropertyDescriptor) {
highlightProperty(nameIdentifier, propertyDescriptor)
highlightPropertyDeclaration(nameIdentifier, propertyDescriptor)
}
super.visitProperty(property)
@@ -55,19 +68,24 @@ internal class PropertiesHighlightingVisitor(holder: AnnotationHolder, bindingCo
if (propertyDescriptor.isVar) {
highlightName(nameIdentifier, MUTABLE_VARIABLE)
}
highlightProperty(nameIdentifier, propertyDescriptor)
highlightPropertyDeclaration(nameIdentifier, propertyDescriptor)
}
super.visitParameter(parameter)
}
private fun highlightProperty(
elementToHighlight: PsiElement,
descriptor: PropertyDescriptor) {
private fun highlightPropertyDeclaration(
elementToHighlight: PsiElement,
descriptor: PropertyDescriptor
) {
highlightName(
elementToHighlight,
attributeKeyForDeclarationFromExtensions(elementToHighlight, descriptor) ?: attributeKeyByPropertyType(descriptor)
)
}
if (applyHighlighterExtensions(elementToHighlight, descriptor)) return
val attributesKey = when {
private fun attributeKeyByPropertyType(descriptor: PropertyDescriptor): TextAttributesKey {
return when {
descriptor.isDynamic() ->
DYNAMIC_PROPERTY_CALL
@@ -80,6 +98,5 @@ internal class PropertiesHighlightingVisitor(holder: AnnotationHolder, bindingCo
else ->
INSTANCE_PROPERTY
}
highlightName(elementToHighlight, attributesKey)
}
}
@@ -89,8 +89,11 @@ internal class TypeKindHighlightingVisitor(holder: AnnotationHolder, bindingCont
val identifier = classOrObject.nameIdentifier
val classDescriptor = bindingContext.get(BindingContext.CLASS, classOrObject)
if (identifier != null && classDescriptor != null) {
if (applyHighlighterExtensions(identifier, classDescriptor)) return
highlightName(identifier, textAttributesKeyForClass(classDescriptor))
highlightName(
identifier,
attributeKeyForDeclarationFromExtensions(classOrObject, classDescriptor)
?: textAttributesKeyForClass(classDescriptor)
)
}
super.visitClassOrObject(classOrObject)
}
@@ -99,8 +102,10 @@ internal class TypeKindHighlightingVisitor(holder: AnnotationHolder, bindingCont
val identifier = typeAlias.nameIdentifier
val descriptor = bindingContext.get(BindingContext.TYPE_ALIAS, typeAlias)
if (identifier != null && descriptor != null) {
if (applyHighlighterExtensions(identifier, descriptor)) return
highlightName(identifier, TYPE_ALIAS)
highlightName(
identifier,
attributeKeyForDeclarationFromExtensions(identifier, descriptor) ?: TYPE_ALIAS
)
}
super.visitTypeAlias(typeAlias)
}