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
This commit is contained in:
Alexander Udalov
2016-03-09 15:01:25 +03:00
parent 52309cd10e
commit 173a838f8c
2 changed files with 10 additions and 29 deletions
@@ -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;
}
}
@@ -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<ParameterInfo>().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)
}
}
}
}