If to when conversion preserves comments too
This commit is contained in:
+5
-1
@@ -21,6 +21,7 @@ import com.intellij.openapi.util.TextRange
|
||||
import org.jetbrains.kotlin.idea.intentions.SelfTargetingRangeIntention
|
||||
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.getSubjectToIntroduce
|
||||
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.introduceSubject
|
||||
import org.jetbrains.kotlin.idea.util.CommentSaver
|
||||
import org.jetbrains.kotlin.lexer.KtTokens
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import java.util.*
|
||||
@@ -32,6 +33,8 @@ class IfToWhenIntention : SelfTargetingRangeIntention<KtIfExpression>(KtIfExpres
|
||||
}
|
||||
|
||||
override fun applyTo(element: KtIfExpression, editor: Editor?) {
|
||||
val commentSaver = CommentSaver(element)
|
||||
|
||||
var whenExpression = KtPsiFactory(element).buildExpression {
|
||||
appendFixedText("when {\n")
|
||||
|
||||
@@ -71,7 +74,8 @@ class IfToWhenIntention : SelfTargetingRangeIntention<KtIfExpression>(KtIfExpres
|
||||
whenExpression = whenExpression.introduceSubject()
|
||||
}
|
||||
|
||||
element.replace(whenExpression)
|
||||
val result = element.replace(whenExpression)
|
||||
commentSaver.restore(result)
|
||||
}
|
||||
|
||||
private fun MutableList<KtExpression>.addOrBranches(expression: KtExpression): List<KtExpression> {
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
fun foo(b: Boolean) {
|
||||
<caret>if (b) 1 // 1
|
||||
else 2
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
fun foo(b: Boolean) {
|
||||
when {
|
||||
b -> 1 // 1
|
||||
else -> 2
|
||||
}
|
||||
}
|
||||
@@ -1495,6 +1495,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/branched/ifWhen/ifToWhen"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true);
|
||||
}
|
||||
|
||||
@TestMetadata("comment.kt")
|
||||
public void testComment() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/branched/ifWhen/ifToWhen/comment.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("ifWithEqualityTests.kt")
|
||||
public void testIfWithEqualityTests() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/branched/ifWhen/ifToWhen/ifWithEqualityTests.kt");
|
||||
|
||||
Reference in New Issue
Block a user