Add braces: don't move EOL comment when if statement is single statement

#KT-28619 Fixed
This commit is contained in:
Toshiaki Kameyama
2019-04-08 06:04:24 +03:00
committed by Mikhail Glukhikh
parent a92cf19641
commit 401d8c2d70
8 changed files with 77 additions and 2 deletions
@@ -65,8 +65,7 @@ class AddBracesIntention : SelfTargetingIntention<KtElement>(KtElement::class.ja
semicolon.delete()
}
// Check for single line if expression
if (element is KtIfExpression && expression.isInSingleLine() && element.`else` == null) {
if (element is KtIfExpression) {
// Check if a comment is actually underneath (\n) the expression
val allElements = element.siblings(withItself = false).filterIsInstance<PsiElement>()
val sibling = allElements.firstOrNull { it is PsiComment }
@@ -0,0 +1,10 @@
fun test(b: Boolean) {
if (b) foo(0)
else<caret>
while (true) {
foo(1)
}
// comment about call below
}
fun foo(i: Int) {}
@@ -0,0 +1,11 @@
fun test(b: Boolean) {
if (b) foo(0)
else {
while (true) {
foo(1)
}
}
// comment about call below
}
fun foo(i: Int) {}
@@ -0,0 +1,10 @@
fun test() {
if (false)<caret>
while (true) {
foo(1)
}
//comment about foo(2)
foo(2)
}
fun foo(i: Int) {}
@@ -0,0 +1,11 @@
fun test() {
if (false) {
while (true) {
foo(1)
}
}
//comment about foo(2)
foo(2)
}
fun foo(i: Int) {}
@@ -0,0 +1,9 @@
fun test() {
if (false)<caret>
while (true)
foo(1)
//comment about foo(2)
foo(2)
}
fun foo(i: Int) {}
@@ -0,0 +1,10 @@
fun test() {
if (false) {
while (true)
foo(1)
}
//comment about foo(2)
foo(2)
}
fun foo(i: Int) {}
@@ -721,6 +721,21 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
runTest("idea/testData/intentions/addBraces/addBracesForSingleLineIfWithCommentBeneath3.kt");
}
@TestMetadata("addBracesForSingleStatementElseWithCommentBeneath.kt")
public void testAddBracesForSingleStatementElseWithCommentBeneath() throws Exception {
runTest("idea/testData/intentions/addBraces/addBracesForSingleStatementElseWithCommentBeneath.kt");
}
@TestMetadata("addBracesForSingleStatementIfWithCommentBeneath.kt")
public void testAddBracesForSingleStatementIfWithCommentBeneath() throws Exception {
runTest("idea/testData/intentions/addBraces/addBracesForSingleStatementIfWithCommentBeneath.kt");
}
@TestMetadata("addBracesForSingleStatementIfWithCommentBeneath2.kt")
public void testAddBracesForSingleStatementIfWithCommentBeneath2() throws Exception {
runTest("idea/testData/intentions/addBraces/addBracesForSingleStatementIfWithCommentBeneath2.kt");
}
@TestMetadata("addBracesForWhile.kt")
public void testAddBracesForWhile() throws Exception {
runTest("idea/testData/intentions/addBraces/addBracesForWhile.kt");