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 a843d7815a7..787d40728d4 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/collections/SimplifyCallChainFix.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/collections/SimplifyCallChainFix.kt @@ -26,6 +26,7 @@ import org.jetbrains.kotlin.idea.core.moveFunctionLiteralOutsideParentheses import org.jetbrains.kotlin.idea.core.replaced import org.jetbrains.kotlin.idea.formatter.commitAndUnblockDocument import org.jetbrains.kotlin.idea.intentions.callExpression +import org.jetbrains.kotlin.idea.util.CommentSaver import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.psi.psiUtil.PsiChildRange @@ -42,6 +43,7 @@ class SimplifyCallChainFix( override fun getFamilyName() = name fun apply(qualifiedExpression: KtQualifiedExpression) { + val commentSaver = CommentSaver(qualifiedExpression) val factory = KtPsiFactory(qualifiedExpression) val firstExpression = qualifiedExpression.receiverExpression @@ -85,6 +87,10 @@ class SimplifyCallChainFix( val project = qualifiedExpression.project val file = qualifiedExpression.containingKtFile val result = qualifiedExpression.replaced(newQualifiedOrCallExpression) + + if (!firstCallHasArguments && !secondCallHasArguments) { + commentSaver.restore(result) + } if (lambdaExpression != null) { val callExpression = when (result) { is KtQualifiedExpression -> result.callExpression diff --git a/idea/testData/inspectionsLocal/collections/simplifiableCallChain/saveComment.kt b/idea/testData/inspectionsLocal/collections/simplifiableCallChain/saveComment.kt new file mode 100644 index 00000000000..e978bded0b5 --- /dev/null +++ b/idea/testData/inspectionsLocal/collections/simplifiableCallChain/saveComment.kt @@ -0,0 +1,4 @@ +// WITH_RUNTIME +val v1 = listOf(1, 2, 3, 11, 33, 25, 100) + .filter { it % 2 == 0 } // Some Comment + .isNotEmpty() // Some Additional Comment \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/collections/simplifiableCallChain/saveComment.kt.after b/idea/testData/inspectionsLocal/collections/simplifiableCallChain/saveComment.kt.after new file mode 100644 index 00000000000..5639d96715d --- /dev/null +++ b/idea/testData/inspectionsLocal/collections/simplifiableCallChain/saveComment.kt.after @@ -0,0 +1,3 @@ +// WITH_RUNTIME +val v1 = // Some Comment + listOf(1, 2, 3, 11, 33, 25, 100).any { it % 2 == 0 } // Some Additional Comment \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/collections/simplifiableCallChain/saveComment2.kt b/idea/testData/inspectionsLocal/collections/simplifiableCallChain/saveComment2.kt new file mode 100644 index 00000000000..f59bc6eade4 --- /dev/null +++ b/idea/testData/inspectionsLocal/collections/simplifiableCallChain/saveComment2.kt @@ -0,0 +1,4 @@ +// WITH_RUNTIME +val v1 = listOf(1, 2, 3, 11, 33, 25, 100) + .filter { it % 2 == 0 } // Some Comment + .isNotEmpty() \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/collections/simplifiableCallChain/saveComment2.kt.after b/idea/testData/inspectionsLocal/collections/simplifiableCallChain/saveComment2.kt.after new file mode 100644 index 00000000000..675e9a00c77 --- /dev/null +++ b/idea/testData/inspectionsLocal/collections/simplifiableCallChain/saveComment2.kt.after @@ -0,0 +1,3 @@ +// WITH_RUNTIME +val v1 = // Some Comment + listOf(1, 2, 3, 11, 33, 25, 100).any { it % 2 == 0 } \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/collections/simplifiableCallChain/saveComment3.kt b/idea/testData/inspectionsLocal/collections/simplifiableCallChain/saveComment3.kt new file mode 100644 index 00000000000..c54f3118ae5 --- /dev/null +++ b/idea/testData/inspectionsLocal/collections/simplifiableCallChain/saveComment3.kt @@ -0,0 +1,4 @@ +// WITH_RUNTIME +val v1 = listOf(1, 2, 3, 11, 33, 25, 100) + .filter { it % 2 == 0 } + .isNotEmpty() // Some Comment \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/collections/simplifiableCallChain/saveComment3.kt.after b/idea/testData/inspectionsLocal/collections/simplifiableCallChain/saveComment3.kt.after new file mode 100644 index 00000000000..47f97f0f332 --- /dev/null +++ b/idea/testData/inspectionsLocal/collections/simplifiableCallChain/saveComment3.kt.after @@ -0,0 +1,2 @@ +// WITH_RUNTIME +val v1 = listOf(1, 2, 3, 11, 33, 25, 100).any { it % 2 == 0 } // Some Comment \ 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 da12575ef06..16691507207 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java @@ -1276,6 +1276,21 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest { runTest("idea/testData/inspectionsLocal/collections/simplifiableCallChain/mapWithReturn.kt"); } + @TestMetadata("saveComment.kt") + public void testSaveComment() throws Exception { + runTest("idea/testData/inspectionsLocal/collections/simplifiableCallChain/saveComment.kt"); + } + + @TestMetadata("saveComment2.kt") + public void testSaveComment2() throws Exception { + runTest("idea/testData/inspectionsLocal/collections/simplifiableCallChain/saveComment2.kt"); + } + + @TestMetadata("saveComment3.kt") + public void testSaveComment3() throws Exception { + runTest("idea/testData/inspectionsLocal/collections/simplifiableCallChain/saveComment3.kt"); + } + @TestMetadata("idea/testData/inspectionsLocal/collections/simplifiableCallChain/primitiveArray") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class)