InvertIfConditionAction: preserve comments after last statement

Fixes IDEA-153371 invert 'if' condition deletes comment lines
This commit is contained in:
Tagir Valeev
2017-09-29 15:19:23 +07:00
parent a64cada7e1
commit e7d2193a56
5 changed files with 63 additions and 1 deletions

View File

@@ -209,7 +209,11 @@ public class InvertIfConditionAction extends PsiElementBaseIntentionAction {
while (firstElement.getPrevSibling() instanceof PsiWhiteSpace || firstElement.getPrevSibling() instanceof PsiComment) {
firstElement = firstElement.getPrevSibling();
}
ifStatement.getParent().addRangeAfter(firstElement, statements[statements.length - 1], ifStatement);
PsiElement lastElement = statements[statements.length - 1];
while (lastElement.getNextSibling() instanceof PsiWhiteSpace || lastElement.getNextSibling() instanceof PsiComment) {
lastElement = lastElement.getNextSibling();
}
ifStatement.getParent().addRangeAfter(firstElement, lastElement, ifStatement);
}
} else {
if (!(thenBranch instanceof PsiReturnStatement)) {

View File

@@ -0,0 +1,13 @@
// "Invert 'if' condition" "true"
class A {
// IDEA-153371
public void foo() {
String value ="not-null";
if (value == null) {
return;
}
System.out.println(value);
// Comment gets deleted.
}
}

View File

@@ -0,0 +1,17 @@
// "Invert 'if' condition" "true"
class A {
public void foo() {
String value ="not-null";
if (value == null) {
return;
}
/* block*/
// Before
System.out.println(value);
/* inside */
System.out.println(value);
// After
/* end block*/
}
}

View File

@@ -0,0 +1,12 @@
// "Invert 'if' condition" "true"
class A {
// IDEA-153371
public void foo() {
String value ="not-null";
<caret>if (value != null) {
System.out.println(value);
// Comment gets deleted.
}
}
}

View File

@@ -0,0 +1,16 @@
// "Invert 'if' condition" "true"
class A {
public void foo() {
String value ="not-null";
<caret>if (value != null) {
/* block*/
// Before
System.out.println(value);
/* inside */
System.out.println(value);
// After
/* end block*/
}
}
}