Skip to content

Commit bec28c8

Browse files
committed
Replace with for-each: add new-lines using comment saver
Related to KT-15858
1 parent 7f880bf commit bec28c8

10 files changed

+27
-25
lines changed

idea/src/org/jetbrains/kotlin/idea/intentions/ConvertToForEachFunctionCallIntention.kt

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,8 @@
1717
package org.jetbrains.kotlin.idea.intentions
1818

1919
import com.intellij.openapi.editor.Editor
20-
import com.intellij.psi.PsiComment
2120
import com.intellij.psi.PsiElement
22-
import org.jetbrains.kotlin.idea.refactoring.getLineNumber
2321
import org.jetbrains.kotlin.idea.util.CommentSaver
24-
import org.jetbrains.kotlin.lexer.KtTokens
2522
import org.jetbrains.kotlin.psi.*
2623
import org.jetbrains.kotlin.psi.psiUtil.contentRange
2724
import org.jetbrains.kotlin.psi.psiUtil.endOffset
@@ -40,20 +37,18 @@ class ConvertToForEachFunctionCallIntention : SelfTargetingIntention<KtForExpres
4037
}
4138

4239
override fun applyTo(element: KtForExpression, editor: Editor?) {
43-
val commentSaver = CommentSaver(element)
40+
val commentSaver = CommentSaver(element, saveLineBreaks = true)
4441

4542
val labelName = element.getLabelName()
4643

4744
val body = element.body!!
4845
val loopParameter = element.loopParameter!!
4946

50-
val blockExpression = body as? KtBlockExpression
51-
val functionBodyArgument: Any = blockExpression?.contentRange() ?: body
52-
val pattern = if (needLineBreaks(blockExpression)) "$0.forEach{$1->\n$2\n}" else "$0.forEach{$1->$2}"
47+
val functionBodyArgument: Any = (body as? KtBlockExpression)?.contentRange() ?: body
5348

5449
val psiFactory = KtPsiFactory(element)
5550
val foreachExpression = psiFactory.createExpressionByPattern(
56-
pattern, element.loopRange!!, loopParameter, functionBodyArgument
51+
"$0.forEach{$1->\n$2}", element.loopRange!!, loopParameter, functionBodyArgument
5752
)
5853
val result = element.replace(foreachExpression) as KtElement
5954

@@ -64,14 +59,6 @@ class ConvertToForEachFunctionCallIntention : SelfTargetingIntention<KtForExpres
6459
commentSaver.restore(result)
6560
}
6661

67-
private fun needLineBreaks(blockExpression: KtBlockExpression?): Boolean {
68-
val contentRange = blockExpression?.contentRange() ?: return false
69-
if ((contentRange.last as? PsiComment)?.tokenType != KtTokens.EOL_COMMENT) return false
70-
val statements = blockExpression.statements
71-
val fistStatementLine = statements.firstOrNull()?.getLineNumber()
72-
return statements.all { it.getLineNumber() == fistStatementLine }
73-
}
74-
7562
private fun KtElement.getContinuesWithLabel(labelName: String?): List<KtContinueExpression> {
7663
val continueElements = ArrayList<KtContinueExpression>()
7764

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
// WITH_RUNTIME
22
fun main() {
3-
1.rangeTo(2).forEach { x -> x }
3+
1.rangeTo(2).forEach { x ->
4+
x
5+
}
46
}

idea/testData/intentions/convertToForEachFunctionCall/blockCommentOnly.kt.after

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,7 @@
22
fun foo() {}
33

44
fun test() {
5-
listOf(1, 2).forEach { l -> /* comment */ }
5+
listOf(1, 2).forEach { l ->
6+
/* comment */
7+
}
68
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// WITH_RUNTIME
22
fun foo() {
3-
(1..10/* from 1 to 10 */).forEach { x /* current */ -> }
3+
(1..10/* from 1 to 10 */).forEach { x /* current */ ->
4+
}
45
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// WITH_RUNTIME
22
fun foo() {
3-
(1..10).forEach { x -> }
3+
(1..10).forEach { x ->
4+
}
45
}

idea/testData/intentions/convertToForEachFunctionCall/iterativeElementTypeSpecified.kt.after

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,7 @@
22
fun main() {
33
val list = 1..4
44

5-
list.forEach { x: Int -> x }
5+
list.forEach { x: Int ->
6+
x
7+
}
68
}

idea/testData/intentions/convertToForEachFunctionCall/noCurlyBraces.kt.after

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ fun foo() {
33
val list = 1..4
44
val i = 0
55

6-
list.forEach { i -> i }
6+
list.forEach { i ->
7+
i
8+
}
79
i
810
}

idea/testData/intentions/convertToForEachFunctionCall/noStatements.kt.after

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
fun foo() {
33
val list = 1..4
44

5-
list.forEach { i -> }
5+
list.forEach { i ->
6+
}
67
}

idea/testData/intentions/convertToForEachFunctionCall/simple.kt.after

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,7 @@
22
fun foo() {
33
val list = 1..4
44

5-
list.forEach { x -> x }
5+
list.forEach { x ->
6+
x
7+
}
68
}

idea/testData/intentions/convertToForEachFunctionCall/typeAnnotatedWithNonBlockBody.kt.after

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,7 @@
22
fun main() {
33
val list = 1..4
44

5-
list.forEach { x: Int -> 11 }
5+
list.forEach { x: Int ->
6+
11
7+
}
68
}

0 commit comments

Comments
 (0)