From 0ed8eb051200313c1a2eed95deaff891066c24bd Mon Sep 17 00:00:00 2001 From: Valentin Kipyatkov Date: Thu, 11 Feb 2016 21:36:45 +0300 Subject: [PATCH] KT-5717 "Replace 'when' with 'if'" loses a comment #KT-5717 Fixed --- .../intentions/WhenToIfIntention.kt | 10 ++++++++-- .../intentions/branched/ifWhen/whenToIf/comment.kt | 8 ++++++++ .../branched/ifWhen/whenToIf/comment.kt.after | 5 +++++ .../kotlin/idea/intentions/IntentionTestGenerated.java | 6 ++++++ 4 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 idea/testData/intentions/branched/ifWhen/whenToIf/comment.kt create mode 100644 idea/testData/intentions/branched/ifWhen/whenToIf/comment.kt.after diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/WhenToIfIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/WhenToIfIntention.kt index 61a65fd4f6b..008bc3c2eed 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/WhenToIfIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/WhenToIfIntention.kt @@ -21,7 +21,10 @@ import com.intellij.openapi.editor.Editor import com.intellij.openapi.util.TextRange import org.jetbrains.kotlin.idea.intentions.SelfTargetingRangeIntention import org.jetbrains.kotlin.idea.intentions.branchedTransformations.combineWhenConditions -import org.jetbrains.kotlin.psi.* +import org.jetbrains.kotlin.idea.util.CommentSaver +import org.jetbrains.kotlin.psi.KtPsiFactory +import org.jetbrains.kotlin.psi.KtWhenExpression +import org.jetbrains.kotlin.psi.buildExpression class WhenToIfIntention : SelfTargetingRangeIntention(KtWhenExpression::class.java, "Replace 'when' with 'if'"), LowPriorityAction { override fun applicabilityRange(element: KtWhenExpression): TextRange? { @@ -33,6 +36,8 @@ class WhenToIfIntention : SelfTargetingRangeIntention(KtWhenEx } override fun applyTo(element: KtWhenExpression, editor: Editor?) { + val commentSaver = CommentSaver(element) + val factory = KtPsiFactory(element) val ifExpression = factory.buildExpression { val entries = element.entries @@ -57,6 +62,7 @@ class WhenToIfIntention : SelfTargetingRangeIntention(KtWhenEx } } - element.replace(ifExpression) + val result = element.replace(ifExpression) + commentSaver.restore(result) } } diff --git a/idea/testData/intentions/branched/ifWhen/whenToIf/comment.kt b/idea/testData/intentions/branched/ifWhen/whenToIf/comment.kt new file mode 100644 index 00000000000..8051b719375 --- /dev/null +++ b/idea/testData/intentions/branched/ifWhen/whenToIf/comment.kt @@ -0,0 +1,8 @@ +fun foo(b: Boolean) { + when { + // comment 1 + b -> 1 // comment 2 + + else -> 2 + } +} \ No newline at end of file diff --git a/idea/testData/intentions/branched/ifWhen/whenToIf/comment.kt.after b/idea/testData/intentions/branched/ifWhen/whenToIf/comment.kt.after new file mode 100644 index 00000000000..dd97f90352e --- /dev/null +++ b/idea/testData/intentions/branched/ifWhen/whenToIf/comment.kt.after @@ -0,0 +1,5 @@ +fun foo(b: Boolean) { + if (// comment 1 + b) 1 // comment 2 + else 2 +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java index 43b49233342..5455ed61d89 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -1564,6 +1564,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest { KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/branched/ifWhen/whenToIf"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true); } + @TestMetadata("comment.kt") + public void testComment() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/branched/ifWhen/whenToIf/comment.kt"); + doTest(fileName); + } + @TestMetadata("whenWithDotQualifiedExpression.kt") public void testWhenWithDotQualifiedExpression() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/branched/ifWhen/whenToIf/whenWithDotQualifiedExpression.kt");