From 8a4e07e3e59fd91c22caa909d52367f16b1cecc4 Mon Sep 17 00:00:00 2001 From: Evgeny Gerashchenko Date: Mon, 2 Apr 2012 17:09:12 +0400 Subject: [PATCH] Added highlighting for function/constructor calls and declarations. --- .../FunctionsHighlightingVisitor.java | 99 +++++++++++++++++++ .../highlighter/JetColorSettingsPage.java | 5 + .../highlighter/JetHighlightingColors.java | 25 +++++ .../jet/plugin/highlighter/JetPsiChecker.java | 1 + 4 files changed, 130 insertions(+) create mode 100644 idea/src/org/jetbrains/jet/plugin/highlighter/FunctionsHighlightingVisitor.java diff --git a/idea/src/org/jetbrains/jet/plugin/highlighter/FunctionsHighlightingVisitor.java b/idea/src/org/jetbrains/jet/plugin/highlighter/FunctionsHighlightingVisitor.java new file mode 100644 index 00000000000..6cb32e4a138 --- /dev/null +++ b/idea/src/org/jetbrains/jet/plugin/highlighter/FunctionsHighlightingVisitor.java @@ -0,0 +1,99 @@ +/* + * 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.AnnotationHolder; +import com.intellij.psi.PsiElement; +import org.jetbrains.jet.lang.descriptors.ConstructorDescriptor; +import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor; +import org.jetbrains.jet.lang.descriptors.FunctionDescriptor; +import org.jetbrains.jet.lang.descriptors.NamespaceDescriptor; +import org.jetbrains.jet.lang.psi.*; +import org.jetbrains.jet.lang.resolve.BindingContext; +import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverDescriptor; + +/** + * @author Evgeny Gerashchenko + * @since 4/2/12 + */ +public class FunctionsHighlightingVisitor extends AfterAnalysisHighlightingVisitor { + public FunctionsHighlightingVisitor(AnnotationHolder holder, BindingContext bindingContext) { + super(holder, bindingContext); + } + + @Override + public void visitJetElement(JetElement element) { + element.acceptChildren(this); + } + + @Override + public void visitNamedFunction(JetNamedFunction function) { + PsiElement nameIdentifier = function.getNameIdentifier(); + if (nameIdentifier != null) { + holder.createInfoAnnotation(nameIdentifier, null).setTextAttributes( + JetHighlightingColors.FUNCTION_DECLARATION); + } + + super.visitNamedFunction(function); + } + + @Override + public void visitDelegationToSuperCallSpecifier(JetDelegatorToSuperCall call) { + JetConstructorCalleeExpression calleeExpression = call.getCalleeExpression(); + JetTypeReference typeRef = calleeExpression.getTypeReference(); + if (typeRef != null) { + JetTypeElement typeElement = typeRef.getTypeElement(); + if (typeElement instanceof JetUserType) { + JetSimpleNameExpression nameExpression = ((JetUserType)typeElement).getReferenceExpression(); + if (nameExpression != null) { + holder.createInfoAnnotation(nameExpression, null).setTextAttributes( + JetHighlightingColors.CONSTRUCTOR_CALL); + } + } + } + super.visitDelegationToSuperCallSpecifier(call); + } + + @Override + public void visitCallExpression(JetCallExpression expression) { + JetExpression callee = expression.getCalleeExpression(); + if (callee instanceof JetReferenceExpression) { + DeclarationDescriptor calleeDescriptor = bindingContext.get(BindingContext.REFERENCE_TARGET, (JetReferenceExpression)callee); + if (calleeDescriptor != null) { + if (calleeDescriptor instanceof ConstructorDescriptor) { + holder.createInfoAnnotation(callee, null).setTextAttributes( + JetHighlightingColors.CONSTRUCTOR_CALL); + } + else if (calleeDescriptor instanceof FunctionDescriptor) { + FunctionDescriptor fun = (FunctionDescriptor)calleeDescriptor; + if (fun.getReceiverParameter() != ReceiverDescriptor.NO_RECEIVER) { + holder.createInfoAnnotation(callee, null).setTextAttributes( + JetHighlightingColors.EXTENSION_FUNCTION_CALL); + } + else if (fun.getContainingDeclaration() instanceof NamespaceDescriptor) { + holder.createInfoAnnotation(callee, null).setTextAttributes(JetHighlightingColors.NAMESPACE_FUNCTION_CALL); + } + else { + holder.createInfoAnnotation(callee, null).setTextAttributes(JetHighlightingColors.FUNCTION_CALL); + } + } + } + } + + super.visitCallExpression(expression); + } +} diff --git a/idea/src/org/jetbrains/jet/plugin/highlighter/JetColorSettingsPage.java b/idea/src/org/jetbrains/jet/plugin/highlighter/JetColorSettingsPage.java index 226871dc80f..b9d4580a0f5 100644 --- a/idea/src/org/jetbrains/jet/plugin/highlighter/JetColorSettingsPage.java +++ b/idea/src/org/jetbrains/jet/plugin/highlighter/JetColorSettingsPage.java @@ -142,6 +142,11 @@ public class JetColorSettingsPage implements ColorSettingsPage { new AttributesDescriptor("Extension property", JetHighlightingColors.EXTENSION_PROPERTY), new AttributesDescriptor("Function literal default parameter", JetHighlightingColors.FUNCTION_LITERAL_DEFAULT_PARAMETER), + new AttributesDescriptor("Function declaration", JetHighlightingColors.FUNCTION_DECLARATION), + new AttributesDescriptor("Function call", JetHighlightingColors.FUNCTION_CALL), + new AttributesDescriptor("Namespace function call", JetHighlightingColors.NAMESPACE_FUNCTION_CALL), + new AttributesDescriptor("Extension function call", JetHighlightingColors.EXTENSION_FUNCTION_CALL), + new AttributesDescriptor("Constructor call", JetHighlightingColors.CONSTRUCTOR_CALL), new AttributesDescriptor(OptionsBundle.message("options.java.attribute.descriptor.bad.character"), JetHighlightingColors.BAD_CHARACTER), diff --git a/idea/src/org/jetbrains/jet/plugin/highlighter/JetHighlightingColors.java b/idea/src/org/jetbrains/jet/plugin/highlighter/JetHighlightingColors.java index 4cd1f3171a8..b3ca1bda1c9 100644 --- a/idea/src/org/jetbrains/jet/plugin/highlighter/JetHighlightingColors.java +++ b/idea/src/org/jetbrains/jet/plugin/highlighter/JetHighlightingColors.java @@ -211,6 +211,31 @@ public class JetHighlightingColors { new TextAttributes(null, null, null, null, Font.BOLD) ); + public static final TextAttributesKey FUNCTION_DECLARATION = TextAttributesKey.createTextAttributesKey( + "KOTLIN_FUNCTION_DECLARATION", + CodeInsightColors.METHOD_DECLARATION_ATTRIBUTES.getDefaultAttributes() + ); + + public static final TextAttributesKey FUNCTION_CALL = TextAttributesKey.createTextAttributesKey( + "KOTLIN_FUNCTION_CALL", + CodeInsightColors.METHOD_CALL_ATTRIBUTES.getDefaultAttributes() + ); + + public static final TextAttributesKey NAMESPACE_FUNCTION_CALL = TextAttributesKey.createTextAttributesKey( + "KOTLIN_NAMESPACE_FUNCTION_CALL", + CodeInsightColors.STATIC_METHOD_ATTRIBUTES.getDefaultAttributes() + ); + + public static final TextAttributesKey EXTENSION_FUNCTION_CALL = TextAttributesKey.createTextAttributesKey( + "KOTLIN_EXTENSION_FUNCTION_CALL", + CodeInsightColors.STATIC_METHOD_ATTRIBUTES.getDefaultAttributes() + ); + + public static final TextAttributesKey CONSTRUCTOR_CALL = TextAttributesKey.createTextAttributesKey( + "KOTLIN_CONSTRUCTOR", + CodeInsightColors.CONSTRUCTOR_CALL_ATTRIBUTES.getDefaultAttributes() + ); + public static final TextAttributesKey BAD_CHARACTER = TextAttributesKey.createTextAttributesKey( "KOTLIN_BAD_CHARACTER", HighlighterColors.BAD_CHARACTER.getDefaultAttributes() diff --git a/idea/src/org/jetbrains/jet/plugin/highlighter/JetPsiChecker.java b/idea/src/org/jetbrains/jet/plugin/highlighter/JetPsiChecker.java index 820443ce9c7..f4a458d3440 100644 --- a/idea/src/org/jetbrains/jet/plugin/highlighter/JetPsiChecker.java +++ b/idea/src/org/jetbrains/jet/plugin/highlighter/JetPsiChecker.java @@ -66,6 +66,7 @@ public class JetPsiChecker implements Annotator { private static HighlightingVisitor[] getAfterAnalysisVisitor(AnnotationHolder holder, BindingContext bindingContext) { return new AfterAnalysisHighlightingVisitor[]{ new PropertiesHighlightingVisitor(holder, bindingContext), + new FunctionsHighlightingVisitor(holder, bindingContext), new VariablesHighlightingVisitor(holder, bindingContext), }; }