KT-11805 Invert if-condition intention breaks code in case of end of line comment
#KT-11805 Fixed
This commit is contained in:
@@ -117,7 +117,7 @@ class InvertIfConditionIntention : SelfTargetingIntention<KtIfExpression>(KtIfEx
|
|||||||
}
|
}
|
||||||
|
|
||||||
//TODO: no block if single?
|
//TODO: no block if single?
|
||||||
val newIf = factory.createExpressionByPattern("if ($0) { $1 }", newCondition, newIfBodyText)
|
val newIf = factory.createExpressionByPattern("if ($0) { $1\n}", newCondition, newIfBodyText) // we need to insert '\n' because the text can end with an end-of-line comment
|
||||||
return updatedIf.replace(newIf) as KtIfExpression
|
return updatedIf.replace(newIf) as KtIfExpression
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,4 @@
|
|||||||
|
fun foo(p: String?): Int? {
|
||||||
|
<caret>if (p == null) return null
|
||||||
|
return p.hashCode() // comment
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
fun foo(p: String?): Int? {
|
||||||
|
<caret>if (p != null) {
|
||||||
|
return p.hashCode() // comment
|
||||||
|
}
|
||||||
|
return null
|
||||||
|
}
|
||||||
@@ -6105,6 +6105,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
|||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("endOfLineCommentBug.kt")
|
||||||
|
public void testEndOfLineCommentBug() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/invertIfCondition/endOfLineCommentBug.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("forLoopWithMultipleExpressions.kt")
|
@TestMetadata("forLoopWithMultipleExpressions.kt")
|
||||||
public void testForLoopWithMultipleExpressions() throws Exception {
|
public void testForLoopWithMultipleExpressions() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/invertIfCondition/forLoopWithMultipleExpressions.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/invertIfCondition/forLoopWithMultipleExpressions.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user