From 99a2add0e846ab3421183149ed349d2b54ac1b46 Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Thu, 6 Sep 2018 15:36:31 +0300 Subject: [PATCH] SimplifyCallChainFix: build new argument list more accurately Related to KT-23691 --- .../collections/SimplifyCallChainFix.kt | 27 ++++++++++++------- 1 file changed, 18 insertions(+), 9 deletions(-) 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 fbe619877b3..8b3d4c49eb7 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/collections/SimplifyCallChainFix.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/collections/SimplifyCallChainFix.kt @@ -23,6 +23,7 @@ import org.jetbrains.kotlin.idea.core.ShortenReferences import org.jetbrains.kotlin.idea.core.replaced import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.psi.* +import org.jetbrains.kotlin.psi.psiUtil.PsiChildRange class SimplifyCallChainFix(private val newCallText: String) : LocalQuickFix { private val shortenedText = newCallText.split("(").joinToString(separator = "(") { @@ -58,19 +59,27 @@ class SimplifyCallChainFix(private val newCallText: String) : LocalQuickFix { } } val firstCallArgumentList = firstCallExpression.valueArgumentList - val firstCallArguments = firstCallArgumentList?.arguments val secondCallArgumentList = secondCallExpression.valueArgumentList - val secondCallArguments = secondCallArgumentList?.arguments - val argumentsText = when { - secondCallArguments?.isNotEmpty() == true && firstCallArguments?.isNotEmpty() == true -> { - "${secondCallArgumentList.text.removeSuffix(")")}, ${firstCallArgumentList.text.removePrefix("(")}" - } - secondCallArguments?.isNotEmpty() == true -> secondCallArgumentList.text - firstCallArguments?.isNotEmpty() == true -> firstCallArgumentList.text - else -> "" + fun KtValueArgumentList.getTextInsideParentheses(): String { + val range = PsiChildRange(leftParenthesis?.nextSibling ?: firstChild, rightParenthesis?.prevSibling ?: lastChild) + return range.joinToString(separator = "") { it.text } } + val lambdaExpression = firstCallExpression.lambdaArguments.singleOrNull()?.getLambdaExpression() + val argumentsText = listOfNotNull( + secondCallArgumentList.takeIf { it?.arguments?.isNotEmpty() == true }, + firstCallArgumentList.takeIf { it?.arguments?.isNotEmpty() == true } + ).let { + if (it.isEmpty()) "" + else it.joinToString( + separator = ", ", + prefix = "(", + postfix = ")" + ) { callArgumentList -> + callArgumentList.getTextInsideParentheses() + } + } val newQualifiedExpression = if (lambdaExpression != null) factory.createExpressionByPattern( "$0$1$2 $3 $4",