[groovy] Avoid resolving untyped parameters in the light version of extended inference

GitOrigin-RevId: 325d70ba8ad2c8dc026888395c9becaac97627f9
This commit is contained in:
Konstantin.Nisht
2020-05-11 16:53:16 +00:00
committed by intellij-monorepo-bot
parent e7e42a5f65
commit 376b4ffb49
@@ -10,7 +10,6 @@ import com.intellij.psi.search.searches.ReferencesSearch
import com.intellij.psi.util.parentOfType
import com.intellij.psi.util.parentOfTypes
import com.intellij.util.Processor
import com.intellij.util.containers.map2Array
import org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement
import org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor
import org.jetbrains.plugins.groovy.lang.psi.api.GrFunctionalExpression
@@ -19,7 +18,6 @@ import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpres
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString
import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod
@@ -45,7 +43,8 @@ private fun SearchRequestCollector.adoptSearchRequest(request: PsiSearchRequest,
private class ScopeFilteringRequestProcessor(private val anchorElement: GrMethod,
private val delegateProcessor: RequestResultProcessor) : RequestResultProcessor() {
private class CollisionFinder(vararg val bannedIdentifiers: String) : GroovyRecursiveElementVisitor() {
private class CollisionFinder(val bannedIdentifiers: List<String>,
val bannedElements: List<PsiElement>) : GroovyRecursiveElementVisitor() {
var foundCollision = false
override fun visitElement(element: GroovyPsiElement) {
@@ -53,7 +52,8 @@ private class ScopeFilteringRequestProcessor(private val anchorElement: GrMethod
}
override fun visitReferenceExpression(referenceExpression: GrReferenceExpression) {
if (referenceExpression.referenceName in bannedIdentifiers) {
if (referenceExpression.referenceName in bannedIdentifiers ||
referenceExpression.staticReference.resolve() in bannedElements) {
foundCollision = true
}
else {
@@ -71,19 +71,29 @@ private class ScopeFilteringRequestProcessor(private val anchorElement: GrMethod
if (element.findElementAt(offsetInElement)?.parentOfType<GrLiteral>() != null) {
return true
}
val enclosingClosure: GrFunctionalExpression? = element.parentOfType<GrFunctionalExpression>()
val call: GrMethodCall? = enclosingClosure?.parentOfType<GrMethodCall>()
val arguments: List<GrExpression>? = call?.closureArguments?.asList()?.plus(call.expressionArguments.asList())
if (arguments?.contains(enclosingClosure) == true && call.callReference?.methodName == anchorElement.name) {
return true
}
if (checkSelfReferencesInArguments(element)) {
val enclosingMethod: GrMethod? = element.parentOfType<GrMethod>()
val bannedElements: List<PsiElement> = enclosingMethod?.parameters?.filter { it.typeElement == null } ?: emptyList()
val collisionFinder = CollisionFinder(listOf(anchorElement.name), bannedElements)
if (hasSelfReferencesInCaller(element, collisionFinder) || hasSelfReferencesInArguments(element, collisionFinder)) {
return true
}
return delegateProcessor.processTextOccurrence(element, offsetInElement, consumer)
}
fun checkSelfReferencesInArguments(element: PsiElement): Boolean {
fun hasSelfReferencesInCaller(element: PsiElement, collisionFinder: CollisionFinder): Boolean {
val enclosingClosure: GrFunctionalExpression = element.parentOfType<GrFunctionalExpression>() ?: return false
val call: GrMethodCall = enclosingClosure.parentOfType<GrMethodCall>() ?: return false
val arguments: List<GrExpression> = call.closureArguments?.asList()?.plus(call.expressionArguments.asList()) ?: emptyList()
if (enclosingClosure !in arguments) {
return false
}
if (collisionFinder.apply(call::accept).foundCollision) {
return true
}
return hasSelfReferencesInCaller(call, collisionFinder)
}
fun hasSelfReferencesInArguments(element: PsiElement, collisionFinder: CollisionFinder): Boolean {
val expressionWithArguments: GrExpression? = element.parentOfTypes(GrMethodCall::class, GrAssignmentExpression::class) ?: return false
val isCorrectlyPointing = when (expressionWithArguments) {
is GrMethodCall -> expressionWithArguments.invokedExpression === element
@@ -91,9 +101,6 @@ private class ScopeFilteringRequestProcessor(private val anchorElement: GrMethod
else -> return false
}
if (isCorrectlyPointing) {
val enclosingMethod: GrMethod? = expressionWithArguments.parentOfType<GrMethod>()?.takeIf { it.name == anchorElement.name }
val bannedIdentifiers: Array<String> = enclosingMethod?.parameters?.map2Array { it.name } ?: emptyArray()
val collisionFinder = CollisionFinder(anchorElement.name, *bannedIdentifiers)
val arguments = when (expressionWithArguments) {
is GrMethodCall -> expressionWithArguments.expressionArguments
is GrAssignmentExpression -> arrayOf(expressionWithArguments.rValue)