TypeKindHighlightingVisitor: J2K

This commit is contained in:
Dmitry Jemerov
2016-05-25 14:40:56 +02:00
parent e07e16abf6
commit e7cb256e81
@@ -14,112 +14,100 @@
* 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.openapi.editor.colors.TextAttributesKey; import com.intellij.openapi.editor.colors.TextAttributesKey
import com.intellij.openapi.util.TextRange; import com.intellij.openapi.util.TextRange
import com.intellij.psi.PsiElement; import com.intellij.psi.PsiElement
import com.intellij.psi.util.PsiTreeUtil; import com.intellij.psi.util.PsiTreeUtil
import org.jetbrains.annotations.NotNull; import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.descriptors.*; import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.*; import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.BindingContext;
class TypeKindHighlightingVisitor extends AfterAnalysisHighlightingVisitor { internal class TypeKindHighlightingVisitor(holder: AnnotationHolder, bindingContext: BindingContext)
TypeKindHighlightingVisitor(AnnotationHolder holder, BindingContext bindingContext) { : AfterAnalysisHighlightingVisitor(holder, bindingContext) {
super(holder, bindingContext);
}
@Override override fun visitSimpleNameExpression(expression: KtSimpleNameExpression) {
public void visitSimpleNameExpression(@NotNull KtSimpleNameExpression expression) { val parent = expression.parent
PsiElement parent = expression.getParent(); if (parent is KtSuperExpression || parent is KtThisExpression) {
if (parent instanceof KtSuperExpression || parent instanceof KtThisExpression) {
// Do nothing: 'super' and 'this' are highlighted as a keyword // Do nothing: 'super' and 'this' are highlighted as a keyword
return; return
} }
if (NameHighlighter.INSTANCE.getNamesHighlightingEnabled()) { if (NameHighlighter.namesHighlightingEnabled) {
DeclarationDescriptor referenceTarget = bindingContext.get(BindingContext.REFERENCE_TARGET, expression); var referenceTarget: DeclarationDescriptor? = bindingContext.get(BindingContext.REFERENCE_TARGET, expression)
if (referenceTarget instanceof ConstructorDescriptor) { if (referenceTarget is ConstructorDescriptor) {
referenceTarget = referenceTarget.getContainingDeclaration(); referenceTarget = referenceTarget.containingDeclaration
} }
if (referenceTarget instanceof ClassDescriptor) { if (referenceTarget is ClassDescriptor) {
TextAttributesKey textAttributesKey = textAttributesKeyForClass((ClassDescriptor) referenceTarget); val textAttributesKey = textAttributesKeyForClass(referenceTarget)
if (textAttributesKey == KotlinHighlightingColors.ANNOTATION) { if (textAttributesKey === KotlinHighlightingColors.ANNOTATION) {
highlightAnnotation(expression); highlightAnnotation(expression)
} }
else { else {
highlightName(expression, textAttributesKey); highlightName(expression, textAttributesKey)
} }
} }
else if (referenceTarget instanceof TypeParameterDescriptor) { else if (referenceTarget is TypeParameterDescriptor) {
highlightName(expression, KotlinHighlightingColors.TYPE_PARAMETER); highlightName(expression, KotlinHighlightingColors.TYPE_PARAMETER)
} }
} }
} }
private void highlightAnnotation(@NotNull KtSimpleNameExpression expression) { private fun highlightAnnotation(expression: KtSimpleNameExpression) {
TextRange range = expression.getTextRange(); var range = expression.textRange
// include '@' symbol if the reference is the first segment of KtAnnotationEntry // include '@' symbol if the reference is the first segment of KtAnnotationEntry
// if "Deprecated" is highlighted then '@' should be highlighted too in "@Deprecated" // if "Deprecated" is highlighted then '@' should be highlighted too in "@Deprecated"
KtAnnotationEntry annotationEntry = PsiTreeUtil.getParentOfType( val annotationEntry = PsiTreeUtil.getParentOfType(
expression, KtAnnotationEntry.class, /* strict = */false, KtValueArgumentList.class); expression, KtAnnotationEntry::class.java, /* strict = */false, KtValueArgumentList::class.java)
if (annotationEntry != null) { if (annotationEntry != null) {
PsiElement atSymbol = annotationEntry.getAtSymbol(); val atSymbol = annotationEntry.atSymbol
if (atSymbol != null) { if (atSymbol != null) {
range = new TextRange(atSymbol.getTextRange().getStartOffset(), expression.getTextRange().getEndOffset()); range = TextRange(atSymbol.textRange.startOffset, expression.textRange.endOffset)
} }
} }
NameHighlighter.highlightName(holder, range, KotlinHighlightingColors.ANNOTATION); NameHighlighter.highlightName(holder, range, KotlinHighlightingColors.ANNOTATION)
} }
@Override override fun visitTypeParameter(parameter: KtTypeParameter) {
public void visitTypeParameter(@NotNull KtTypeParameter parameter) { val identifier = parameter.nameIdentifier
PsiElement identifier = parameter.getNameIdentifier();
if (identifier != null) { if (identifier != null) {
highlightName(identifier, KotlinHighlightingColors.TYPE_PARAMETER); highlightName(identifier, KotlinHighlightingColors.TYPE_PARAMETER)
} }
super.visitTypeParameter(parameter); super.visitTypeParameter(parameter)
} }
@Override override fun visitClassOrObject(classOrObject: KtClassOrObject) {
public void visitClassOrObject(@NotNull KtClassOrObject classOrObject) { val identifier = classOrObject.nameIdentifier
PsiElement identifier = classOrObject.getNameIdentifier(); val classDescriptor = bindingContext.get(BindingContext.CLASS, classOrObject)
ClassDescriptor classDescriptor = bindingContext.get(BindingContext.CLASS, classOrObject);
if (identifier != null && classDescriptor != null) { if (identifier != null && classDescriptor != null) {
highlightName(identifier, textAttributesKeyForClass(classDescriptor)); highlightName(identifier, textAttributesKeyForClass(classDescriptor))
} }
super.visitClassOrObject(classOrObject); super.visitClassOrObject(classOrObject)
} }
@Override override fun visitDynamicType(type: KtDynamicType) {
public void visitDynamicType(@NotNull KtDynamicType type) {
// Do nothing: 'dynamic' is highlighted as a keyword // Do nothing: 'dynamic' is highlighted as a keyword
} }
private void highlightName(@NotNull PsiElement whatToHighlight, @NotNull TextAttributesKey textAttributesKey) { private fun highlightName(whatToHighlight: PsiElement, textAttributesKey: TextAttributesKey) {
NameHighlighter.highlightName(holder, whatToHighlight, textAttributesKey); NameHighlighter.highlightName(holder, whatToHighlight, textAttributesKey)
} }
@NotNull private fun textAttributesKeyForClass(descriptor: ClassDescriptor): TextAttributesKey {
private static TextAttributesKey textAttributesKeyForClass(@NotNull ClassDescriptor descriptor) { when (descriptor.kind) {
switch (descriptor.getKind()) { ClassKind.INTERFACE -> return KotlinHighlightingColors.TRAIT
case INTERFACE: ClassKind.ANNOTATION_CLASS -> return KotlinHighlightingColors.ANNOTATION
return KotlinHighlightingColors.TRAIT; ClassKind.OBJECT -> return KotlinHighlightingColors.OBJECT
case ANNOTATION_CLASS: ClassKind.ENUM_ENTRY -> return KotlinHighlightingColors.ENUM_ENTRY
return KotlinHighlightingColors.ANNOTATION; else -> return if (descriptor.modality === Modality.ABSTRACT)
case OBJECT: KotlinHighlightingColors.ABSTRACT_CLASS
return KotlinHighlightingColors.OBJECT; else
case ENUM_ENTRY: KotlinHighlightingColors.CLASS
return KotlinHighlightingColors.ENUM_ENTRY;
default:
return descriptor.getModality() == Modality.ABSTRACT
? KotlinHighlightingColors.ABSTRACT_CLASS
: KotlinHighlightingColors.CLASS;
} }
} }
} }