JetPsiFactory.createByPattern supports PsiChildRange as argument, used it in ConvertToForEachFunctionCallIntention
This commit is contained in:
+2
-13
@@ -37,21 +37,10 @@ public class ConvertToForEachFunctionCallIntention : JetSelfTargetingIntention<J
|
||||
val body = element.getBody()!!
|
||||
val loopParameter = element.getLoopParameter()!!
|
||||
|
||||
val functionBodyText = when (body) {
|
||||
is JetBlockExpression -> {
|
||||
val content = body.contentRange()
|
||||
val text = content.getText()
|
||||
if (content.last?.getNode()?.getElementType() == JetTokens.EOL_COMMENT)
|
||||
text + "\n"
|
||||
else
|
||||
text
|
||||
}
|
||||
|
||||
else -> body.getText()
|
||||
}
|
||||
val functionBodyArgument: Any = if (body is JetBlockExpression) body.contentRange() else body
|
||||
|
||||
val foreachExpression = JetPsiFactory(element).createExpressionByPattern(
|
||||
"$0.forEach{$1->$2}", element.getLoopRange()!!, loopParameter, functionBodyText)
|
||||
"$0.forEach{$1->$2}", element.getLoopRange()!!, loopParameter, functionBodyArgument)
|
||||
element.replace(foreachExpression)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
fun foo() {
|
||||
val list = 1..4
|
||||
|
||||
list.forEach { x ->// start of loop
|
||||
list.forEach { x ->
|
||||
// start of loop
|
||||
// comment 1
|
||||
var v = x + 1
|
||||
// comment 2
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
fun foo() {
|
||||
val list = 1..4
|
||||
|
||||
list.forEach { x ->// comment
|
||||
list.forEach { x ->
|
||||
// comment
|
||||
var v = x + 1
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
fun foo() {
|
||||
<caret>for (x in 1..10) {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fun foo() {
|
||||
(1..10).forEach { x -> }
|
||||
}
|
||||
@@ -3943,6 +3943,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("emptyBody.kt")
|
||||
public void testEmptyBody() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/convertToForEachFunctionCall/emptyBody.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("iterativeElementTypeSpecified.kt")
|
||||
public void testIterativeElementTypeSpecified() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/convertToForEachFunctionCall/iterativeElementTypeSpecified.kt");
|
||||
|
||||
Reference in New Issue
Block a user