Moved filtering of excluded symbols to KotlinIndicesHelper
This commit is contained in:
+2
-4
@@ -28,7 +28,6 @@ import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
|
|||||||
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
|
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
|
||||||
import org.jetbrains.kotlin.idea.caches.resolve.ResolutionFacade
|
import org.jetbrains.kotlin.idea.caches.resolve.ResolutionFacade
|
||||||
import org.jetbrains.kotlin.idea.core.KotlinIndicesHelper
|
import org.jetbrains.kotlin.idea.core.KotlinIndicesHelper
|
||||||
import org.jetbrains.kotlin.idea.core.isInExcludedPackage
|
|
||||||
import org.jetbrains.kotlin.idea.project.ProjectStructureUtil
|
import org.jetbrains.kotlin.idea.project.ProjectStructureUtil
|
||||||
import org.jetbrains.kotlin.platform.JavaToKotlinClassMap
|
import org.jetbrains.kotlin.platform.JavaToKotlinClassMap
|
||||||
import org.jetbrains.kotlin.psi.JetFile
|
import org.jetbrains.kotlin.psi.JetFile
|
||||||
@@ -42,12 +41,11 @@ class AllClassesCompletion(private val parameters: CompletionParameters,
|
|||||||
//TODO: this is a temporary hack until we have built-ins in indices
|
//TODO: this is a temporary hack until we have built-ins in indices
|
||||||
val builtIns = JavaToKotlinClassMap.INSTANCE.allKotlinClasses()
|
val builtIns = JavaToKotlinClassMap.INSTANCE.allKotlinClasses()
|
||||||
val filteredBuiltIns = builtIns
|
val filteredBuiltIns = builtIns
|
||||||
.filter { kindFilter(it.getKind()) && prefixMatcher.prefixMatches(it.getName().asString()) && !isInExcludedPackage(it) }
|
.filter { kindFilter(it.getKind()) && prefixMatcher.prefixMatches(it.getName().asString()) }
|
||||||
filteredBuiltIns.forEach { classDescriptorCollector(it) }
|
filteredBuiltIns.forEach { classDescriptorCollector(it) }
|
||||||
|
|
||||||
kotlinIndicesHelper
|
kotlinIndicesHelper
|
||||||
.getClassDescriptors({ prefixMatcher.prefixMatches(it) }, kindFilter)
|
.getKotlinClasses({ prefixMatcher.prefixMatches(it) }, kindFilter)
|
||||||
.filter { !isInExcludedPackage(it) }
|
|
||||||
.forEach { classDescriptorCollector(it) }
|
.forEach { classDescriptorCollector(it) }
|
||||||
|
|
||||||
if (!ProjectStructureUtil.isJsKotlinModule(parameters.getOriginalFile() as JetFile)) {
|
if (!ProjectStructureUtil.isJsKotlinModule(parameters.getOriginalFile() as JetFile)) {
|
||||||
|
|||||||
@@ -165,7 +165,7 @@ abstract class CompletionSession(protected val configuration: CompletionSessionC
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected val indicesHelper: KotlinIndicesHelper
|
protected val indicesHelper: KotlinIndicesHelper
|
||||||
get() = KotlinIndicesHelper(project, resolutionFacade, searchScope, moduleDescriptor, isVisibleFilter)
|
get() = KotlinIndicesHelper(project, resolutionFacade, searchScope, moduleDescriptor, isVisibleFilter, true)
|
||||||
|
|
||||||
protected fun isVisibleDescriptor(descriptor: DeclarationDescriptor): Boolean {
|
protected fun isVisibleDescriptor(descriptor: DeclarationDescriptor): Boolean {
|
||||||
if (descriptor is TypeParameterDescriptor && !isTypeParameterVisible(descriptor)) return false
|
if (descriptor is TypeParameterDescriptor && !isTypeParameterVisible(descriptor)) return false
|
||||||
@@ -247,17 +247,16 @@ abstract class CompletionSession(protected val configuration: CompletionSessionC
|
|||||||
|
|
||||||
protected fun getTopLevelCallables(): Collection<DeclarationDescriptor> {
|
protected fun getTopLevelCallables(): Collection<DeclarationDescriptor> {
|
||||||
val descriptors = indicesHelper.getTopLevelCallables({ prefixMatcher.prefixMatches(it) })
|
val descriptors = indicesHelper.getTopLevelCallables({ prefixMatcher.prefixMatches(it) })
|
||||||
return filterShadowedNonImportedAndExcluded(descriptors, reference!!)
|
return filterShadowedNonImported(descriptors, reference!!)
|
||||||
}
|
}
|
||||||
|
|
||||||
protected fun getTopLevelExtensions(): Collection<CallableDescriptor> {
|
protected fun getTopLevelExtensions(): Collection<CallableDescriptor> {
|
||||||
val descriptors = indicesHelper.getCallableTopLevelExtensions({ prefixMatcher.prefixMatches(it) }, reference!!.expression, bindingContext)
|
val descriptors = indicesHelper.getCallableTopLevelExtensions({ prefixMatcher.prefixMatches(it) }, reference!!.expression, bindingContext)
|
||||||
return filterShadowedNonImportedAndExcluded(descriptors, reference)
|
return filterShadowedNonImported(descriptors, reference)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun filterShadowedNonImportedAndExcluded(descriptors: Collection<CallableDescriptor>, reference: JetSimpleNameReference): Collection<CallableDescriptor> {
|
private fun filterShadowedNonImported(descriptors: Collection<CallableDescriptor>, reference: JetSimpleNameReference): Collection<CallableDescriptor> {
|
||||||
val notExcluded = descriptors.filter { !isInExcludedPackage(it) }
|
return ShadowedDeclarationsFilter(bindingContext, moduleDescriptor, project).filterNonImported(descriptors, referenceVariants, reference.expression)
|
||||||
return ShadowedDeclarationsFilter(bindingContext, moduleDescriptor, project).filterNonImported(notExcluded, referenceVariants, reference.expression)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected fun addAllClasses(kindFilter: (ClassKind) -> Boolean) {
|
protected fun addAllClasses(kindFilter: (ClassKind) -> Boolean) {
|
||||||
|
|||||||
@@ -19,10 +19,13 @@ package org.jetbrains.kotlin.idea.core
|
|||||||
import com.intellij.codeInsight.CodeInsightSettings
|
import com.intellij.codeInsight.CodeInsightSettings
|
||||||
import com.intellij.openapi.project.Project
|
import com.intellij.openapi.project.Project
|
||||||
import com.intellij.psi.search.GlobalSearchScope
|
import com.intellij.psi.search.GlobalSearchScope
|
||||||
|
import com.intellij.psi.search.PsiShortNamesCache
|
||||||
import com.intellij.psi.stubs.StringStubIndexExtension
|
import com.intellij.psi.stubs.StringStubIndexExtension
|
||||||
import org.jetbrains.kotlin.descriptors.*
|
import org.jetbrains.kotlin.descriptors.*
|
||||||
import org.jetbrains.kotlin.idea.caches.resolve.ResolutionFacade
|
import org.jetbrains.kotlin.idea.caches.resolve.ResolutionFacade
|
||||||
|
import org.jetbrains.kotlin.idea.caches.resolve.getResolutionFacade
|
||||||
import org.jetbrains.kotlin.idea.codeInsight.ReferenceVariantsHelper
|
import org.jetbrains.kotlin.idea.codeInsight.ReferenceVariantsHelper
|
||||||
|
import org.jetbrains.kotlin.idea.imports.importableFqName
|
||||||
import org.jetbrains.kotlin.idea.stubindex.*
|
import org.jetbrains.kotlin.idea.stubindex.*
|
||||||
import org.jetbrains.kotlin.idea.util.CallType
|
import org.jetbrains.kotlin.idea.util.CallType
|
||||||
import org.jetbrains.kotlin.idea.util.getImplicitReceiversWithInstance
|
import org.jetbrains.kotlin.idea.util.getImplicitReceiversWithInstance
|
||||||
@@ -49,8 +52,15 @@ public class KotlinIndicesHelper(
|
|||||||
private val resolutionFacade: ResolutionFacade,
|
private val resolutionFacade: ResolutionFacade,
|
||||||
private val scope: GlobalSearchScope,
|
private val scope: GlobalSearchScope,
|
||||||
private val moduleDescriptor: ModuleDescriptor,
|
private val moduleDescriptor: ModuleDescriptor,
|
||||||
private val visibilityFilter: (DeclarationDescriptor) -> Boolean
|
visibilityFilter: (DeclarationDescriptor) -> Boolean,
|
||||||
|
applyExcludeSettings: Boolean
|
||||||
) {
|
) {
|
||||||
|
private val descriptorFilter =
|
||||||
|
if (applyExcludeSettings)
|
||||||
|
{ d -> visibilityFilter(d) && !isExcludedFromAutoImport(d) }
|
||||||
|
else
|
||||||
|
visibilityFilter
|
||||||
|
|
||||||
public fun getTopLevelCallablesByName(name: String): Collection<CallableDescriptor> {
|
public fun getTopLevelCallablesByName(name: String): Collection<CallableDescriptor> {
|
||||||
val declarations = HashSet<JetNamedDeclaration>()
|
val declarations = HashSet<JetNamedDeclaration>()
|
||||||
declarations.addTopLevelNonExtensionCallablesByName(JetFunctionShortNameIndex.getInstance(), name)
|
declarations.addTopLevelNonExtensionCallablesByName(JetFunctionShortNameIndex.getInstance(), name)
|
||||||
@@ -62,7 +72,7 @@ public class KotlinIndicesHelper(
|
|||||||
else {
|
else {
|
||||||
(resolutionFacade.resolveToDescriptor(it) as? CallableDescriptor).singletonOrEmptyList()
|
(resolutionFacade.resolveToDescriptor(it) as? CallableDescriptor).singletonOrEmptyList()
|
||||||
}
|
}
|
||||||
}.filter { it.getExtensionReceiverParameter() == null && visibilityFilter(it) }
|
}.filter { it.getExtensionReceiverParameter() == null && descriptorFilter(it) }
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun MutableSet<JetNamedDeclaration>.addTopLevelNonExtensionCallablesByName(
|
private fun MutableSet<JetNamedDeclaration>.addTopLevelNonExtensionCallablesByName(
|
||||||
@@ -78,7 +88,7 @@ public class KotlinIndicesHelper(
|
|||||||
.map { FqName(it) }
|
.map { FqName(it) }
|
||||||
.filter { nameFilter(it.shortName().asString()) }
|
.filter { nameFilter(it.shortName().asString()) }
|
||||||
.toSet()
|
.toSet()
|
||||||
.flatMap { findTopLevelCallables(it).filter(visibilityFilter) }
|
.flatMap { findTopLevelCallables(it).filter(descriptorFilter) }
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun getCallableTopLevelExtensions(nameFilter: (String) -> Boolean, expression: JetSimpleNameExpression, bindingContext: BindingContext): Collection<CallableDescriptor> {
|
public fun getCallableTopLevelExtensions(nameFilter: (String) -> Boolean, expression: JetSimpleNameExpression, bindingContext: BindingContext): Collection<CallableDescriptor> {
|
||||||
@@ -148,7 +158,7 @@ public class KotlinIndicesHelper(
|
|||||||
val result = LinkedHashSet<CallableDescriptor>()
|
val result = LinkedHashSet<CallableDescriptor>()
|
||||||
|
|
||||||
fun processDescriptor(descriptor: CallableDescriptor) {
|
fun processDescriptor(descriptor: CallableDescriptor) {
|
||||||
if (visibilityFilter(descriptor)) {
|
if (descriptorFilter(descriptor)) {
|
||||||
for ((receiverValue, callType) in receiverValues) {
|
for ((receiverValue, callType) in receiverValues) {
|
||||||
result.addAll(descriptor.substituteExtensionIfCallable(receiverValue, callType, bindingContext, dataFlowInfo, moduleDescriptor))
|
result.addAll(descriptor.substituteExtensionIfCallable(receiverValue, callType, bindingContext, dataFlowInfo, moduleDescriptor))
|
||||||
}
|
}
|
||||||
@@ -172,7 +182,14 @@ public class KotlinIndicesHelper(
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun getClassDescriptors(nameFilter: (String) -> Boolean, kindFilter: (ClassKind) -> Boolean): Collection<ClassDescriptor> {
|
public fun getJvmClassesByName(name: String): Collection<ClassifierDescriptor>
|
||||||
|
= PsiShortNamesCache.getInstance(project).getClassesByName(name, scope)
|
||||||
|
.map { resolutionFacade.psiClassToDescriptor(it) }
|
||||||
|
.filterNotNull()
|
||||||
|
.filter(descriptorFilter)
|
||||||
|
.toSet()
|
||||||
|
|
||||||
|
public fun getKotlinClasses(nameFilter: (String) -> Boolean, kindFilter: (ClassKind) -> Boolean): Collection<ClassDescriptor> {
|
||||||
return JetFullClassNameIndex.getInstance().getAllKeys(project).asSequence()
|
return JetFullClassNameIndex.getInstance().getAllKeys(project).asSequence()
|
||||||
.map { FqName(it) }
|
.map { FqName(it) }
|
||||||
.filter { nameFilter(it.shortName().asString()) }
|
.filter { nameFilter(it.shortName().asString()) }
|
||||||
@@ -190,7 +207,7 @@ public class KotlinIndicesHelper(
|
|||||||
|
|
||||||
// Note: Can't search with psi element as analyzer could be built over temp files
|
// Note: Can't search with psi element as analyzer could be built over temp files
|
||||||
return ResolveSessionUtils.getClassOrObjectDescriptorsByFqName(moduleDescriptor, classFQName) { kindFilter(it.getKind()) }
|
return ResolveSessionUtils.getClassOrObjectDescriptorsByFqName(moduleDescriptor, classFQName) { kindFilter(it.getKind()) }
|
||||||
.filter(visibilityFilter)
|
.filter(descriptorFilter)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun findTopLevelCallables(fqName: FqName): Collection<CallableDescriptor> {
|
private fun findTopLevelCallables(fqName: FqName): Collection<CallableDescriptor> {
|
||||||
@@ -198,11 +215,12 @@ public class KotlinIndicesHelper(
|
|||||||
.filterIsInstance<CallableDescriptor>()
|
.filterIsInstance<CallableDescriptor>()
|
||||||
.filter { it.getExtensionReceiverParameter() == null }
|
.filter { it.getExtensionReceiverParameter() == null }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun isExcludedFromAutoImport(descriptor: DeclarationDescriptor): Boolean {
|
||||||
|
val fqName = descriptor.importableFqName?.asString() ?: return false
|
||||||
|
|
||||||
|
return CodeInsightSettings.getInstance().EXCLUDED_PACKAGES
|
||||||
|
.any { excluded -> fqName == excluded || (fqName.startsWith(excluded) && fqName[excluded.length()] == '.') }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun isInExcludedPackage(descriptor: DeclarationDescriptor): Boolean {
|
|
||||||
val fqName = DescriptorUtils.getFqName(descriptor).asString()
|
|
||||||
|
|
||||||
return CodeInsightSettings.getInstance().EXCLUDED_PACKAGES
|
|
||||||
.any { excluded -> fqName == excluded || fqName.startsWith(excluded + ".") }
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -24,8 +24,6 @@ import com.intellij.openapi.command.CommandProcessor
|
|||||||
import com.intellij.openapi.editor.Editor
|
import com.intellij.openapi.editor.Editor
|
||||||
import com.intellij.openapi.project.Project
|
import com.intellij.openapi.project.Project
|
||||||
import com.intellij.psi.PsiFile
|
import com.intellij.psi.PsiFile
|
||||||
import com.intellij.psi.search.GlobalSearchScope
|
|
||||||
import com.intellij.psi.search.PsiShortNamesCache
|
|
||||||
import com.intellij.psi.util.PsiModificationTracker
|
import com.intellij.psi.util.PsiModificationTracker
|
||||||
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
|
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
|
||||||
import org.jetbrains.kotlin.descriptors.DeclarationDescriptorWithVisibility
|
import org.jetbrains.kotlin.descriptors.DeclarationDescriptorWithVisibility
|
||||||
@@ -37,7 +35,6 @@ import org.jetbrains.kotlin.idea.caches.resolve.ResolutionFacade
|
|||||||
import org.jetbrains.kotlin.idea.caches.resolve.getResolutionFacade
|
import org.jetbrains.kotlin.idea.caches.resolve.getResolutionFacade
|
||||||
import org.jetbrains.kotlin.idea.core.KotlinIndicesHelper
|
import org.jetbrains.kotlin.idea.core.KotlinIndicesHelper
|
||||||
import org.jetbrains.kotlin.idea.core.isVisible
|
import org.jetbrains.kotlin.idea.core.isVisible
|
||||||
import org.jetbrains.kotlin.idea.core.psiClassToDescriptor
|
|
||||||
import org.jetbrains.kotlin.idea.project.ProjectStructureUtil
|
import org.jetbrains.kotlin.idea.project.ProjectStructureUtil
|
||||||
import org.jetbrains.kotlin.psi.JetFile
|
import org.jetbrains.kotlin.psi.JetFile
|
||||||
import org.jetbrains.kotlin.psi.JetPsiUtil
|
import org.jetbrains.kotlin.psi.JetPsiUtil
|
||||||
@@ -48,8 +45,6 @@ import org.jetbrains.kotlin.resolve.DescriptorUtils
|
|||||||
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
|
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
|
||||||
import org.jetbrains.kotlin.utils.CachedValueProperty
|
import org.jetbrains.kotlin.utils.CachedValueProperty
|
||||||
import java.util.ArrayList
|
import java.util.ArrayList
|
||||||
import com.intellij.codeInsight.*
|
|
||||||
import org.jetbrains.kotlin.idea.core.isInExcludedPackage
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check possibility and perform fix for unresolved references.
|
* Check possibility and perform fix for unresolved references.
|
||||||
@@ -140,31 +135,23 @@ public class AutoImportFix(element: JetSimpleNameExpression) : JetHintAction<Jet
|
|||||||
val result = ArrayList<DeclarationDescriptor>()
|
val result = ArrayList<DeclarationDescriptor>()
|
||||||
|
|
||||||
val moduleDescriptor = resolutionFacade.findModuleDescriptor(element)
|
val moduleDescriptor = resolutionFacade.findModuleDescriptor(element)
|
||||||
val indicesHelper = KotlinIndicesHelper(file.getProject(), resolutionFacade, searchScope, moduleDescriptor, ::isVisible)
|
val indicesHelper = KotlinIndicesHelper(file.getProject(), resolutionFacade, searchScope, moduleDescriptor, ::isVisible, true)
|
||||||
|
|
||||||
if (!element.isImportDirectiveExpression() && !JetPsiUtil.isSelectorInQualified(element)) {
|
if (!element.isImportDirectiveExpression() && !JetPsiUtil.isSelectorInQualified(element)) {
|
||||||
if (ProjectStructureUtil.isJsKotlinModule(file)) {
|
if (ProjectStructureUtil.isJsKotlinModule(file)) {
|
||||||
result.addAll(indicesHelper.getClassDescriptors({ it == referenceName }, { true }))
|
result.addAll(indicesHelper.getKotlinClasses({ it == referenceName }, { true }))
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
getClasses(referenceName, file, searchScope).filterTo(result, ::isVisible)
|
result.addAll(indicesHelper.getJvmClassesByName(referenceName))
|
||||||
}
|
}
|
||||||
result.addAll(indicesHelper.getTopLevelCallablesByName(referenceName))
|
result.addAll(indicesHelper.getTopLevelCallablesByName(referenceName))
|
||||||
}
|
}
|
||||||
|
|
||||||
result.addAll(indicesHelper.getCallableTopLevelExtensions({ it == referenceName }, element, bindingContext))
|
result.addAll(indicesHelper.getCallableTopLevelExtensions({ it == referenceName }, element, bindingContext))
|
||||||
|
|
||||||
return result.filter { it -> !isInExcludedPackage(it) }
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getClasses(name: String, file: JetFile, searchScope: GlobalSearchScope): Collection<DeclarationDescriptor>
|
|
||||||
= getShortNamesCache(file).getClassesByName(name, searchScope)
|
|
||||||
.map { element.getResolutionFacade().psiClassToDescriptor(it) }
|
|
||||||
.filterNotNull()
|
|
||||||
.toSet()
|
|
||||||
|
|
||||||
private fun getShortNamesCache(jetFile: JetFile): PsiShortNamesCache = PsiShortNamesCache.getInstance(jetFile.getProject())
|
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
private val ERRORS = setOf(Errors.UNRESOLVED_REFERENCE, Errors.UNRESOLVED_REFERENCE_WRONG_RECEIVER)
|
private val ERRORS = setOf(Errors.UNRESOLVED_REFERENCE, Errors.UNRESOLVED_REFERENCE_WRONG_RECEIVER)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user