[kotlin] replace useScope with PsiSearchHelper.getUseScope apply scope extensions

^KTIJ-20131

GitOrigin-RevId: 365c046387431d01e64311e991b8e4daf17c5859
This commit is contained in:
Dmitry Gridin
2021-11-29 05:05:29 +00:00
committed by intellij-monorepo-bot
parent 931009a706
commit c3eb4cab5e
11 changed files with 43 additions and 31 deletions
@@ -27,6 +27,7 @@ import org.jetbrains.kotlin.idea.search.KotlinSearchUsagesSupport.Companion.forE
import org.jetbrains.kotlin.idea.search.KotlinSearchUsagesSupport.Companion.isOverridable
import org.jetbrains.kotlin.idea.search.allScope
import org.jetbrains.kotlin.idea.search.excludeKotlinSources
import org.jetbrains.kotlin.idea.search.useScope
import org.jetbrains.kotlin.idea.util.application.runReadAction
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.containingClassOrObject
@@ -109,7 +110,7 @@ fun PsiElement.toPossiblyFakeLightMethods(): List<PsiMethod> {
}
fun KtNamedDeclaration.forEachOverridingElement(
scope: SearchScope = runReadAction { useScope },
scope: SearchScope = runReadAction { useScope() },
searchDeeply: Boolean = true,
processor: (superMember: PsiElement, overridingMember: PsiElement) -> Boolean
): Boolean {
@@ -136,7 +137,7 @@ fun KtNamedDeclaration.hasOverridingElement(): Boolean {
}
fun PsiMethod.forEachImplementation(
scope: SearchScope = runReadAction { useScope },
scope: SearchScope = runReadAction { useScope() },
processor: (PsiElement) -> Boolean
): Boolean = forEachOverridingMethod(scope, processor) && FunctionalExpressionSearch.search(
this,
@@ -151,14 +152,15 @@ fun PsiMethod.forEachImplementation(
),
DeprecationLevel.ERROR
)
@JvmName("forEachOverridingMethod")
fun PsiMethod.forEachOverridingMethodCompat(
scope: SearchScope = runReadAction { useScope },
scope: SearchScope = runReadAction { useScope() },
processor: (PsiMethod) -> Boolean
): Boolean = forEachOverridingMethod(scope, processor)
fun PsiClass.forEachDeclaredMemberOverride(processor: (superMember: PsiElement, overridingMember: PsiElement) -> Boolean) {
val scope = runReadAction { useScope }
val scope = runReadAction { useScope() }
if (!providedIsKtFakeLightClass()) {
AllOverridingMethodsSearch.search(this, scope.excludeKotlinSources()).all { processor(it.first, it.second) }
@@ -22,6 +22,7 @@ import org.jetbrains.kotlin.idea.search.KotlinSearchUsagesSupport.Companion.forE
import org.jetbrains.kotlin.idea.search.KotlinSearchUsagesSupport.Companion.isExpectDeclaration
import org.jetbrains.kotlin.idea.search.declarationsSearch.forEachImplementation
import org.jetbrains.kotlin.idea.search.declarationsSearch.toPossiblyFakeLightMethods
import org.jetbrains.kotlin.idea.search.useScope
import org.jetbrains.kotlin.idea.util.application.runReadAction
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.contains
@@ -32,6 +32,7 @@ import org.jetbrains.kotlin.idea.search.excludeFileTypes
import org.jetbrains.kotlin.idea.search.ideaExtensions.KotlinReferencesSearchOptions
import org.jetbrains.kotlin.idea.search.ideaExtensions.KotlinReferencesSearchParameters
import org.jetbrains.kotlin.idea.search.restrictToKotlinSources
import org.jetbrains.kotlin.idea.search.useScope
import org.jetbrains.kotlin.idea.util.application.isUnitTestMode
import org.jetbrains.kotlin.idea.util.application.runReadAction
import org.jetbrains.kotlin.kdoc.psi.impl.KDocName
@@ -311,8 +312,7 @@ class ExpressionsOfTypeProcessor(
throw ProcessCanceledException()
}
val file = psiClass.containingFile
if (file != null) file.useScope else psiClass.useScope
psiClass.containingFile?.useScope() ?: psiClass.useScope()
}
private fun addStaticMemberToProcess(psiMember: PsiMember, scope: SearchScope, processor: ReferenceProcessor) {
@@ -10,6 +10,7 @@ import com.intellij.util.ArrayUtil
import org.jetbrains.kotlin.asJava.unwrapped
import org.jetbrains.kotlin.idea.search.declarationsSearch.HierarchySearchRequest
import org.jetbrains.kotlin.idea.search.declarationsSearch.searchInheritors
import org.jetbrains.kotlin.idea.search.useScope
import org.jetbrains.kotlin.psi.KtCallableDeclaration
import org.jetbrains.kotlin.psi.psiUtil.containingClassOrObject
import org.jetbrains.kotlin.psi.psiUtil.createSmartPointer
@@ -24,7 +25,7 @@ class KotlinOverrideTreeStructure(project: Project, declaration: KtCallableDecla
override fun buildChildren(nodeDescriptor: HierarchyNodeDescriptor): Array<Any> {
val baseElement = baseElement.element ?: return ArrayUtil.EMPTY_OBJECT_ARRAY
val psiElement = nodeDescriptor.psiElement ?: return ArrayUtil.EMPTY_OBJECT_ARRAY
val subclasses = HierarchySearchRequest(psiElement, psiElement.useScope, false).searchInheritors().findAll()
val subclasses = HierarchySearchRequest(psiElement, psiElement.useScope(), false).searchInheritors().findAll()
return subclasses.mapNotNull {
val subclass = it.unwrapped ?: return@mapNotNull null
KotlinOverrideHierarchyNodeDescriptor(nodeDescriptor, subclass, baseElement)
@@ -14,6 +14,7 @@ import org.jetbrains.kotlin.idea.KotlinBundle
import org.jetbrains.kotlin.idea.intentions.ConvertToScopeIntention.ScopeFunction.*
import org.jetbrains.kotlin.idea.references.mainReference
import org.jetbrains.kotlin.idea.search.ideaExtensions.KotlinReferencesSearchParameters
import org.jetbrains.kotlin.idea.search.useScope
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.anyDescendantOfType
@@ -158,7 +159,7 @@ sealed class ConvertToScopeIntention(private val scopeFunction: ScopeFunction) :
}
val searchParameters = KotlinReferencesSearchParameters(
element, element.useScope, ignoreAccessScope = false
element, element.useScope(), ignoreAccessScope = false
)
val range = PsiTreeUtil.getElementsOfRange(firstTarget, lastTarget)
@@ -20,6 +20,7 @@ import org.jetbrains.kotlin.idea.codeInsight.DescriptorToSourceUtilsIde
import org.jetbrains.kotlin.idea.core.util.runSynchronouslyWithProgress
import org.jetbrains.kotlin.idea.search.declarationsSearch.HierarchySearchRequest
import org.jetbrains.kotlin.idea.search.declarationsSearch.searchInheritors
import org.jetbrains.kotlin.idea.search.useScope
import org.jetbrains.kotlin.idea.util.application.runReadAction
import org.jetbrains.kotlin.idea.util.application.runWriteAction
import org.jetbrains.kotlin.incremental.components.NoLookupLocation
@@ -67,7 +68,7 @@ class ChangeSuspendInHierarchyFix(
val name = (baseClass as? PsiNamedElement)?.name ?: return@forEach
progressIndicator.text = KotlinBundle.message("fix.change.progress.looking.inheritors", name)
val classes = listOf(baseClass) + HierarchySearchRequest(baseClass, baseClass.useScope).searchInheritors()
val classes = listOf(baseClass) + HierarchySearchRequest(baseClass, baseClass.useScope()).searchInheritors()
classes.mapNotNullTo(result) {
val subClass = it.unwrapped as? KtClassOrObject ?: return@mapNotNullTo null
val classDescriptor = subClass.unsafeResolveToDescriptor() as ClassDescriptor
@@ -46,6 +46,7 @@ import org.jetbrains.kotlin.idea.search.codeUsageScopeRestrictedToKotlinSources
import org.jetbrains.kotlin.idea.search.ideaExtensions.KotlinReferencesSearchOptions
import org.jetbrains.kotlin.idea.search.ideaExtensions.KotlinReferencesSearchParameters
import org.jetbrains.kotlin.idea.search.usagesSearch.processDelegationCallConstructorUsages
import org.jetbrains.kotlin.idea.search.useScope
import org.jetbrains.kotlin.idea.util.IdeDescriptorRenderers
import org.jetbrains.kotlin.idea.util.getReceiverTargetDescriptor
import org.jetbrains.kotlin.kdoc.psi.impl.KDocName
@@ -150,7 +151,7 @@ class KotlinChangeSignatureUsageProcessor : ChangeSignatureUsageProcessor {
val element = callerUsage.element ?: return
for (ref in ReferencesSearch.search(element, element.useScope)) {
for (ref in ReferencesSearch.search(element, element.useScope())) {
val callElement = ref.element.getParentOfTypeAndBranch<KtCallElement> { calleeExpression } ?: continue
result.add(KotlinCallerCallUsage(callElement))
}
@@ -204,7 +205,7 @@ class KotlinChangeSignatureUsageProcessor : ChangeSignatureUsageProcessor {
private fun findReferences(functionPsi: PsiElement): Set<PsiReference> {
val result = LinkedHashSet<PsiReference>()
val searchScope = functionPsi.useScope
val searchScope = functionPsi.useScope()
val options = KotlinReferencesSearchOptions(
acceptCallableOverrides = true,
acceptOverloads = false,
@@ -283,7 +284,7 @@ class KotlinChangeSignatureUsageProcessor : ChangeSignatureUsageProcessor {
val oldParamName = oldParam.name
if (parameterInfo == newReceiverInfo || (oldParamName != null && oldParamName != parameterInfo.name) || isDataClass && i != parameterInfo.oldIndex) {
for (reference in ReferencesSearch.search(oldParam, oldParam.useScope)) {
for (reference in ReferencesSearch.search(oldParam, oldParam.useScope())) {
val element = reference.element
if (isDataClass &&
@@ -324,7 +325,7 @@ class KotlinChangeSignatureUsageProcessor : ChangeSignatureUsageProcessor {
}
}
functionPsi.processDelegationCallConstructorUsages(functionPsi.useScope) {
functionPsi.processDelegationCallConstructorUsages(functionPsi.useScope()) {
when (it) {
is KtConstructorDelegationCall -> result.add(KotlinConstructorDelegationCallUsage(it, changeInfo))
is KtSuperTypeCallEntry -> result.add(KotlinFunctionCallUsage(it, functionUsageInfo))
@@ -529,7 +530,7 @@ class KotlinChangeSignatureUsageProcessor : ChangeSignatureUsageProcessor {
if (!(oldParamName != null && oldParamName != parameterInfo.name)) continue
for (reference in ReferencesSearch.search(oldParam, oldParam.useScope)) {
for (reference in ReferencesSearch.search(oldParam, oldParam.useScope())) {
val element = reference.element
// Usages in named arguments of the calls usage will be changed when the function call is changed
if (!((element is KtSimpleNameExpression || element is KDocName) && element.parent !is KtValueArgumentName)) continue
@@ -31,6 +31,7 @@ import org.jetbrains.kotlin.idea.caches.resolve.unsafeResolveToDescriptor
import org.jetbrains.kotlin.idea.caches.resolve.util.getJavaMethodDescriptor
import org.jetbrains.kotlin.idea.hierarchy.calls.CalleeReferenceProcessor
import org.jetbrains.kotlin.idea.hierarchy.calls.KotlinCallHierarchyNodeDescriptor
import org.jetbrains.kotlin.idea.search.useScope
import org.jetbrains.kotlin.psi.KtClass
import org.jetbrains.kotlin.psi.KtFunction
import org.jetbrains.kotlin.psi.psiUtil.getElementTextWithContext
@@ -100,8 +101,11 @@ class KotlinMethodNode(
}
}
}
val query = myMethod.getRepresentativeLightMethod()?.let { MethodReferencesSearch.search(it, it.useScope, true) }
?: ReferencesSearch.search(myMethod, myMethod.useScope)
val query = myMethod.getRepresentativeLightMethod()
?.let { MethodReferencesSearch.search(it, it.useScope(), true) }
?: ReferencesSearch.search(myMethod, myMethod.useScope())
query.forEach { processor.process(it) }
return callers.toList()
}
@@ -28,6 +28,7 @@ import org.jetbrains.kotlin.idea.references.resolveToDescriptors
import org.jetbrains.kotlin.idea.resolve.ResolutionFacade
import org.jetbrains.kotlin.idea.search.declarationsSearch.HierarchySearchRequest
import org.jetbrains.kotlin.idea.search.declarationsSearch.searchInheritors
import org.jetbrains.kotlin.idea.search.useScope
import org.jetbrains.kotlin.idea.util.IdeDescriptorRenderers
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.*
@@ -215,7 +216,7 @@ private fun KotlinPullUpData.checkAccidentalOverrides(
if (memberDescriptor is CallableDescriptor && !member.hasModifier(KtTokens.PRIVATE_KEYWORD)) {
val memberDescriptorInTargetClass = memberDescriptor.substitute(sourceToTargetClassSubstitutor)
if (memberDescriptorInTargetClass != null) {
val sequence = HierarchySearchRequest<PsiElement>(targetClass, targetClass.useScope)
val sequence = HierarchySearchRequest<PsiElement>(targetClass, targetClass.useScope())
.searchInheritors()
.asSequence()
.filterNot { it.isSourceOrTarget(this) }
@@ -24,11 +24,11 @@ import org.jetbrains.kotlin.idea.refactoring.getThisLabelName
import org.jetbrains.kotlin.idea.references.mainReference
import org.jetbrains.kotlin.idea.resolve.ResolutionFacade
import org.jetbrains.kotlin.idea.search.and
import org.jetbrains.kotlin.idea.search.ideaExtensions.KotlinAwareReferencesSearchParameters
import org.jetbrains.kotlin.idea.search.ideaExtensions.KotlinReferencesSearchOptions
import org.jetbrains.kotlin.idea.search.ideaExtensions.KotlinReferencesSearchParameters
import org.jetbrains.kotlin.idea.search.isPotentiallyOperator
import org.jetbrains.kotlin.idea.search.restrictToKotlinSources
import org.jetbrains.kotlin.idea.search.useScope
import org.jetbrains.kotlin.idea.util.IdeDescriptorRenderers
import org.jetbrains.kotlin.idea.util.getAllAccessibleFunctions
import org.jetbrains.kotlin.idea.util.getAllAccessibleVariables
@@ -374,18 +374,17 @@ internal fun checkNewNameUsagesRetargeting(
}
val operator = declaration.isOperator()
for (candidateDescriptor in declaration.getResolutionScope().getRelevantDescriptors(declaration, newName)) {
val candidate =
DescriptorToSourceUtilsIde.getAnyDeclaration(declaration.project, candidateDescriptor) as? PsiNamedElement ?: continue
val searchParameters =
KotlinReferencesSearchParameters(
candidate,
scope = candidate.useScope.restrictToKotlinSources() and declaration.useScope,
kotlinOptions = KotlinReferencesSearchOptions(searchForOperatorConventions = operator)
)
val usages = ReferencesSearch.search(searchParameters)
.mapTo(SmartList<UsageInfo>()) { MoveRenameUsageInfo(it, candidate) }
val candidate = DescriptorToSourceUtilsIde.getAnyDeclaration(declaration.project, candidateDescriptor) as? PsiNamedElement
?: continue
val searchParameters = KotlinReferencesSearchParameters(
candidate,
scope = candidate.useScope().restrictToKotlinSources() and declaration.useScope(),
kotlinOptions = KotlinReferencesSearchOptions(searchForOperatorConventions = operator)
)
val usages = ReferencesSearch.search(searchParameters).mapTo(SmartList<UsageInfo>()) { MoveRenameUsageInfo(it, candidate) }
checkUsagesRetargeting(candidate, declaration, currentName, false, listOf(descriptor), usages, newUsages)
usages.filterIsInstanceTo<KtResolvableCollisionUsageInfo, MutableList<UsageInfo>>(newUsages)
}
@@ -34,6 +34,7 @@ import org.jetbrains.kotlin.idea.search.ideaExtensions.KotlinReferencesSearchOpt
import org.jetbrains.kotlin.idea.search.ideaExtensions.KotlinReferencesSearchParameters
import org.jetbrains.kotlin.idea.search.projectScope
import org.jetbrains.kotlin.idea.search.usagesSearch.processDelegationCallConstructorUsages
import org.jetbrains.kotlin.idea.search.useScope
import org.jetbrains.kotlin.idea.util.application.isUnitTestMode
import org.jetbrains.kotlin.idea.util.isExpectDeclaration
import org.jetbrains.kotlin.idea.util.liftToExpected
@@ -79,7 +80,7 @@ class KotlinSafeDeleteProcessor : JavaSafeDeleteProcessor() {
return elementsToSearch.asSequence().flatMap {
val searchParameters = KotlinReferencesSearchParameters(
it,
if (it.hasActualModifier()) it.project.projectScope() else it.useScope,
if (it.hasActualModifier()) it.project.projectScope() else it.useScope(),
kotlinOptions = KotlinReferencesSearchOptions(acceptCallableOverrides = true)
)
ReferencesSearch.search(searchParameters).asSequence()
@@ -236,7 +237,7 @@ class KotlinSafeDeleteProcessor : JavaSafeDeleteProcessor() {
else -> return
}
for (constructor in constructors) {
constructor.processDelegationCallConstructorUsages((constructor as PsiElement).useScope) {
constructor.processDelegationCallConstructorUsages((constructor as PsiElement).useScope()) {
if (!getIgnoranceCondition().value(it)) {
usages.add(SafeDeleteReferenceSimpleDeleteUsageInfo(it, element, false))
}