If to when: more accurate comment handling #KT-12649 Fixed
This commit is contained in:
+5
-3
@@ -89,9 +89,11 @@ class IfToWhenIntention : SelfTargetingRangeIntention<KtIfExpression>(KtIfExpres
|
|||||||
|
|
||||||
override fun applyTo(element: KtIfExpression, editor: Editor?) {
|
override fun applyTo(element: KtIfExpression, editor: Editor?) {
|
||||||
val siblings = element.siblings()
|
val siblings = element.siblings()
|
||||||
val commentSaver = CommentSaver(PsiChildRange(element, siblings.last()), saveLineBreaks = true)
|
val elementCommentSaver = CommentSaver(element)
|
||||||
|
val fullCommentSaver = CommentSaver(PsiChildRange(element, siblings.last()), saveLineBreaks = true)
|
||||||
|
|
||||||
val toDelete = ArrayList<PsiElement>()
|
val toDelete = ArrayList<PsiElement>()
|
||||||
|
var applyFullCommentSaver = true
|
||||||
var whenExpression = KtPsiFactory(element).buildExpression {
|
var whenExpression = KtPsiFactory(element).buildExpression {
|
||||||
appendFixedText("when {\n")
|
appendFixedText("when {\n")
|
||||||
|
|
||||||
@@ -134,8 +136,8 @@ class IfToWhenIntention : SelfTargetingRangeIntention<KtIfExpression>(KtIfExpres
|
|||||||
currentIfExpression = currentElseBranch
|
currentIfExpression = currentElseBranch
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
toDelete.addAll(baseIfExpressionForSyntheticBranch.siblingsUpTo(currentElseBranch))
|
|
||||||
appendElseBlock(currentElseBranch)
|
appendElseBlock(currentElseBranch)
|
||||||
|
applyFullCommentSaver = false
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -149,7 +151,7 @@ class IfToWhenIntention : SelfTargetingRangeIntention<KtIfExpression>(KtIfExpres
|
|||||||
}
|
}
|
||||||
|
|
||||||
val result = element.replace(whenExpression)
|
val result = element.replace(whenExpression)
|
||||||
commentSaver.restore(result)
|
(if (applyFullCommentSaver) fullCommentSaver else elementCommentSaver).restore(result)
|
||||||
toDelete.forEach {
|
toDelete.forEach {
|
||||||
it.delete()
|
it.delete()
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-2
@@ -1,6 +1,6 @@
|
|||||||
fun foo(arg: Int): Int {
|
fun foo(arg: Int): Int {
|
||||||
when (arg) {
|
when (arg) {
|
||||||
0 -> return 0 // 1
|
0 -> return 0 // 1
|
||||||
else -> return -1 // 2
|
else -> return -1
|
||||||
}
|
} // 2
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
fun toInt(s: Number): Int {
|
||||||
|
<caret>if (s is Int) {
|
||||||
|
foo()
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
|
||||||
|
// code below will be lost!
|
||||||
|
bar()
|
||||||
|
// before return
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
fun foo() {}
|
||||||
|
fun bar() {}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
fun toInt(s: Number): Int {
|
||||||
|
when (s) {
|
||||||
|
is Int -> foo()
|
||||||
|
else -> return -1
|
||||||
|
}
|
||||||
|
|
||||||
|
// code below will be lost!
|
||||||
|
bar()
|
||||||
|
// before return
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
fun foo() {}
|
||||||
|
fun bar() {}
|
||||||
@@ -1576,6 +1576,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
|||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("ifElseSwallowTail.kt")
|
||||||
|
public void testIfElseSwallowTail() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/branched/ifWhen/ifToWhen/ifElseSwallowTail.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("ifWithEqualityTests.kt")
|
@TestMetadata("ifWithEqualityTests.kt")
|
||||||
public void testIfWithEqualityTests() throws Exception {
|
public void testIfWithEqualityTests() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/branched/ifWhen/ifToWhen/ifWithEqualityTests.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/branched/ifWhen/ifToWhen/ifWithEqualityTests.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user