Add 'reformat' flag to DeprecatedSymbolUsageFixBase

This commit is contained in:
Dmitry Jemerov
2017-12-18 13:35:26 +01:00
parent 48021ce5a1
commit 18cb9593c8
4 changed files with 26 additions and 15 deletions
@@ -54,7 +54,8 @@ class CodeToInlineBuilder(
fun prepareCodeToInline( fun prepareCodeToInline(
mainExpression: KtExpression?, mainExpression: KtExpression?,
statementsBefore: List<KtExpression>, statementsBefore: List<KtExpression>,
analyze: () -> BindingContext analyze: () -> BindingContext,
reformat: Boolean
): CodeToInline { ): CodeToInline {
var bindingContext = analyze() var bindingContext = analyze()
@@ -62,7 +63,7 @@ class CodeToInlineBuilder(
bindingContext = insertExplicitTypeArguments(codeToInline, bindingContext, analyze) bindingContext = insertExplicitTypeArguments(codeToInline, bindingContext, analyze)
processReferences(codeToInline, bindingContext) processReferences(codeToInline, bindingContext, reformat)
if (mainExpression != null) { if (mainExpression != null) {
val functionLiteralExpression = mainExpression.unpackFunctionLiteral(true) val functionLiteralExpression = mainExpression.unpackFunctionLiteral(true)
@@ -134,7 +135,7 @@ class CodeToInlineBuilder(
return analyze() return analyze()
} }
private fun processReferences(codeToInline: MutableCodeToInline, bindingContext: BindingContext) { private fun processReferences(codeToInline: MutableCodeToInline, bindingContext: BindingContext, reformat: Boolean) {
val receiversToAdd = ArrayList<Pair<KtExpression, KtExpression>>() val receiversToAdd = ArrayList<Pair<KtExpression, KtExpression>>()
codeToInline.forEachDescendantOfType<KtSimpleNameExpression> { expression -> codeToInline.forEachDescendantOfType<KtSimpleNameExpression> { expression ->
@@ -174,7 +175,8 @@ class CodeToInlineBuilder(
for ((expr, receiverExpression) in receiversToAdd.asReversed()) { for ((expr, receiverExpression) in receiversToAdd.asReversed()) {
val expressionToReplace = expr.parent as? KtCallExpression ?: expr val expressionToReplace = expr.parent as? KtCallExpression ?: expr
codeToInline.replaceExpression(expressionToReplace, codeToInline.replaceExpression(expressionToReplace,
psiFactory.createExpressionByPattern("$0.$1", receiverExpression, expressionToReplace)) psiFactory.createExpressionByPattern("$0.$1", receiverExpression, expressionToReplace,
reformat = reformat))
} }
} }
} }
@@ -59,13 +59,13 @@ abstract class DeprecatedSymbolUsageFixBase(
override fun isAvailable(project: Project, editor: Editor?, file: KtFile): Boolean { override fun isAvailable(project: Project, editor: Editor?, file: KtFile): Boolean {
val element = element ?: return false val element = element ?: return false
val strategy = buildUsageReplacementStrategy(element, replaceWith, recheckAnnotation = true) val strategy = buildUsageReplacementStrategy(element, replaceWith, recheckAnnotation = true, reformat = false)
return strategy?.createReplacer(element) != null return strategy?.createReplacer(element) != null
} }
final override fun invoke(project: Project, editor: Editor?, file: KtFile) { final override fun invoke(project: Project, editor: Editor?, file: KtFile) {
val element = element ?: return val element = element ?: return
val strategy = buildUsageReplacementStrategy(element, replaceWith, recheckAnnotation = false, editor = editor) ?: return val strategy = buildUsageReplacementStrategy(element, replaceWith, recheckAnnotation = false, reformat = true, editor = editor) ?: return
invoke(strategy, project, editor) invoke(strategy, project, editor)
} }
@@ -120,7 +120,13 @@ abstract class DeprecatedSymbolUsageFixBase(
return Data(nameExpression, replacement, descriptor) return Data(nameExpression, replacement, descriptor)
} }
private fun buildUsageReplacementStrategy(element: KtSimpleNameExpression, replaceWith: ReplaceWith, recheckAnnotation: Boolean, editor: Editor? = null): UsageReplacementStrategy? { private fun buildUsageReplacementStrategy(
element: KtSimpleNameExpression,
replaceWith: ReplaceWith,
recheckAnnotation: Boolean,
reformat: Boolean,
editor: Editor? = null
): UsageReplacementStrategy? {
val resolutionFacade = element.getResolutionFacade() val resolutionFacade = element.getResolutionFacade()
val bindingContext = resolutionFacade.analyze(element, BodyResolveMode.PARTIAL) val bindingContext = resolutionFacade.analyze(element, BodyResolveMode.PARTIAL)
var target = element.mainReference.resolveToDescriptors(bindingContext).singleOrNull() ?: return null var target = element.mainReference.resolveToDescriptors(bindingContext).singleOrNull() ?: return null
@@ -138,7 +144,7 @@ abstract class DeprecatedSymbolUsageFixBase(
is CallableDescriptor -> { is CallableDescriptor -> {
val resolvedCall = element.getResolvedCall(bindingContext) ?: return null val resolvedCall = element.getResolvedCall(bindingContext) ?: return null
if (!resolvedCall.isReallySuccess()) return null if (!resolvedCall.isReallySuccess()) return null
val replacement = ReplaceWithAnnotationAnalyzer.analyzeCallableReplacement(replaceWith, target, resolutionFacade) ?: return null val replacement = ReplaceWithAnnotationAnalyzer.analyzeCallableReplacement(replaceWith, target, resolutionFacade, reformat) ?: return null
return CallableUsageReplacementStrategy(replacement, inlineSetter = false) return CallableUsageReplacementStrategy(replacement, inlineSetter = false)
} }
@@ -171,7 +177,7 @@ abstract class DeprecatedSymbolUsageFixBase(
} }
target is ClassDescriptor -> { target is ClassDescriptor -> {
val constructor = target.unsubstitutedPrimaryConstructor ?: return null val constructor = target.unsubstitutedPrimaryConstructor ?: return null
val replacementExpression = ReplaceWithAnnotationAnalyzer.analyzeCallableReplacement(replaceWith, constructor, resolutionFacade) ?: return null val replacementExpression = ReplaceWithAnnotationAnalyzer.analyzeCallableReplacement(replaceWith, constructor, resolutionFacade, reformat) ?: return null
ClassUsageReplacementStrategy(null, replacementExpression, element.project) ClassUsageReplacementStrategy(null, replacementExpression, element.project)
} }
else -> null else -> null
@@ -48,19 +48,21 @@ object ReplaceWithAnnotationAnalyzer {
fun analyzeCallableReplacement( fun analyzeCallableReplacement(
annotation: ReplaceWith, annotation: ReplaceWith,
symbolDescriptor: CallableDescriptor, symbolDescriptor: CallableDescriptor,
resolutionFacade: ResolutionFacade resolutionFacade: ResolutionFacade,
reformat: Boolean
): CodeToInline? { ): CodeToInline? {
val originalDescriptor = (if (symbolDescriptor is CallableMemberDescriptor) val originalDescriptor = (if (symbolDescriptor is CallableMemberDescriptor)
DescriptorUtils.unwrapFakeOverride(symbolDescriptor) DescriptorUtils.unwrapFakeOverride(symbolDescriptor)
else else
symbolDescriptor).original symbolDescriptor).original
return analyzeOriginal(annotation, originalDescriptor, resolutionFacade) return analyzeOriginal(annotation, originalDescriptor, resolutionFacade, reformat)
} }
private fun analyzeOriginal( private fun analyzeOriginal(
annotation: ReplaceWith, annotation: ReplaceWith,
symbolDescriptor: CallableDescriptor, symbolDescriptor: CallableDescriptor,
resolutionFacade: ResolutionFacade resolutionFacade: ResolutionFacade,
reformat: Boolean
): CodeToInline? { ): CodeToInline? {
val psiFactory = KtPsiFactory(resolutionFacade.project) val psiFactory = KtPsiFactory(resolutionFacade.project)
val expression = try { val expression = try {
@@ -80,7 +82,8 @@ object ReplaceWithAnnotationAnalyzer {
fun analyzeExpression() = expression.analyzeInContext(scope, expressionTypingServices = expressionTypingServices) fun analyzeExpression() = expression.analyzeInContext(scope, expressionTypingServices = expressionTypingServices)
return CodeToInlineBuilder(symbolDescriptor, resolutionFacade).prepareCodeToInline(expression, emptyList(), ::analyzeExpression) return CodeToInlineBuilder(symbolDescriptor, resolutionFacade)
.prepareCodeToInline(expression, emptyList(), ::analyzeExpression, reformat)
} }
fun analyzeClassifierReplacement( fun analyzeClassifierReplacement(
@@ -157,9 +157,9 @@ internal fun buildCodeToInline(
} }
return builder.prepareCodeToInline(lastReturn?.returnedExpression, return builder.prepareCodeToInline(lastReturn?.returnedExpression,
statements.dropLast(returnStatements.size), ::analyzeBodyCopy) statements.dropLast(returnStatements.size), ::analyzeBodyCopy, reformat = true)
} }
else { else {
return builder.prepareCodeToInline(bodyCopy, emptyList(), ::analyzeBodyCopy) return builder.prepareCodeToInline(bodyCopy, emptyList(), ::analyzeBodyCopy, reformat = true)
} }
} }