From 7efc1bcb5cace97a9eda0455624718b83ac57505 Mon Sep 17 00:00:00 2001 From: Evgeny Gerashchenko Date: Thu, 29 Mar 2012 22:56:25 +0400 Subject: [PATCH] Added highlighting for classes, abstract classes, traits and type parameters. --- .../highlighter/JetColorSettingsPage.java | 5 ++ .../highlighter/JetHighlightingColors.java | 20 +++++ .../jet/plugin/highlighter/JetPsiChecker.java | 1 + .../TypeKindHighlightingVisitor.java | 87 +++++++++++++++++++ 4 files changed, 113 insertions(+) create mode 100644 idea/src/org/jetbrains/jet/plugin/highlighter/TypeKindHighlightingVisitor.java diff --git a/idea/src/org/jetbrains/jet/plugin/highlighter/JetColorSettingsPage.java b/idea/src/org/jetbrains/jet/plugin/highlighter/JetColorSettingsPage.java index 5b7ed79569f..8c5d66d661a 100644 --- a/idea/src/org/jetbrains/jet/plugin/highlighter/JetColorSettingsPage.java +++ b/idea/src/org/jetbrains/jet/plugin/highlighter/JetColorSettingsPage.java @@ -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), diff --git a/idea/src/org/jetbrains/jet/plugin/highlighter/JetHighlightingColors.java b/idea/src/org/jetbrains/jet/plugin/highlighter/JetHighlightingColors.java index 26377385413..d035acadc2a 100644 --- a/idea/src/org/jetbrains/jet/plugin/highlighter/JetHighlightingColors.java +++ b/idea/src/org/jetbrains/jet/plugin/highlighter/JetHighlightingColors.java @@ -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", diff --git a/idea/src/org/jetbrains/jet/plugin/highlighter/JetPsiChecker.java b/idea/src/org/jetbrains/jet/plugin/highlighter/JetPsiChecker.java index 4ce6180a790..d136a1bb452 100644 --- a/idea/src/org/jetbrains/jet/plugin/highlighter/JetPsiChecker.java +++ b/idea/src/org/jetbrains/jet/plugin/highlighter/JetPsiChecker.java @@ -59,6 +59,7 @@ public class JetPsiChecker implements Annotator { return new HighlightingVisitor[]{ new SoftKeywordsHighlightingVisitor(holder), new LabelsHighlightingVisitor(holder), + new TypeKindHighlightingVisitor(holder), }; } diff --git a/idea/src/org/jetbrains/jet/plugin/highlighter/TypeKindHighlightingVisitor.java b/idea/src/org/jetbrains/jet/plugin/highlighter/TypeKindHighlightingVisitor.java new file mode 100644 index 00000000000..53d89a91639 --- /dev/null +++ b/idea/src/org/jetbrains/jet/plugin/highlighter/TypeKindHighlightingVisitor.java @@ -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); + } +}