FunctionsHighlightingVisitor: J2K

This commit is contained in:
Dmitry Jemerov
2016-05-24 20:49:57 +02:00
parent 83b5028ac2
commit c515740e38
2 changed files with 50 additions and 58 deletions
@@ -19,7 +19,7 @@ package org.jetbrains.kotlin.idea.highlighter;
import com.intellij.lang.annotation.AnnotationHolder; import com.intellij.lang.annotation.AnnotationHolder;
import org.jetbrains.kotlin.resolve.BindingContext; import org.jetbrains.kotlin.resolve.BindingContext;
abstract class AfterAnalysisHighlightingVisitor extends HighlightingVisitor { public abstract class AfterAnalysisHighlightingVisitor extends HighlightingVisitor {
protected BindingContext bindingContext; protected BindingContext bindingContext;
protected AfterAnalysisHighlightingVisitor(AnnotationHolder holder, BindingContext bindingContext) { protected AfterAnalysisHighlightingVisitor(AnnotationHolder holder, BindingContext bindingContext) {
@@ -14,88 +14,80 @@
* 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.psi.PsiElement; import org.jetbrains.kotlin.builtins.isFunctionTypeOrSubtype
import org.jetbrains.annotations.NotNull; import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.builtins.FunctionTypesKt; import org.jetbrains.kotlin.descriptors.ConstructorDescriptor
import org.jetbrains.kotlin.descriptors.*; import org.jetbrains.kotlin.descriptors.FunctionDescriptor
import org.jetbrains.kotlin.psi.*; import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.resolve.BindingContext; import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.DescriptorUtils; import org.jetbrains.kotlin.resolve.DescriptorUtils
import org.jetbrains.kotlin.resolve.calls.callUtil.CallUtilKt; import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall; import org.jetbrains.kotlin.resolve.calls.model.VariableAsFunctionResolvedCall
import org.jetbrains.kotlin.resolve.calls.model.VariableAsFunctionResolvedCall; import org.jetbrains.kotlin.resolve.calls.tasks.isDynamic
import org.jetbrains.kotlin.resolve.calls.tasks.DynamicCallsKt;
public class FunctionsHighlightingVisitor extends AfterAnalysisHighlightingVisitor { class FunctionsHighlightingVisitor(holder: AnnotationHolder, bindingContext: BindingContext) :
public FunctionsHighlightingVisitor(AnnotationHolder holder, BindingContext bindingContext) { AfterAnalysisHighlightingVisitor(holder, bindingContext) {
super(holder, bindingContext);
}
@Override override fun visitNamedFunction(function: KtNamedFunction) {
public void visitNamedFunction(@NotNull KtNamedFunction function) { val nameIdentifier = function.nameIdentifier
PsiElement nameIdentifier = function.getNameIdentifier();
if (nameIdentifier != null) { if (nameIdentifier != null) {
NameHighlighter.highlightName(holder, nameIdentifier, KotlinHighlightingColors.FUNCTION_DECLARATION); NameHighlighter.highlightName(holder, nameIdentifier, KotlinHighlightingColors.FUNCTION_DECLARATION)
} }
super.visitNamedFunction(function); super.visitNamedFunction(function)
} }
@Override override fun visitSuperTypeCallEntry(call: KtSuperTypeCallEntry) {
public void visitSuperTypeCallEntry(@NotNull KtSuperTypeCallEntry call) { val calleeExpression = call.calleeExpression
KtConstructorCalleeExpression calleeExpression = call.getCalleeExpression(); val typeRef = calleeExpression.typeReference
KtTypeReference typeRef = calleeExpression.getTypeReference();
if (typeRef != null) { if (typeRef != null) {
KtTypeElement typeElement = typeRef.getTypeElement(); val typeElement = typeRef.typeElement
if (typeElement instanceof KtUserType) { if (typeElement is KtUserType) {
KtSimpleNameExpression nameExpression = ((KtUserType)typeElement).getReferenceExpression(); val nameExpression = typeElement.referenceExpression
if (nameExpression != null) { if (nameExpression != null) {
NameHighlighter.highlightName(holder, nameExpression, KotlinHighlightingColors.CONSTRUCTOR_CALL); NameHighlighter.highlightName(holder, nameExpression, KotlinHighlightingColors.CONSTRUCTOR_CALL)
} }
} }
} }
super.visitSuperTypeCallEntry(call); super.visitSuperTypeCallEntry(call)
} }
@Override override fun visitCallExpression(expression: KtCallExpression) {
public void visitCallExpression(@NotNull KtCallExpression expression) { val callee = expression.calleeExpression
KtExpression callee = expression.getCalleeExpression(); val resolvedCall = expression.getResolvedCall(bindingContext)
ResolvedCall<?> resolvedCall = CallUtilKt.getResolvedCall(expression, bindingContext); if (callee is KtReferenceExpression && resolvedCall != null) {
if (callee instanceof KtReferenceExpression && resolvedCall != null) { val calleeDescriptor = resolvedCall.resultingDescriptor
CallableDescriptor calleeDescriptor = resolvedCall.getResultingDescriptor();
if (DynamicCallsKt.isDynamic(calleeDescriptor)) { if (calleeDescriptor.isDynamic()) {
NameHighlighter.highlightName(holder, callee, KotlinHighlightingColors.DYNAMIC_FUNCTION_CALL); NameHighlighter.highlightName(holder, callee, KotlinHighlightingColors.DYNAMIC_FUNCTION_CALL)
} }
else if (resolvedCall instanceof VariableAsFunctionResolvedCall) { else if (resolvedCall is VariableAsFunctionResolvedCall) {
DeclarationDescriptor container = calleeDescriptor.getContainingDeclaration(); val container = calleeDescriptor.containingDeclaration
boolean containedInFunctionClassOrSubclass = val containedInFunctionClassOrSubclass = container is ClassDescriptor && container.defaultType.isFunctionTypeOrSubtype
container instanceof ClassDescriptor && NameHighlighter.highlightName(holder, callee, if (containedInFunctionClassOrSubclass)
FunctionTypesKt.isFunctionTypeOrSubtype(((ClassDescriptor) container).getDefaultType()); KotlinHighlightingColors.VARIABLE_AS_FUNCTION_CALL
NameHighlighter.highlightName(holder, callee, containedInFunctionClassOrSubclass else
? KotlinHighlightingColors.VARIABLE_AS_FUNCTION_CALL KotlinHighlightingColors.VARIABLE_AS_FUNCTION_LIKE_CALL)
: KotlinHighlightingColors.VARIABLE_AS_FUNCTION_LIKE_CALL);
} }
else { else {
if (calleeDescriptor instanceof ConstructorDescriptor) { if (calleeDescriptor is ConstructorDescriptor) {
NameHighlighter.highlightName(holder, callee, KotlinHighlightingColors.CONSTRUCTOR_CALL); NameHighlighter.highlightName(holder, callee, KotlinHighlightingColors.CONSTRUCTOR_CALL)
} }
else if (calleeDescriptor instanceof FunctionDescriptor) { else if (calleeDescriptor is FunctionDescriptor) {
FunctionDescriptor fun = (FunctionDescriptor) calleeDescriptor; NameHighlighter.highlightName(holder, callee, KotlinHighlightingColors.FUNCTION_CALL)
NameHighlighter.highlightName(holder, callee, KotlinHighlightingColors.FUNCTION_CALL); if (DescriptorUtils.isTopLevelDeclaration(calleeDescriptor)) {
if (DescriptorUtils.isTopLevelDeclaration(fun)) { NameHighlighter.highlightName(holder, callee, KotlinHighlightingColors.PACKAGE_FUNCTION_CALL)
NameHighlighter.highlightName(holder, callee, KotlinHighlightingColors.PACKAGE_FUNCTION_CALL);
} }
if (fun.getExtensionReceiverParameter() != null) { if (calleeDescriptor.extensionReceiverParameter != null) {
NameHighlighter.highlightName(holder, callee, KotlinHighlightingColors.EXTENSION_FUNCTION_CALL); NameHighlighter.highlightName(holder, callee, KotlinHighlightingColors.EXTENSION_FUNCTION_CALL)
} }
} }
} }
} }
super.visitCallExpression(expression); super.visitCallExpression(expression)
} }
} }