ConvertForEachToForLoopIntention preserves comments
This commit is contained in:
@@ -18,9 +18,9 @@ package org.jetbrains.kotlin.idea.intentions
|
||||
|
||||
import com.intellij.openapi.editor.Editor
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||
import org.jetbrains.kotlin.idea.core.CommentSaver
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.resolve.DescriptorUtils
|
||||
import org.jetbrains.kotlin.resolve.calls.callUtil.getCall
|
||||
import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
|
||||
import org.jetbrains.kotlin.resolve.scopes.receivers.ExpressionReceiver
|
||||
|
||||
@@ -37,8 +37,13 @@ public class ConvertForEachToForLoopIntention : JetSelfTargetingOffsetIndependen
|
||||
|
||||
override fun applyTo(element: JetSimpleNameExpression, editor: Editor) {
|
||||
val (expressionToReplace, receiver, functionLiteral) = extractData(element)!!
|
||||
|
||||
val commentSaver = CommentSaver(expressionToReplace)
|
||||
|
||||
val loop = generateLoop(functionLiteral, receiver)
|
||||
expressionToReplace.replace(loop)
|
||||
val result = expressionToReplace.replace(loop)
|
||||
|
||||
commentSaver.restoreComments(result)
|
||||
}
|
||||
|
||||
private data class Data(
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
// WITH_RUNTIME
|
||||
fun foo(list: List<String?>) {
|
||||
list.filter { it != null } /* filter out nulls */
|
||||
.forEach<caret> { print(1..4) /* print x */ }
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
// WITH_RUNTIME
|
||||
fun foo(list: List<String?>) {
|
||||
for (it in list.filter { it != null } /* filter out nulls */) {
|
||||
print(1..4) /* print x */
|
||||
}
|
||||
}
|
||||
@@ -2875,6 +2875,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("keepComments.kt")
|
||||
public void testKeepComments() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/convertForEachToForLoop/keepComments.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("parenthesizedExpression.kt")
|
||||
public void testParenthesizedExpression() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/convertForEachToForLoop/parenthesizedExpression.kt");
|
||||
|
||||
Reference in New Issue
Block a user