preserve comments on invert if (IDEA-159847)

This commit is contained in:
Anna Kozlova
2016-08-15 21:07:36 +03:00
parent 1d7e2f6389
commit b2b09c29ee
3 changed files with 22 additions and 3 deletions

View File

@@ -318,9 +318,11 @@ public class InvertIfConditionAction extends PsiElementBaseIntentionAction {
private static void addAfter(PsiIfStatement ifStatement, PsiStatement thenBranch) throws IncorrectOperationException {
if (thenBranch instanceof PsiBlockStatement) {
PsiBlockStatement blockStatement = (PsiBlockStatement) thenBranch;
PsiStatement[] statements = blockStatement.getCodeBlock().getStatements();
if (statements.length > 0) {
ifStatement.getParent().addRangeAfter(statements[0], statements[statements.length - 1], ifStatement);
final PsiCodeBlock block = blockStatement.getCodeBlock();
final PsiElement firstBodyElement = block.getFirstBodyElement();
final PsiElement lastBodyElement = block.getLastBodyElement();
if (firstBodyElement != null && lastBodyElement != null) {
ifStatement.getParent().addRangeAfter(firstBodyElement, lastBodyElement, ifStatement);
}
} else {
ifStatement.getParent().addAfter(thenBranch, ifStatement);

View File

@@ -0,0 +1,9 @@
// "Invert 'if' condition" "true"
class A {
public void foo() {
if (!c) {
return;
} //comments to restore
a();
}
}

View File

@@ -0,0 +1,8 @@
// "Invert 'if' condition" "true"
class A {
public void foo() {
<caret>if (c) { //comments to restore
a();
}
}
}