From 63b59662129fc1a11b520d1e06b7d7fb4fe21879 Mon Sep 17 00:00:00 2001 From: Valentin Kipyatkov Date: Fri, 6 Nov 2015 14:19:45 +0300 Subject: [PATCH] Top-level callables are added one by one --- .../idea/util/ShadowedDeclarationsFilter.kt | 28 +++++++++++-------- .../idea/completion/BasicCompletionSession.kt | 5 +++- .../idea/completion/CompletionSession.kt | 24 +++++++++------- .../smart/SmartCompletionSession.kt | 6 ++-- 4 files changed, 38 insertions(+), 25 deletions(-) diff --git a/idea/ide-common/src/org/jetbrains/kotlin/idea/util/ShadowedDeclarationsFilter.kt b/idea/ide-common/src/org/jetbrains/kotlin/idea/util/ShadowedDeclarationsFilter.kt index ce8698f045a..9c5b3e59fd3 100644 --- a/idea/ide-common/src/org/jetbrains/kotlin/idea/util/ShadowedDeclarationsFilter.kt +++ b/idea/ide-common/src/org/jetbrains/kotlin/idea/util/ShadowedDeclarationsFilter.kt @@ -79,23 +79,27 @@ public class ShadowedDeclarationsFilter( .flatMap { group -> filterEqualSignatureGroup(group) } } - public fun filterNonImported( - declarations: Collection, + public fun createNonImportedDeclarationsFilter( importedDeclarations: Collection - ): Collection { + ): (Collection) -> Collection { val importedDeclarationsSet = importedDeclarations.toSet() - val nonImportedDeclarations = declarations.filter { it !in importedDeclarationsSet } - val importedDeclarationsBySignature = importedDeclarationsSet.groupBy { signature(it) } - val notShadowed = HashSet() - // same signature non-imported declarations from different packages do not shadow each other - for ((pair, group) in nonImportedDeclarations.groupBy { signature(it) to packageName(it) }) { - val imported = importedDeclarationsBySignature[pair.first] - val all = if (imported != null) group + imported else group - notShadowed.addAll(filterEqualSignatureGroup(all, descriptorsToImport = group)) + return filter@ { declarations -> + // optimization + if (declarations.size == 1 && importedDeclarationsBySignature[signature(declarations.single())] == null) return@filter declarations + + val nonImportedDeclarations = declarations.filter { it !in importedDeclarationsSet } + + val notShadowed = HashSet() + // same signature non-imported declarations from different packages do not shadow each other + for ((pair, group) in nonImportedDeclarations.groupBy { signature(it) to packageName(it) }) { + val imported = importedDeclarationsBySignature[pair.first] + val all = if (imported != null) group + imported else group + notShadowed.addAll(filterEqualSignatureGroup(all, descriptorsToImport = group)) + } + declarations.filter { it in notShadowed } } - return declarations.filter { it in notShadowed } } private fun signature(descriptor: DeclarationDescriptor): Any { diff --git a/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/BasicCompletionSession.kt b/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/BasicCompletionSession.kt index 234c5205f46..160953e5ae7 100644 --- a/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/BasicCompletionSession.kt +++ b/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/BasicCompletionSession.kt @@ -242,7 +242,10 @@ class BasicCompletionSession( private fun completeNonImported(lookupElementFactory: LookupElementFactory) { if (shouldCompleteTopLevelCallablesFromIndex()) { - collector.addDescriptorElements(getTopLevelCallables(), lookupElementFactory, notImported = true) + processTopLevelCallables { + collector.addDescriptorElements(it, lookupElementFactory, notImported = true) + flushToResultSet() + } } val classKindFilter: ((ClassKind) -> Boolean)? diff --git a/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/CompletionSession.kt b/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/CompletionSession.kt index ee7a58fb77f..3bf8ba67377 100644 --- a/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/CompletionSession.kt +++ b/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/CompletionSession.kt @@ -337,7 +337,9 @@ abstract class CompletionSession( if (shadowedDeclarationsFilter != null) { variants = shadowedDeclarationsFilter.filter(variants) - notImportedExtensions = shadowedDeclarationsFilter.filterNonImported(notImportedExtensions, variants) + notImportedExtensions = shadowedDeclarationsFilter + .createNonImportedDeclarationsFilter(importedDeclarations = variants) + .invoke(notImportedExtensions) } if (configuration.filterOutJavaGettersAndSetters) { @@ -394,10 +396,17 @@ abstract class CompletionSession( return callTypeAndReceiver.receiver == null } - protected fun getTopLevelCallables(): Collection { - val result = LinkedHashSet() - indicesHelper(true).processTopLevelCallables({ prefixMatcher.prefixMatches(it) }) { result.add(it) } - return result.filterShadowedNonImported() + protected fun processTopLevelCallables(processor: (CallableDescriptor) -> Unit) { + val shadowedFilter = ShadowedDeclarationsFilter.create(bindingContext, resolutionFacade, nameExpression!!, callTypeAndReceiver) + ?.createNonImportedDeclarationsFilter(referenceVariants!!.imported) + indicesHelper(true).processTopLevelCallables({ prefixMatcher.prefixMatches(it) }) { + if (shadowedFilter != null) { + shadowedFilter(listOf(it)).singleOrNull()?.let(processor) + } + else { + processor(it) + } + } } protected fun CallTypeAndReceiver<*, *>.shouldCompleteCallableExtensions(): Boolean { @@ -405,11 +414,6 @@ abstract class CompletionSession( && this !is CallTypeAndReceiver.IMPORT_DIRECTIVE } - private fun Collection.filterShadowedNonImported(): Collection { - val filter = ShadowedDeclarationsFilter.create(bindingContext, resolutionFacade, nameExpression!!, callTypeAndReceiver) - return if (filter != null) filter.filterNonImported(this, referenceVariants!!.imported) else this - } - protected fun addClassesFromIndex(kindFilter: (ClassKind) -> Boolean) { AllClassesCompletion(parameters, indicesHelper(true), prefixMatcher, resolutionFacade, kindFilter) .collect( diff --git a/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/smart/SmartCompletionSession.kt b/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/smart/SmartCompletionSession.kt index 295298e6154..bd9c47e0c4a 100644 --- a/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/smart/SmartCompletionSession.kt +++ b/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/smart/SmartCompletionSession.kt @@ -120,8 +120,10 @@ class SmartCompletionSession(configuration: CompletionSessionConfiguration, para } if (shouldCompleteTopLevelCallablesFromIndex()) { - getTopLevelCallables().forEach { collector.addElements(filter(it, lookupElementFactory), notImported = true) } - flushToResultSet() + processTopLevelCallables { + collector.addElements(filter(it, lookupElementFactory), notImported = true) + flushToResultSet() + } } if (position.getContainingFile() is KtCodeFragment) {