DeprecatedSymbolUsageFix: optimization to not search descriptors to import twice

This commit is contained in:
Valentin Kipyatkov
2015-05-22 22:52:19 +03:00
parent ee681cbeed
commit ba3d5f944e
2 changed files with 8 additions and 13 deletions
@@ -221,11 +221,7 @@ public abstract class DeprecatedSymbolUsageFixBase(
//TODO: drop import of old function (if not needed anymore)? //TODO: drop import of old function (if not needed anymore)?
val file = result.getContainingJetFile() val file = result.getContainingJetFile()
for (importFqName in replacement.imports) { replacement.descriptorsToImport.forEach { ImportInsertHelper.getInstance(project).importDescriptor(file, it) }
val descriptors = file.getResolutionFacade().resolveImportReference(file, importFqName)
val descriptorToImport = descriptors.firstOrNull() ?: continue
ImportInsertHelper.getInstance(project).importDescriptor(file, descriptorToImport)
}
result = postProcessInsertedExpression(result, wrapper.addedStatements) result = postProcessInsertedExpression(result, wrapper.addedStatements)
@@ -56,7 +56,7 @@ data class ReplaceWith(val expression: String, vararg val imports: String)
object ReplaceWithAnnotationAnalyzer { object ReplaceWithAnnotationAnalyzer {
public data class ReplacementExpression( public data class ReplacementExpression(
val expression: JetExpression, val expression: JetExpression,
val imports: Collection<FqName>, val descriptorsToImport: Collection<DeclarationDescriptor>,
val parameterUsages: Map<ValueParameterDescriptor, Collection<JetExpression>> val parameterUsages: Map<ValueParameterDescriptor, Collection<JetExpression>>
) )
@@ -84,14 +84,15 @@ object ReplaceWithAnnotationAnalyzer {
val psiFactory = JetPsiFactory(project) val psiFactory = JetPsiFactory(project)
var expression = psiFactory.createExpression(annotation.expression) var expression = psiFactory.createExpression(annotation.expression)
val importFqNames = annotation.imports val explicitlyImportedSymbols = annotation.imports
.filter { FqNameUnsafe.isValid(it) } .filter { FqNameUnsafe.isValid(it) }
.map { FqNameUnsafe(it) } .map { FqNameUnsafe(it) }
.filter { it.isSafe() } .filter { it.isSafe() }
.mapTo(LinkedHashSet<FqName>()) { it.toSafe() } .mapTo(LinkedHashSet<FqName>()) { it.toSafe() }
.flatMap { resolutionFacade.resolveImportReference(file, it) }
val descriptorsToImport = explicitlyImportedSymbols.toArrayList()
val symbolScope = getResolutionScope(symbolDescriptor) val symbolScope = getResolutionScope(symbolDescriptor)
val explicitlyImportedSymbols = importFqNames.flatMap { resolutionFacade.resolveImportReference(file, it) }
val scope = ChainedScope(symbolDescriptor, "ReplaceWith resolution scope", ExplicitImportsScope(explicitlyImportedSymbols), symbolScope) val scope = ChainedScope(symbolDescriptor, "ReplaceWith resolution scope", ExplicitImportsScope(explicitlyImportedSymbols), symbolScope)
val bindingContext = expression.analyzeInContext(scope) val bindingContext = expression.analyzeInContext(scope)
@@ -103,10 +104,8 @@ object ReplaceWithAnnotationAnalyzer {
expression.forEachDescendantOfType<JetSimpleNameExpression> { expression -> expression.forEachDescendantOfType<JetSimpleNameExpression> { expression ->
val target = bindingContext[BindingContext.REFERENCE_TARGET, expression] ?: return@forEachDescendantOfType val target = bindingContext[BindingContext.REFERENCE_TARGET, expression] ?: return@forEachDescendantOfType
if (target.canBeReferencedViaImport()) { if (target.canBeReferencedViaImport() && (target.isExtension || expression.getReceiverExpression() == null)) {
if (target.isExtension || expression.getReceiverExpression() == null) { descriptorsToImport.addIfNotNull(target)
importFqNames.addIfNotNull(target.importableFqName)
}
} }
if (expression.getReceiverExpression() == null) { if (expression.getReceiverExpression() == null) {
@@ -147,7 +146,7 @@ object ReplaceWithAnnotationAnalyzer {
it.putCopyableUserData(parameterUsageKey, null) it.putCopyableUserData(parameterUsageKey, null)
} }
return ReplacementExpression(expression, importFqNames, parameterUsages) return ReplacementExpression(expression, descriptorsToImport, parameterUsages)
} }
private fun getResolutionScope(descriptor: DeclarationDescriptor): JetScope { private fun getResolutionScope(descriptor: DeclarationDescriptor): JetScope {