invert if: fix psi replacement (IDEA-174966)

This commit is contained in:
Anna Kozlova
2017-06-26 17:07:29 +03:00
parent 329cc26c29
commit 9e12271bd3
4 changed files with 33 additions and 7 deletions
@@ -320,11 +320,12 @@ public class InvertIfConditionAction extends PsiElementBaseIntentionAction {
final Project project = statement.getProject();
final PsiElementFactory factory = JavaPsiFacade.getInstance(project).getElementFactory();
final CodeStyleManager codeStyle = CodeStyleManager.getInstance(project);
PsiCodeBlock codeBlock = factory.createCodeBlockFromText("{}", statement);
codeBlock = (PsiCodeBlock)codeStyle.reformat(codeBlock);
codeBlock.add(statement);
codeBlock = (PsiCodeBlock)statement.replace(codeBlock);
return codeBlock.getStatements()[0];
PsiIfStatement ifStatement = (PsiIfStatement)factory.createStatementFromText("if (true) {}", statement);
ifStatement = (PsiIfStatement)codeStyle.reformat(ifStatement);
PsiStatement thenBranch = ifStatement.getThenBranch();
((PsiBlockStatement)thenBranch).getCodeBlock().add(statement);
PsiCodeBlock stmt = ((PsiBlockStatement)statement.replace(thenBranch)).getCodeBlock();
return stmt.getStatements()[0];
}
private static PsiIfStatement addAfterWithinCodeBlock(@NotNull PsiIfStatement ifStatement, @NotNull PsiStatement branch) {
@@ -1,8 +1,7 @@
// "Invert 'if' condition" "true"
class Main {
boolean method(boolean a, boolean b) {
for (int i = 1; i < 10; i++)
{
for (int i = 1; i < 10; i++) {
if (b) {
continue;
}
@@ -0,0 +1,14 @@
// "Invert 'if' condition" "true"
class Main {
Object foo(boolean b1, boolean b2) {
if (b1) {
return null;
} else {
if (b2) {
return null;
}
return "a";
}
}
}
@@ -0,0 +1,12 @@
// "Invert 'if' condition" "true"
class Main {
Object foo(boolean b1, boolean b2) {
if (b1) {
return null;
} else if (!b<caret>2) {
return "a";
}
return null;
}
}