mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
invert if: fix psi replacement (IDEA-174966)
This commit is contained in:
+6
-5
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user