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