diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/collections/SimplifyCallChainFix.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/collections/SimplifyCallChainFix.kt index 1d59afb5d46..1530f188674 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/collections/SimplifyCallChainFix.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/collections/SimplifyCallChainFix.kt @@ -20,8 +20,11 @@ import com.intellij.codeInsight.actions.OptimizeImportsProcessor import com.intellij.codeInspection.LocalQuickFix import com.intellij.codeInspection.ProblemDescriptor import com.intellij.openapi.project.Project +import com.intellij.psi.codeStyle.CodeStyleManager import org.jetbrains.kotlin.idea.core.ShortenReferences +import org.jetbrains.kotlin.idea.core.moveFunctionLiteralOutsideParentheses import org.jetbrains.kotlin.idea.core.replaced +import org.jetbrains.kotlin.idea.intentions.callExpression import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.psi.psiUtil.PsiChildRange @@ -47,8 +50,8 @@ class SimplifyCallChainFix( else -> "" } - val receiverExpressionOrEmptyString: Any = - if (!removeReceiverOfFirstCall && firstExpression is KtQualifiedExpression) firstExpression.receiverExpression else "" + val receiverExpressionOrEmptyString = + if (!removeReceiverOfFirstCall && firstExpression is KtQualifiedExpression) firstExpression.receiverExpression.text else "" val firstCallExpression = AbstractCallChainChecker.getCallExpression(firstExpression) ?: return factory.modifyArguments(firstCallExpression) @@ -63,45 +66,35 @@ class SimplifyCallChainFix( } val lambdaExpression = firstCallExpression.lambdaArguments.singleOrNull()?.getLambdaExpression() + val additionalArgument = conversion.additionalArgument + val secondCallHasArguments = secondCallArgumentList?.arguments?.isNotEmpty() == true + val firstCallHasArguments = firstCallArgumentList?.arguments?.isNotEmpty() == true val argumentsText = listOfNotNull( - secondCallArgumentList.takeIf { it?.arguments?.isNotEmpty() == true }, - firstCallArgumentList.takeIf { it?.arguments?.isNotEmpty() == true } - ).let { - val additionalArgument = conversion.additionalArgument - when { - it.isNotEmpty() -> it.joinToString( - separator = ", ", - prefix = "(", - postfix = ")" - ) { callArgumentList -> - callArgumentList.getTextInsideParentheses() - } - additionalArgument != null -> "($additionalArgument)" - else -> "" - } - } + secondCallArgumentList.takeIf { secondCallHasArguments }?.getTextInsideParentheses(), + firstCallArgumentList.takeIf { firstCallHasArguments }?.getTextInsideParentheses(), + additionalArgument.takeIf { !firstCallHasArguments && !secondCallHasArguments }, + lambdaExpression?.text + ).joinToString(separator = ",") val newCallText = conversion.replacement - val newQualifiedExpression = if (lambdaExpression != null) factory.createExpressionByPattern( - "$0$1$2 $3 $4", - receiverExpressionOrEmptyString, - operationSign, - newCallText, - argumentsText, - lambdaExpression.text - ) - else factory.createExpressionByPattern( - "$0$1$2 $3", - receiverExpressionOrEmptyString, - operationSign, - newCallText, - argumentsText + val newQualifiedOrCallExpression = factory.createExpression( + "$receiverExpressionOrEmptyString$operationSign$newCallText($argumentsText)" ) + if (lambdaExpression != null) { + val callExpression = when (newQualifiedOrCallExpression) { + is KtQualifiedExpression -> newQualifiedOrCallExpression.callExpression + is KtCallExpression -> newQualifiedOrCallExpression + else -> null + } + callExpression?.moveFunctionLiteralOutsideParentheses() + } + val project = qualifiedExpression.project val file = qualifiedExpression.containingKtFile - val result = qualifiedExpression.replaced(newQualifiedExpression) - ShortenReferences.DEFAULT.process(result) + val result = qualifiedExpression.replaced(newQualifiedOrCallExpression) + val reformatted = CodeStyleManager.getInstance(project).reformat(result) + ShortenReferences.DEFAULT.process(reformatted as KtElement) if (runOptimizeImports) { OptimizeImportsProcessor(project, file).run() } diff --git a/idea/testData/inspectionsLocal/collections/simplifiableCallChain/joinToStringViaBuilder.kt.after b/idea/testData/inspectionsLocal/collections/simplifiableCallChain/joinToStringViaBuilder.kt.after index 3611c4d5858..eab454a296c 100644 --- a/idea/testData/inspectionsLocal/collections/simplifiableCallChain/joinToStringViaBuilder.kt.after +++ b/idea/testData/inspectionsLocal/collections/simplifiableCallChain/joinToStringViaBuilder.kt.after @@ -1,7 +1,7 @@ // WITH_RUNTIME val x = listOf(1, 2, 3).joinToString(prefix = "= ", separator = " + ") { - val sb = StringBuilder() - sb.append(it).append(" + ").append(it) - sb +val sb = StringBuilder() +sb.append(it).append(" + ").append(it) +sb } \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/coroutines/deferredIsResult/complex.kt.after b/idea/testData/inspectionsLocal/coroutines/deferredIsResult/complex.kt.after index 02463a7473b..efcc3ae0efb 100644 --- a/idea/testData/inspectionsLocal/coroutines/deferredIsResult/complex.kt.after +++ b/idea/testData/inspectionsLocal/coroutines/deferredIsResult/complex.kt.after @@ -8,9 +8,9 @@ suspend fun myFunction(context: CoroutineContext, switch: Int): Int { with (GlobalScope) { when (switch) { 0 -> return withContext(Dispatchers.Default) { - val x = 123 - x * x - } + val x = 123 + x * x + } 1 -> return withContext(context) { -1 } else -> return withContext(Dispatchers.Default) { 9 } }