From 24716479523b98a1d0582dce8c83faf9d8d31c7d Mon Sep 17 00:00:00 2001 From: Valentin Kipyatkov Date: Wed, 30 Sep 2015 16:21:28 +0300 Subject: [PATCH] Minor refactoring --- .../kotlin/idea/core/KotlinIndicesHelper.kt | 32 +++++++++++-------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/idea/idea-core/src/org/jetbrains/kotlin/idea/core/KotlinIndicesHelper.kt b/idea/idea-core/src/org/jetbrains/kotlin/idea/core/KotlinIndicesHelper.kt index 463234d212c..071bab37cf3 100644 --- a/idea/idea-core/src/org/jetbrains/kotlin/idea/core/KotlinIndicesHelper.kt +++ b/idea/idea-core/src/org/jetbrains/kotlin/idea/core/KotlinIndicesHelper.kt @@ -94,12 +94,12 @@ public class KotlinIndicesHelper( } public fun getCallableTopLevelExtensions(nameFilter: (String) -> Boolean, expression: JetSimpleNameExpression, bindingContext: BindingContext): Collection { - val receiverValues = receiverValues(expression, bindingContext) + val (callType, receiverValues) = receiverValues(expression, bindingContext) if (receiverValues.isEmpty()) return emptyList() val dataFlowInfo = bindingContext.getDataFlowInfo(expression) - val receiverTypeNames = possibleReceiverTypeNames(receiverValues.map { it.first }, dataFlowInfo, bindingContext) + val receiverTypeNames = possibleReceiverTypeNames(receiverValues, dataFlowInfo, bindingContext) val index = JetTopLevelExtensionsByReceiverTypeIndex.INSTANCE @@ -111,7 +111,7 @@ public class KotlinIndicesHelper( } .flatMap { index.get(it, project, scope).asSequence() } - return findSuitableExtensions(declarations, receiverValues, dataFlowInfo, bindingContext) + return findSuitableExtensions(declarations, callType to receiverValues, dataFlowInfo, bindingContext) } private fun possibleReceiverTypeNames(receiverValues: Collection, dataFlowInfo: DataFlowInfo, bindingContext: BindingContext): Set { @@ -131,22 +131,28 @@ public class KotlinIndicesHelper( constructor.getSupertypes().forEach { addTypeNames(it) } } - private fun receiverValues(expression: JetSimpleNameExpression, bindingContext: BindingContext): Collection>> { + private fun receiverValues(expression: JetSimpleNameExpression, bindingContext: BindingContext): Pair, Collection> { val callTypeAndReceiver = CallTypeAndReceiver.detect(expression) - val receiverElement = callTypeAndReceiver.receiver + val receiverValues = receiverValues(expression, callTypeAndReceiver.receiver, bindingContext) + return callTypeAndReceiver.callType to receiverValues + } + + private fun receiverValues( + expression: JetSimpleNameExpression, + receiverElement: TReceiver, + bindingContext: BindingContext + ): Collection { if (receiverElement != null) { if (receiverElement !is JetExpression) return emptyList() //TODO? val expressionType = bindingContext.getType(receiverElement) - if (expressionType == null || expressionType.isError()) return emptyList() + if (expressionType == null || expressionType.isError) return emptyList() - val receiverValue = ExpressionReceiver(receiverElement, expressionType) - - return listOf(receiverValue to callTypeAndReceiver.callType) + return listOf(ExpressionReceiver(receiverElement, expressionType)) } else { val resolutionScope = bindingContext[BindingContext.RESOLUTION_SCOPE, expression] ?: return emptyList() - return resolutionScope.getImplicitReceiversWithInstance().map { it.getValue() to callTypeAndReceiver.callType } + return resolutionScope.getImplicitReceiversWithInstance().map { it.value } } } @@ -155,7 +161,7 @@ public class KotlinIndicesHelper( */ private fun findSuitableExtensions( declarations: Sequence, - receiverValues: Collection>>, + receiverValues: Pair, Collection>, dataFlowInfo: DataFlowInfo, bindingContext: BindingContext ): Collection { @@ -163,8 +169,8 @@ public class KotlinIndicesHelper( fun processDescriptor(descriptor: CallableDescriptor) { if (descriptorFilter(descriptor)) { - for ((receiverValue, callType) in receiverValues) { - result.addAll(descriptor.substituteExtensionIfCallable(receiverValue, callType, bindingContext, dataFlowInfo, moduleDescriptor)) + for (receiverValue in receiverValues.second) { + result.addAll(descriptor.substituteExtensionIfCallable(receiverValue, receiverValues.first, bindingContext, dataFlowInfo, moduleDescriptor)) } } }