Less use of RESOLUTUON_SCOPE from BindingContext

This commit is contained in:
Valentin Kipyatkov
2015-10-24 13:36:01 +03:00
parent 961ffee8ee
commit 44415cf321
5 changed files with 37 additions and 47 deletions
@@ -337,7 +337,9 @@ public class ReferenceVariantsHelper(
expression: KtSimpleNameExpression,
nameFilter: (Name) -> Boolean
): Collection<DeclarationDescriptor> {
val resolutionScope = context[BindingContext.RESOLUTION_SCOPE, expression] ?: return listOf()
return resolutionScope.getDescriptorsFiltered(DescriptorKindFilter.PACKAGES, nameFilter).filter(visibilityFilter)
val resolutionScope = context[BindingContext.LEXICAL_SCOPE, expression] ?: return listOf()
return resolutionScope.collectAllFromImportingScopes {
it.getDescriptorsFiltered(DescriptorKindFilter.PACKAGES, nameFilter).filter(visibilityFilter)
}
}
}
@@ -109,7 +109,6 @@ macro.variable.of.type=kotlinVariable()
macro.iterable.variable=kotlinIterableVariable()
macro.suggest.variable.name=kotlinSuggestVariableName()
macro.fun.parameters=functionParameters()
macro.fun.anonymousSuper=anonymousSuper()
change.visibility.modifier=Change visibility modifier
options.kotlin.attribute.descriptor.builtin.annotation=Built-in annotation
@@ -27,25 +27,20 @@ import com.intellij.psi.PsiNamedElement
import com.intellij.psi.util.PsiTreeUtil
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.ClassKind
import org.jetbrains.kotlin.idea.JetBundle
import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.idea.caches.resolve.getResolutionFacade
import org.jetbrains.kotlin.idea.core.getResolutionScope
import org.jetbrains.kotlin.psi.KtExpression
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter
import org.jetbrains.kotlin.utils.addIfNotNull
import java.util.*
import org.jetbrains.kotlin.resolve.scopes.utils.collectAllFromMeAndParent
import org.jetbrains.kotlin.resolve.scopes.utils.getDescriptorsFiltered
class JetAnonymousSuperMacro : Macro() {
override fun getName(): String {
return "anonymousSuper"
}
override fun getPresentableName(): String {
return JetBundle.message("macro.fun.anonymousSuper")
}
override fun getName() = "anonymousSuper"
override fun getPresentableName() = "anonymousSuper()"
override fun calculateResult(params: Array<Expression>, context: ExpressionContext): Result? {
val editor = context.editor
@@ -54,22 +49,18 @@ class JetAnonymousSuperMacro : Macro() {
}
val vars = getSupertypes(params, context)
if (vars == null || vars.size() == 0) return null
return JetPsiElementResult(vars[0])
if (vars == null || vars.size == 0) return null
return JetPsiElementResult(vars.first())
}
override fun calculateLookupItems(params: Array<Expression>, context: ExpressionContext): Array<LookupElement>? {
val vars = getSupertypes(params, context)
if (vars == null || vars.size < 2) return null
val set = LinkedHashSet<LookupElement>()
for (`var` in vars) {
set.add(LookupElementBuilder.create(`var`))
}
return set.toArray<LookupElement>(arrayOfNulls<LookupElement>(set.size))
val superTypes = getSupertypes(params, context)
if (superTypes == null || superTypes.size < 2) return null
return superTypes.map { LookupElementBuilder.create(it) }.toTypedArray()
}
private fun getSupertypes(params: Array<Expression>, context: ExpressionContext): Array<PsiNamedElement>? {
if (params.size() != 0) return null
if (params.size != 0) return null
val project = context.project
PsiDocumentManager.getInstance(project).commitAllDocuments()
@@ -80,19 +71,13 @@ class JetAnonymousSuperMacro : Macro() {
val expression = PsiTreeUtil.getParentOfType(psiFile.findElementAt(context.startOffset), KtExpression::class.java) ?: return null
val bindingContext = expression.analyze(BodyResolveMode.FULL)
val scope = bindingContext.get(BindingContext.RESOLUTION_SCOPE, expression) ?: return null
val resolutionScope = expression.getResolutionScope(bindingContext, expression.getResolutionFacade())
val result = ArrayList<PsiNamedElement>()
for (descriptor in scope.getDescriptors(DescriptorKindFilter.NON_SINGLETON_CLASSIFIERS)) {
if (descriptor !is ClassDescriptor) continue
if (!descriptor.modality.isOverridable) continue
val kind = descriptor.kind
if (kind == ClassKind.INTERFACE || kind == ClassKind.CLASS) {
result.addIfNotNull(DescriptorToSourceUtils.descriptorToDeclaration(descriptor) as PsiNamedElement?)
}
}
return result.toArray<PsiNamedElement>(arrayOfNulls<PsiNamedElement>(result.size))
return resolutionScope
.collectAllFromMeAndParent { it.getDescriptorsFiltered(DescriptorKindFilter.NON_SINGLETON_CLASSIFIERS) }
.filter { it is ClassDescriptor && it.modality.isOverridable && (it.kind == ClassKind.CLASS || it.kind == ClassKind.INTERFACE) }
.map { DescriptorToSourceUtils.descriptorToDeclaration(it) as PsiNamedElement? }
.filterNotNull()
.toTypedArray()
}
}
@@ -23,6 +23,8 @@ import org.jetbrains.kotlin.descriptors.ConstructorDescriptor
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
import org.jetbrains.kotlin.diagnostics.Diagnostic
import org.jetbrains.kotlin.idea.caches.resolve.analyzeFullyAndGetResult
import org.jetbrains.kotlin.idea.caches.resolve.getResolutionFacade
import org.jetbrains.kotlin.idea.core.getResolutionScope
import org.jetbrains.kotlin.idea.core.quickfix.QuickFixUtil
import org.jetbrains.kotlin.idea.quickfix.createFromUsage.callableBuilder.getExpressionForTypeGuess
import org.jetbrains.kotlin.idea.quickfix.createFromUsage.callableBuilder.getTypeParameters
@@ -38,6 +40,7 @@ import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
import org.jetbrains.kotlin.psi.psiUtil.parents
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.scopes.utils.asKtScope
import org.jetbrains.kotlin.resolve.scopes.utils.getClassifier
import org.jetbrains.kotlin.resolve.source.getPsi
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstanceOrNull
@@ -137,12 +140,12 @@ fun KotlinType.hasTypeParametersToAdd(functionDescriptor: FunctionDescriptor, co
val scope =
when (functionDescriptor) {
is ConstructorDescriptor -> {
(functionDescriptor.containingDeclaration as? ClassDescriptorWithResolutionScopes)?.scopeForClassHeaderResolution?.asKtScope()
(functionDescriptor.containingDeclaration as? ClassDescriptorWithResolutionScopes)?.scopeForClassHeaderResolution
}
is FunctionDescriptor -> {
val function = functionDescriptor.source.getPsi() as? KtFunction
function?.let { context[BindingContext.RESOLUTION_SCOPE, it.bodyExpression] }
function?.bodyExpression?.getResolutionScope(context, function!!.getResolutionFacade())
}
else -> null
@@ -27,10 +27,12 @@ import org.jetbrains.kotlin.asJava.toLightMethods
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.descriptors.impl.AnonymousFunctionDescriptor
import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.idea.caches.resolve.getResolutionFacade
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptor
import org.jetbrains.kotlin.idea.codeInsight.DescriptorToSourceUtilsIde
import org.jetbrains.kotlin.idea.core.CollectingNameValidator
import org.jetbrains.kotlin.idea.core.KotlinNameSuggester
import org.jetbrains.kotlin.idea.core.getResolutionScope
import org.jetbrains.kotlin.idea.refactoring.changeSignature.usages.JetCallableDefinitionUsage
import org.jetbrains.kotlin.incremental.components.NoLookupLocation
import org.jetbrains.kotlin.name.Name
@@ -38,10 +40,9 @@ import org.jetbrains.kotlin.psi.KtCallableDeclaration
import org.jetbrains.kotlin.psi.KtClass
import org.jetbrains.kotlin.psi.KtFunction
import org.jetbrains.kotlin.psi.KtNamedDeclaration
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.DescriptorUtils
import java.util.Collections
import java.util.HashSet
import org.jetbrains.kotlin.resolve.scopes.utils.collectAllFromMeAndParent
import java.util.*
public class JetChangeSignatureData(
override val baseDescriptor: CallableDescriptor,
@@ -76,12 +77,12 @@ public class JetChangeSignatureData(
private fun createReceiverInfoIfNeeded(): JetParameterInfo? {
val callable = baseDeclaration as? KtCallableDeclaration ?: return null
val bodyScope = (callable as? KtFunction)?.getBodyExpression()?.let { it.analyze()[BindingContext.RESOLUTION_SCOPE, it] }
val paramNames = baseDescriptor.getValueParameters().map { it.getName().asString() }
val bodyScope = (callable as? KtFunction)?.bodyExpression?.let { it.getResolutionScope(it.analyze(), it.getResolutionFacade()) }
val paramNames = baseDescriptor.valueParameters.map { it.name.asString() }
val validator = bodyScope?.let { bodyScope ->
CollectingNameValidator(paramNames) {
val name = Name.identifier(it)
bodyScope.getLocalVariable(name) == null && bodyScope.getProperties(name, NoLookupLocation.FROM_IDE).isEmpty()
CollectingNameValidator(paramNames) { name ->
val identifier = Name.identifier(name)
bodyScope.collectAllFromMeAndParent { it.getDeclaredVariables(identifier, NoLookupLocation.FROM_IDE) }.isEmpty()
}
} ?: CollectingNameValidator(paramNames)
val receiverType = baseDescriptor.getExtensionReceiverParameter()?.getType() ?: return null