Fix SimplifiableCallChain inspection quick fix removes comments for intermediate operations

This commit is contained in:
Burak Eregar
2019-03-13 01:30:04 +00:00
committed by Mikhail Glukhikh
parent 8728bc0820
commit db4144426a
8 changed files with 41 additions and 0 deletions
@@ -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
@@ -0,0 +1,4 @@
// WITH_RUNTIME
val v1 = listOf(1, 2, 3, 11, 33, 25, 100)
.filter<caret> { it % 2 == 0 } // Some Comment
.isNotEmpty() // Some Additional Comment
@@ -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
@@ -0,0 +1,4 @@
// WITH_RUNTIME
val v1 = listOf(1, 2, 3, 11, 33, 25, 100)
.filter<caret> { it % 2 == 0 } // Some Comment
.isNotEmpty()
@@ -0,0 +1,3 @@
// WITH_RUNTIME
val v1 = // Some Comment
listOf(1, 2, 3, 11, 33, 25, 100).any { it % 2 == 0 }
@@ -0,0 +1,4 @@
// WITH_RUNTIME
val v1 = listOf(1, 2, 3, 11, 33, 25, 100)
.filter<caret> { it % 2 == 0 }
.isNotEmpty() // Some Comment
@@ -0,0 +1,2 @@
// WITH_RUNTIME
val v1 = listOf(1, 2, 3, 11, 33, 25, 100).any { it % 2 == 0 } // Some Comment
@@ -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)