KT-4822 Wrong scope is used for local variable name completion

#KT-4822 Fixed
This commit is contained in:
Valentin Kipyatkov
2015-06-11 22:10:04 +03:00
parent 14ddc9d972
commit b301b22f47
14 changed files with 229 additions and 63 deletions
@@ -75,7 +75,7 @@ public class ReferenceVariantsHelper(
nameFilter: (Name) -> Boolean
): Collection<DeclarationDescriptor> {
val parent = expression.getParent()
val resolutionScope = context.correctedResolutionScope(expression) ?: return listOf()
val resolutionScope = context[BindingContext.RESOLUTION_SCOPE, expression] ?: return listOf()
val containingDeclaration = resolutionScope.getContainingDeclaration()
if (parent is JetImportDirective || parent is JetPackageDirective) {
@@ -156,7 +156,7 @@ public class ShadowedDeclarationsFilter(
}
val calleeExpression = call.getCalleeExpression() ?: return descriptors
var resolutionScope = bindingContext.correctedResolutionScope(calleeExpression) ?: return descriptors
var resolutionScope = bindingContext[BindingContext.RESOLUTION_SCOPE, calleeExpression] ?: return descriptors
if (descriptorsToImport.isNotEmpty()) {
resolutionScope = ChainedScope(resolutionScope.getContainingDeclaration(), "Scope with explicitly imported descriptors",
@@ -16,15 +16,8 @@
package org.jetbrains.kotlin.idea.util
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.descriptors.VariableDescriptor
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.parentsWithSelf
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter
import org.jetbrains.kotlin.resolve.scopes.JetScope
import org.jetbrains.kotlin.utils.addIfNotNull
public fun JetFunctionLiteral.findLabelAndCall(): Pair<Name?, JetCallExpression?> {
val literalParent = (this.getParent() as JetFunctionLiteralExpression).getParent()
@@ -51,34 +44,3 @@ public fun JetFunctionLiteral.findLabelAndCall(): Pair<Name?, JetCallExpression?
}
}
}
// returns corrected resolution scope excluding variable inside its own initializer
// will not be needed after correcting JetScope stored BindingContext (see KT-4822 Wrong scope is used for local variable name completion)
public fun BindingContext.correctedResolutionScope(expression: JetExpression): JetScope? {
val scope = get(BindingContext.RESOLUTION_SCOPE, expression) ?: return null
val variablesToExclude = hashSetOf<VariableDescriptor>()
for (element in expression.parentsWithSelf) {
if (element is JetExpression) {
val declaration = element.getParent() as? JetVariableDeclaration ?: continue
if (element == declaration.getInitializer()) {
variablesToExclude.addIfNotNull(get(BindingContext.VARIABLE, declaration))
}
}
}
if (variablesToExclude.isEmpty()) return scope
return object : JetScope by scope {
override fun getDescriptors(kindFilter: DescriptorKindFilter, nameFilter: (Name) -> Boolean)
= scope.getDescriptors(kindFilter, nameFilter).filter { it !in variablesToExclude }
//TODO: it's not correct!
override fun getLocalVariable(name: Name): VariableDescriptor? {
val variable = scope.getLocalVariable(name) ?: return null
return if (variable in variablesToExclude) null else variable
}
override fun getProperties(name: Name) = scope.getProperties(name).filter { it !in variablesToExclude }
}
}