KT-11805 Invert if-condition intention breaks code in case of end of line comment

#KT-11805 Fixed
This commit is contained in:
Valentin Kipyatkov
2016-04-07 10:57:03 +03:00
parent b258af5288
commit 22a4bbe1aa
4 changed files with 17 additions and 1 deletions
@@ -117,7 +117,7 @@ class InvertIfConditionIntention : SelfTargetingIntention<KtIfExpression>(KtIfEx
}
//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
}
}
@@ -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);
}
@TestMetadata("endOfLineCommentBug.kt")
public void testEndOfLineCommentBug() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/invertIfCondition/endOfLineCommentBug.kt");
doTest(fileName);
}
@TestMetadata("forLoopWithMultipleExpressions.kt")
public void testForLoopWithMultipleExpressions() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/invertIfCondition/forLoopWithMultipleExpressions.kt");