diff --git a/idea/src/org/jetbrains/kotlin/idea/codeInliner/CodeToInlineBuilder.kt b/idea/src/org/jetbrains/kotlin/idea/codeInliner/CodeToInlineBuilder.kt index 43447728367..bc808c06ff1 100644 --- a/idea/src/org/jetbrains/kotlin/idea/codeInliner/CodeToInlineBuilder.kt +++ b/idea/src/org/jetbrains/kotlin/idea/codeInliner/CodeToInlineBuilder.kt @@ -54,7 +54,8 @@ class CodeToInlineBuilder( fun prepareCodeToInline( mainExpression: KtExpression?, statementsBefore: List, - 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>() codeToInline.forEachDescendantOfType { 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)) } } } \ No newline at end of file diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/replaceWith/DeprecatedSymbolUsageFixBase.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/replaceWith/DeprecatedSymbolUsageFixBase.kt index 219853fe6be..d312b484554 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/replaceWith/DeprecatedSymbolUsageFixBase.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/replaceWith/DeprecatedSymbolUsageFixBase.kt @@ -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 diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/replaceWith/ReplaceWithAnnotationAnalyzer.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/replaceWith/ReplaceWithAnnotationAnalyzer.kt index 2119d5883e2..e7860044195 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/replaceWith/ReplaceWithAnnotationAnalyzer.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/replaceWith/ReplaceWithAnnotationAnalyzer.kt @@ -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( diff --git a/idea/src/org/jetbrains/kotlin/idea/refactoring/inline/inlineUtils.kt b/idea/src/org/jetbrains/kotlin/idea/refactoring/inline/inlineUtils.kt index f4e61eb5b9d..ed2af3af2f9 100644 --- a/idea/src/org/jetbrains/kotlin/idea/refactoring/inline/inlineUtils.kt +++ b/idea/src/org/jetbrains/kotlin/idea/refactoring/inline/inlineUtils.kt @@ -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) } }