From 173a838f8c40ec8a3e10e656f8e8497e9302d8a7 Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Wed, 9 Mar 2016 15:01:25 +0300 Subject: [PATCH] Minor, improve some usages of function type utilities Re change in FunctionsHighlightingVisitor: KBI#isFunctionOrExtensionFunctionType already takes care of supertypes, no need to do additional loop --- .../FunctionsHighlightingVisitor.java | 33 ++++--------------- ...ctionFromCallableReferenceActionFactory.kt | 6 ++-- 2 files changed, 10 insertions(+), 29 deletions(-) diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/highlighter/FunctionsHighlightingVisitor.java b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/highlighter/FunctionsHighlightingVisitor.java index a4017fcb760..907f2635181 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/highlighter/FunctionsHighlightingVisitor.java +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/highlighter/FunctionsHighlightingVisitor.java @@ -28,8 +28,6 @@ import org.jetbrains.kotlin.resolve.calls.callUtil.CallUtilKt; import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall; import org.jetbrains.kotlin.resolve.calls.model.VariableAsFunctionResolvedCall; import org.jetbrains.kotlin.resolve.calls.tasks.DynamicCallsKt; -import org.jetbrains.kotlin.types.KotlinType; -import org.jetbrains.kotlin.types.TypeUtils; public class FunctionsHighlightingVisitor extends AfterAnalysisHighlightingVisitor { public FunctionsHighlightingVisitor(AnnotationHolder holder, BindingContext bindingContext) { @@ -73,9 +71,13 @@ public class FunctionsHighlightingVisitor extends AfterAnalysisHighlightingVisit NameHighlighter.highlightName(holder, callee, KotlinHighlightingColors.DYNAMIC_FUNCTION_CALL); } else if (resolvedCall instanceof VariableAsFunctionResolvedCall) { - NameHighlighter.highlightName(holder, callee, containedInFunctionClassOrSubclass(calleeDescriptor) - ? KotlinHighlightingColors.VARIABLE_AS_FUNCTION_CALL - : KotlinHighlightingColors.VARIABLE_AS_FUNCTION_LIKE_CALL); + DeclarationDescriptor container = calleeDescriptor.getContainingDeclaration(); + boolean containedInFunctionClassOrSubclass = + container instanceof ClassDescriptor && + KotlinBuiltIns.isFunctionOrExtensionFunctionType(((ClassDescriptor) container).getDefaultType()); + NameHighlighter.highlightName(holder, callee, containedInFunctionClassOrSubclass + ? KotlinHighlightingColors.VARIABLE_AS_FUNCTION_CALL + : KotlinHighlightingColors.VARIABLE_AS_FUNCTION_LIKE_CALL); } else { if (calleeDescriptor instanceof ConstructorDescriptor) { @@ -96,25 +98,4 @@ public class FunctionsHighlightingVisitor extends AfterAnalysisHighlightingVisit super.visitCallExpression(expression); } - - private static boolean containedInFunctionClassOrSubclass(DeclarationDescriptor calleeDescriptor) { - DeclarationDescriptor parent = calleeDescriptor.getContainingDeclaration(); - if (!(parent instanceof ClassDescriptor)) { - return false; - } - - KotlinType defaultType = ((ClassDescriptor) parent).getDefaultType(); - - if (KotlinBuiltIns.isFunctionOrExtensionFunctionType(defaultType)) { - return true; - } - - for (KotlinType supertype : TypeUtils.getAllSupertypes(defaultType)) { - if (KotlinBuiltIns.isFunctionOrExtensionFunctionType(supertype)) { - return true; - } - } - - return false; - } } diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/createFromUsage/createCallable/CreateFunctionFromCallableReferenceActionFactory.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/createFromUsage/createCallable/CreateFunctionFromCallableReferenceActionFactory.kt index fd6edd97b7c..1e6ab342805 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/createFromUsage/createCallable/CreateFunctionFromCallableReferenceActionFactory.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/createFromUsage/createCallable/CreateFunctionFromCallableReferenceActionFactory.kt @@ -39,13 +39,13 @@ object CreateFunctionFromCallableReferenceActionFactory : CreateCallableMemberFr val context = resolutionFacade.analyze(element, BodyResolveMode.PARTIAL) return element .guessTypes(context, resolutionFacade.moduleDescriptor) - .filter { KotlinBuiltIns.isExactFunctionType(it) || KotlinBuiltIns.isExactExtensionFunctionType(it) } + .filter(KotlinBuiltIns::isExactFunctionOrExtensionFunctionType) .mapNotNull { val expectedReceiverType = KotlinBuiltIns.getReceiverType(it) val actualReceiverTypeRef = element.typeReference val receiverTypeInfo = actualReceiverTypeRef?.let { TypeInfo(it, Variance.IN_VARIANCE) } ?: TypeInfo.Empty val returnTypeInfo = TypeInfo(KotlinBuiltIns.getReturnTypeFromFunctionType(it), Variance.OUT_VARIANCE) - val containers = element.getExtractionContainers(includeAll = true).ifEmpty { return@mapNotNull null } + val containers = element.getExtractionContainers(includeAll = true).ifEmpty { return@mapNotNull null } val parameterInfos = SmartList().apply { if (actualReceiverTypeRef == null && expectedReceiverType != null) { add(ParameterInfo(TypeInfo(expectedReceiverType, Variance.IN_VARIANCE))) @@ -60,4 +60,4 @@ object CreateFunctionFromCallableReferenceActionFactory : CreateCallableMemberFr FunctionInfo(name, receiverTypeInfo, returnTypeInfo, containers, parameterInfos) } } -} \ No newline at end of file +}