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(
mainExpression: KtExpression?,
statementsBefore: List<KtExpression>,
analyze: () -> BindingContext
analyze: () -> BindingContext,
reformat: Boolean
): CodeToInline {
var bindingContext = analyze()
@@ -62,7 +63,7 @@ class CodeToInlineBuilder(
bindingContext = insertExplicitTypeArguments(codeToInline, bindingContext, analyze)
processReferences(codeToInline, bindingContext)
processReferences(codeToInline, bindingContext, reformat)
if (mainExpression != null) {
val functionLiteralExpression = mainExpression.unpackFunctionLiteral(true)
@@ -134,7 +135,7 @@ class CodeToInlineBuilder(
return analyze()
}
private fun processReferences(codeToInline: MutableCodeToInline, bindingContext: BindingContext) {
private fun processReferences(codeToInline: MutableCodeToInline, bindingContext: BindingContext, reformat: Boolean) {
val receiversToAdd = ArrayList<Pair<KtExpression, KtExpression>>()
codeToInline.forEachDescendantOfType<KtSimpleNameExpression> { expression ->
@@ -174,7 +175,8 @@ class CodeToInlineBuilder(
for ((expr, receiverExpression) in receiversToAdd.asReversed()) {
val expressionToReplace = expr.parent as? KtCallExpression ?: expr
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 {
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
}
final override fun invoke(project: Project, editor: Editor?, file: KtFile) {
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)
}
@@ -120,7 +120,13 @@ abstract class DeprecatedSymbolUsageFixBase(
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 bindingContext = resolutionFacade.analyze(element, BodyResolveMode.PARTIAL)
var target = element.mainReference.resolveToDescriptors(bindingContext).singleOrNull() ?: return null
@@ -138,7 +144,7 @@ abstract class DeprecatedSymbolUsageFixBase(
is CallableDescriptor -> {
val resolvedCall = element.getResolvedCall(bindingContext) ?: 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)
}
@@ -171,7 +177,7 @@ abstract class DeprecatedSymbolUsageFixBase(
}
target is ClassDescriptor -> {
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)
}
else -> null
@@ -48,19 +48,21 @@ object ReplaceWithAnnotationAnalyzer {
fun analyzeCallableReplacement(
annotation: ReplaceWith,
symbolDescriptor: CallableDescriptor,
resolutionFacade: ResolutionFacade
resolutionFacade: ResolutionFacade,
reformat: Boolean
): CodeToInline? {
val originalDescriptor = (if (symbolDescriptor is CallableMemberDescriptor)
DescriptorUtils.unwrapFakeOverride(symbolDescriptor)
else
symbolDescriptor).original
return analyzeOriginal(annotation, originalDescriptor, resolutionFacade)
return analyzeOriginal(annotation, originalDescriptor, resolutionFacade, reformat)
}
private fun analyzeOriginal(
annotation: ReplaceWith,
symbolDescriptor: CallableDescriptor,
resolutionFacade: ResolutionFacade
resolutionFacade: ResolutionFacade,
reformat: Boolean
): CodeToInline? {
val psiFactory = KtPsiFactory(resolutionFacade.project)
val expression = try {
@@ -80,7 +82,8 @@ object ReplaceWithAnnotationAnalyzer {
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(
@@ -157,9 +157,9 @@ internal fun buildCodeToInline(
}
return builder.prepareCodeToInline(lastReturn?.returnedExpression,
statements.dropLast(returnStatements.size), ::analyzeBodyCopy)
statements.dropLast(returnStatements.size), ::analyzeBodyCopy, reformat = true)
}
else {
return builder.prepareCodeToInline(bodyCopy, emptyList(), ::analyzeBodyCopy)
return builder.prepareCodeToInline(bodyCopy, emptyList(), ::analyzeBodyCopy, reformat = true)
}
}