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 3f1bf43b4bb..fbe619877b3 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/collections/SimplifyCallChainFix.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/collections/SimplifyCallChainFix.kt @@ -21,8 +21,8 @@ import com.intellij.codeInspection.ProblemDescriptor import com.intellij.openapi.project.Project 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.utils.addToStdlib.ifNotEmpty class SimplifyCallChainFix(private val newCallText: String) : LocalQuickFix { private val shortenedText = newCallText.split("(").joinToString(separator = "(") { @@ -49,12 +49,29 @@ class SimplifyCallChainFix(private val newCallText: String) : LocalQuickFix { val firstCallExpression = AbstractCallChainChecker.getCallExpression(firstExpression) ?: return val secondCallExpression = secondQualifiedExpression.selectorExpression as? KtCallExpression ?: return - val lastArgumentPrefix = if (newCallText.startsWith("joinTo")) "transform = " else "" - val arguments = secondCallExpression.valueArgumentList?.arguments.orEmpty().map { it.text } + - firstCallExpression.valueArgumentList?.arguments.orEmpty().map { "$lastArgumentPrefix${it.text}" } + val lastArgumentName = if (newCallText.startsWith("joinTo")) Name.identifier("transform") else null + if (lastArgumentName != null) { + val lastArgument = firstCallExpression.valueArgumentList?.arguments?.singleOrNull() + val argumentExpression = lastArgument?.getArgumentExpression() + if (argumentExpression != null) { + lastArgument.replace(factory.createArgument(argumentExpression, lastArgumentName)) + } + } + 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 -> "" + } val lambdaExpression = firstCallExpression.lambdaArguments.singleOrNull()?.getLambdaExpression() - val argumentsText = arguments.ifNotEmpty { joinToString(prefix = "(", postfix = ")") } ?: "" val newQualifiedExpression = if (lambdaExpression != null) factory.createExpressionByPattern( "$0$1$2 $3 $4", receiverExpression ?: "", diff --git a/idea/testData/inspectionsLocal/collections/simplifiableCallChain/joinToStringWithReferenceAndComment.kt b/idea/testData/inspectionsLocal/collections/simplifiableCallChain/joinToStringWithReferenceAndComment.kt new file mode 100644 index 00000000000..a7f62f0a279 --- /dev/null +++ b/idea/testData/inspectionsLocal/collections/simplifiableCallChain/joinToStringWithReferenceAndComment.kt @@ -0,0 +1,12 @@ +// WITH_RUNTIME + +val x = listOf(1, 2, 3).map( + // comment1 + Int::toString +).joinToString( + // comment2 + prefix = "= ", + // comment3 + separator = " + " + // comment4 +) \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/collections/simplifiableCallChain/joinToStringWithReferenceAndComment.kt.after b/idea/testData/inspectionsLocal/collections/simplifiableCallChain/joinToStringWithReferenceAndComment.kt.after new file mode 100644 index 00000000000..c0127ebc9f3 --- /dev/null +++ b/idea/testData/inspectionsLocal/collections/simplifiableCallChain/joinToStringWithReferenceAndComment.kt.after @@ -0,0 +1,12 @@ +// WITH_RUNTIME + +val x = listOf(1, 2, 3).joinToString( + // comment2 + prefix = "= ", + // comment3 + separator = " + " + // comment4 + , + // comment1 + transform = Int::toString +) \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/collections/simplifiableCallChain/joinToWithComment.kt b/idea/testData/inspectionsLocal/collections/simplifiableCallChain/joinToWithComment.kt new file mode 100644 index 00000000000..30d587a9af1 --- /dev/null +++ b/idea/testData/inspectionsLocal/collections/simplifiableCallChain/joinToWithComment.kt @@ -0,0 +1,12 @@ +// WITH_RUNTIME + +val sb = StringBuilder() +val x = listOf(1, 2, 3).map { "$it*$it" }.joinTo( + // comment1 + buffer = sb, + // comment2 + prefix = "= ", + // comment3 + separator = " + " + // comment4 +) \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/collections/simplifiableCallChain/joinToWithComment.kt.after b/idea/testData/inspectionsLocal/collections/simplifiableCallChain/joinToWithComment.kt.after new file mode 100644 index 00000000000..9867ff5243c --- /dev/null +++ b/idea/testData/inspectionsLocal/collections/simplifiableCallChain/joinToWithComment.kt.after @@ -0,0 +1,12 @@ +// WITH_RUNTIME + +val sb = StringBuilder() +val x = listOf(1, 2, 3).joinTo( + // comment1 + buffer = sb, + // comment2 + prefix = "= ", + // comment3 + separator = " + " + // comment4 +) { "$it*$it" } \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/collections/simplifiableCallChain/listOfNotNullWithComment.kt b/idea/testData/inspectionsLocal/collections/simplifiableCallChain/listOfNotNullWithComment.kt new file mode 100644 index 00000000000..7a9888d7877 --- /dev/null +++ b/idea/testData/inspectionsLocal/collections/simplifiableCallChain/listOfNotNullWithComment.kt @@ -0,0 +1,8 @@ +// WITH_RUNTIME + +fun test() { + listOf( + true, // comment1 + null // comment2 + ).filterNotNull().first() +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/collections/simplifiableCallChain/listOfNotNullWithComment.kt.after b/idea/testData/inspectionsLocal/collections/simplifiableCallChain/listOfNotNullWithComment.kt.after new file mode 100644 index 00000000000..07514a76345 --- /dev/null +++ b/idea/testData/inspectionsLocal/collections/simplifiableCallChain/listOfNotNullWithComment.kt.after @@ -0,0 +1,8 @@ +// WITH_RUNTIME + +fun test() { + listOfNotNull( + true, // comment1 + null // comment2 + ).first() +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java index 13b9bf56e04..825d4fd9bac 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java @@ -1113,16 +1113,31 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest { runTest("idea/testData/inspectionsLocal/collections/simplifiableCallChain/joinToStringWithReference.kt"); } + @TestMetadata("joinToStringWithReferenceAndComment.kt") + public void testJoinToStringWithReferenceAndComment() throws Exception { + runTest("idea/testData/inspectionsLocal/collections/simplifiableCallChain/joinToStringWithReferenceAndComment.kt"); + } + @TestMetadata("joinToStringWithReferenceFake.kt") public void testJoinToStringWithReferenceFake() throws Exception { runTest("idea/testData/inspectionsLocal/collections/simplifiableCallChain/joinToStringWithReferenceFake.kt"); } + @TestMetadata("joinToWithComment.kt") + public void testJoinToWithComment() throws Exception { + runTest("idea/testData/inspectionsLocal/collections/simplifiableCallChain/joinToWithComment.kt"); + } + @TestMetadata("listOfNotNull.kt") public void testListOfNotNull() throws Exception { runTest("idea/testData/inspectionsLocal/collections/simplifiableCallChain/listOfNotNull.kt"); } + @TestMetadata("listOfNotNullWithComment.kt") + public void testListOfNotNullWithComment() throws Exception { + runTest("idea/testData/inspectionsLocal/collections/simplifiableCallChain/listOfNotNullWithComment.kt"); + } + @TestMetadata("mapNotNull.kt") public void testMapNotNull() throws Exception { runTest("idea/testData/inspectionsLocal/collections/simplifiableCallChain/mapNotNull.kt");