Added highlighting for classes, abstract classes, traits and type parameters.

This commit is contained in:
Evgeny Gerashchenko
2012-03-29 22:56:25 +04:00
parent 537b2733dc
commit 7efc1bcb5c
4 changed files with 113 additions and 0 deletions
@@ -123,6 +123,11 @@ public class JetColorSettingsPage implements ColorSettingsPage {
new AttributesDescriptor("KDoc tag value", JetHighlightingColors.DOC_COMMENT_TAG_VALUE),
new AttributesDescriptor("KDoc markup", JetHighlightingColors.DOC_COMMENT_MARKUP),
new AttributesDescriptor(OptionsBundle.message("options.java.attribute.descriptor.class"), JetHighlightingColors.CLASS),
new AttributesDescriptor(OptionsBundle.message("options.java.attribute.descriptor.type.parameter"), JetHighlightingColors.TYPE_PARAMETER),
new AttributesDescriptor(OptionsBundle.message("options.java.attribute.descriptor.abstract.class"), JetHighlightingColors.ABSTRACT_CLASS),
new AttributesDescriptor("Trait", JetHighlightingColors.TRAIT),
new AttributesDescriptor("Wrapped into ref", JetHighlightingColors.WRAPPED_INTO_REF),
new AttributesDescriptor("Instance property with backing field", JetHighlightingColors.INSTANCE_PROPERTY_WITH_BACKING_FIELD),
@@ -136,6 +136,26 @@ public class JetHighlightingColors {
SyntaxHighlighterColors.DOC_COMMENT_MARKUP.getDefaultAttributes()
);
public static final TextAttributesKey CLASS = TextAttributesKey.createTextAttributesKey(
"KOTLIN_CLASS",
CodeInsightColors.CLASS_NAME_ATTRIBUTES.getDefaultAttributes()
);
public static final TextAttributesKey TYPE_PARAMETER = TextAttributesKey.createTextAttributesKey(
"KOTLIN_TYPE_PARAMETER",
CodeInsightColors.TYPE_PARAMETER_NAME_ATTRIBUTES.getDefaultAttributes()
);
public static final TextAttributesKey ABSTRACT_CLASS = TextAttributesKey.createTextAttributesKey(
"KOTLIN_ABSTRACT_CLASS",
CodeInsightColors.ABSTRACT_CLASS_NAME_ATTRIBUTES.getDefaultAttributes()
);
public static final TextAttributesKey TRAIT = TextAttributesKey.createTextAttributesKey(
"KOTLIN_TRAIT",
CodeInsightColors.INTERFACE_NAME_ATTRIBUTES.getDefaultAttributes()
);
// TODO review: is it needed?
public static final TextAttributesKey WRAPPED_INTO_REF = TextAttributesKey.createTextAttributesKey(
"KOTLIN_WRAPPED_INTO_REF",
@@ -59,6 +59,7 @@ public class JetPsiChecker implements Annotator {
return new HighlightingVisitor[]{
new SoftKeywordsHighlightingVisitor(holder),
new LabelsHighlightingVisitor(holder),
new TypeKindHighlightingVisitor(holder),
};
}
@@ -0,0 +1,87 @@
/*
* Copyright 2010-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.plugin.highlighter;
import com.intellij.lang.annotation.Annotation;
import com.intellij.lang.annotation.AnnotationHolder;
import com.intellij.openapi.editor.colors.TextAttributesKey;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiReference;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lexer.JetTokens;
/**
* @author Evgeny Gerashchenko
* @since 3/29/12
*/
class TypeKindHighlightingVisitor extends HighlightingVisitor {
protected TypeKindHighlightingVisitor(AnnotationHolder holder) {
super(holder);
}
private void visitNameExpression(JetExpression expression) {
PsiReference ref = expression.getReference();
if (ref == null) return;
PsiElement target = ref.resolve();
if (target instanceof JetClass) {
highlightClassByKind((JetClass)target, expression);
} else if (target instanceof JetTypeParameter) {
holder.createInfoAnnotation(expression, null).setTextAttributes(JetHighlightingColors.TYPE_PARAMETER);
}
}
@Override
public void visitSimpleNameExpression(JetSimpleNameExpression expression) {
visitNameExpression(expression);
}
@Override
public void visitQualifiedExpression(JetQualifiedExpression expression) {
visitNameExpression(expression);
}
@Override
public void visitTypeParameter(JetTypeParameter parameter) {
PsiElement identifier = parameter.getNameIdentifier();
if (identifier != null) {
holder.createInfoAnnotation(identifier, null).setTextAttributes(JetHighlightingColors.TYPE_PARAMETER);
}
}
@Override
public void visitClass(JetClass klass) {
PsiElement identifier = klass.getNameIdentifier();
if (identifier != null) {
highlightClassByKind(klass, identifier);
}
}
private void highlightClassByKind(@NotNull JetClass klass, @NotNull PsiElement whatToHighlight) {
TextAttributesKey textAttributes = JetHighlightingColors.CLASS;
if (klass.isTrait()) {
textAttributes = JetHighlightingColors.TRAIT;
} else {
JetModifierList modifierList = klass.getModifierList();
if (modifierList != null && modifierList.hasModifier(JetTokens.ABSTRACT_KEYWORD)) {
textAttributes = JetHighlightingColors.ABSTRACT_CLASS;
}
}
Annotation annotation = holder.createInfoAnnotation(whatToHighlight, null);
annotation.setTextAttributes(textAttributes);
}
}