diff --git a/idea/ide-common/src/org/jetbrains/kotlin/idea/codeInsight/ReferenceVariantsHelper.kt b/idea/ide-common/src/org/jetbrains/kotlin/idea/codeInsight/ReferenceVariantsHelper.kt index 0e780e9612d..92a63aadcd9 100644 --- a/idea/ide-common/src/org/jetbrains/kotlin/idea/codeInsight/ReferenceVariantsHelper.kt +++ b/idea/ide-common/src/org/jetbrains/kotlin/idea/codeInsight/ReferenceVariantsHelper.kt @@ -56,16 +56,16 @@ public class ReferenceVariantsHelper( public fun getReferenceVariants( expression: JetSimpleNameExpression, kindFilter: DescriptorKindFilter, - shouldCastToRuntimeType: Boolean, + useRuntimeReceiverType: Boolean, nameFilter: (Name) -> Boolean ): Collection { - return getReferenceVariantsNoVisibilityFilter(expression, kindFilter, shouldCastToRuntimeType, nameFilter).filter(visibilityFilter) + return getReferenceVariantsNoVisibilityFilter(expression, kindFilter, useRuntimeReceiverType, nameFilter).filter(visibilityFilter) } private fun getReferenceVariantsNoVisibilityFilter( expression: JetSimpleNameExpression, kindFilter: DescriptorKindFilter, - shouldCastToRuntimeType: Boolean, + useRuntimeReceiverType: Boolean, nameFilter: (Name) -> Boolean ): Collection { val parent = expression.getParent() @@ -92,7 +92,7 @@ public class ReferenceVariantsHelper( qualifier.scope.getDescriptorsFiltered(kindFilter exclude DescriptorKindExclude.Extensions, nameFilter).filterTo(descriptors) { callType.canCall(it) } } - val expressionType = if (shouldCastToRuntimeType) + val expressionType = if (useRuntimeReceiverType) getQualifierRuntimeType(receiverExpression) else context[BindingContext.EXPRESSION_TYPE, receiverExpression] diff --git a/idea/src/org/jetbrains/kotlin/idea/completion/CompletionSession.kt b/idea/src/org/jetbrains/kotlin/idea/completion/CompletionSession.kt index 1bc4a17b596..343a502c74f 100644 --- a/idea/src/org/jetbrains/kotlin/idea/completion/CompletionSession.kt +++ b/idea/src/org/jetbrains/kotlin/idea/completion/CompletionSession.kt @@ -169,9 +169,9 @@ abstract class CompletionSessionBase(protected val configuration: CompletionSess // set is used only for completion in code fragments private var alreadyAddedDescriptors: Collection by Delegates.notNull() - protected fun getReferenceVariants(kindFilter: DescriptorKindFilter, shouldCastToRuntimeType: Boolean): Collection { - val descriptors = referenceVariantsHelper!!.getReferenceVariants(reference!!.expression, kindFilter, shouldCastToRuntimeType, prefixMatcher.asNameFilter()) - if (!shouldCastToRuntimeType) { + protected fun getReferenceVariants(kindFilter: DescriptorKindFilter, runtimeReceiverType: Boolean): Collection { + val descriptors = referenceVariantsHelper!!.getReferenceVariants(reference!!.expression, kindFilter, runtimeReceiverType, prefixMatcher.asNameFilter()) + if (!runtimeReceiverType) { if (position.getContainingFile() is JetCodeFragment) { alreadyAddedDescriptors = descriptors } @@ -277,7 +277,7 @@ class BasicCompletionSession(configuration: CompletionSessionConfiguration, } if (completionKind != CompletionKind.KEYWORDS_ONLY) { - addReferenceVariants(kindFilter, shouldCastToRuntimeType = false) + addReferenceVariants(kindFilter, runtimeReceiverType = false) } val ampIndex = prefix.indexOf("@") @@ -319,7 +319,7 @@ class BasicCompletionSession(configuration: CompletionSessionConfiguration, if (position.getContainingFile() is JetCodeFragment) { flushToResultSet() - addReferenceVariants(kindFilter, shouldCastToRuntimeType = true) + addReferenceVariants(kindFilter, runtimeReceiverType = true) } } } @@ -355,11 +355,11 @@ class BasicCompletionSession(configuration: CompletionSessionConfiguration, } } - private fun addReferenceVariants(kindFilter: DescriptorKindFilter, shouldCastToRuntimeType: Boolean) { + private fun addReferenceVariants(kindFilter: DescriptorKindFilter, runtimeReceiverType: Boolean) { collector.addDescriptorElements( - getReferenceVariants(kindFilter, shouldCastToRuntimeType), + getReferenceVariants(kindFilter, runtimeReceiverType), suppressAutoInsertion = false, - shouldCastToRuntimeType = shouldCastToRuntimeType) + withReceiverCast = runtimeReceiverType) } } @@ -382,14 +382,14 @@ class SmartCompletionSession(configuration: CompletionSessionConfiguration, para if (reference != null) { val filter = result.declarationFilter if (filter != null) { - getReferenceVariants(DESCRIPTOR_KIND_MASK, false).forEach { collector.addElements(filter(it)) } + getReferenceVariants(DESCRIPTOR_KIND_MASK, runtimeReceiverType = false).forEach { collector.addElements(filter(it)) } flushToResultSet() processNonImported { collector.addElements(filter(it)) } flushToResultSet() if (position.getContainingFile() is JetCodeFragment) { - getReferenceVariants(DESCRIPTOR_KIND_MASK, true).forEach { collector.addElementsWithReceiverCast(filter(it)) } + getReferenceVariants(DESCRIPTOR_KIND_MASK, runtimeReceiverType = true).forEach { collector.addElements(filter(it).map { it.withReceiverCast() }) } flushToResultSet() } } diff --git a/idea/src/org/jetbrains/kotlin/idea/completion/CompletionUtils.kt b/idea/src/org/jetbrains/kotlin/idea/completion/CompletionUtils.kt index 3945de3f53c..4c1ee730cce 100644 --- a/idea/src/org/jetbrains/kotlin/idea/completion/CompletionUtils.kt +++ b/idea/src/org/jetbrains/kotlin/idea/completion/CompletionUtils.kt @@ -80,7 +80,7 @@ fun LookupElement.assignPriority(priority: ItemPriority): LookupElement { fun LookupElement.suppressAutoInsertion() = AutoCompletionPolicy.NEVER_AUTOCOMPLETE.applyPolicy(this) -fun LookupElement.shouldCastReceiver(): LookupElement { +fun LookupElement.withReceiverCast(): LookupElement { return object: LookupElementDecorator(this) { override fun handleInsert(context: InsertionContext) { super.handleInsert(context) @@ -335,4 +335,3 @@ fun breakOrContinueExpressionItems(position: JetElement, breakOrContinue: String } return result } - diff --git a/idea/src/org/jetbrains/kotlin/idea/completion/LookupElementsCollector.kt b/idea/src/org/jetbrains/kotlin/idea/completion/LookupElementsCollector.kt index 569460afdfc..c6b601ce68f 100644 --- a/idea/src/org/jetbrains/kotlin/idea/completion/LookupElementsCollector.kt +++ b/idea/src/org/jetbrains/kotlin/idea/completion/LookupElementsCollector.kt @@ -52,18 +52,18 @@ class LookupElementsCollector( public fun addDescriptorElements(descriptors: Iterable, suppressAutoInsertion: Boolean, // auto-insertion suppression is used for elements that require adding an import - shouldCastToRuntimeType: Boolean = false + withReceiverCast: Boolean = false ) { for (descriptor in descriptors) { - addDescriptorElements(descriptor, suppressAutoInsertion, shouldCastToRuntimeType) + addDescriptorElements(descriptor, suppressAutoInsertion, withReceiverCast) } } - public fun addDescriptorElements(descriptor: DeclarationDescriptor, suppressAutoInsertion: Boolean, shouldCastToRuntimeType: Boolean) { + public fun addDescriptorElements(descriptor: DeclarationDescriptor, suppressAutoInsertion: Boolean, withReceiverCast: Boolean) { run { var lookupElement = lookupElementFactory.createLookupElement(resolutionFacade, descriptor, true) - if (shouldCastToRuntimeType) { - lookupElement = lookupElement.shouldCastReceiver() + if (withReceiverCast) { + lookupElement = lookupElement.withReceiverCast() } if (suppressAutoInsertion) { addElementWithAutoInsertionSuppressed(lookupElement) @@ -140,8 +140,4 @@ class LookupElementsCollector( public fun addElements(elements: Iterable) { elements.forEach { addElement(it) } } - - public fun addElementsWithReceiverCast(elements: Iterable) { - elements.forEach { addElement(it.shouldCastReceiver()) } - } }