invert if: don't include single rBrace in block

found by property testing
This commit is contained in:
Anna.Kozlova
2018-11-09 13:51:51 +01:00
parent b1ede49516
commit 338f87ab95
3 changed files with 46 additions and 3 deletions
@@ -287,9 +287,14 @@ public class InvertIfConditionAction extends PsiElementBaseIntentionAction {
PsiBlockStatement codeBlock = (PsiBlockStatement) factory.createStatementFromText("{}", ifStatement);
codeBlock.getCodeBlock().addRange(first, last);
ct.replaceAndRestoreComments(ifStatement.getThenBranch(), codeBlock);
first.getParent().deleteChildRange(first, last);
if (first == last && PsiUtil.isJavaToken(last, JavaTokenType.RBRACE)) {
ct.replaceAndRestoreComments(ifStatement.getThenBranch(), codeBlock);
}
else {
codeBlock.getCodeBlock().addRange(first, last);
ct.replaceAndRestoreComments(ifStatement.getThenBranch(), codeBlock);
first.getParent().deleteChildRange(first, last);
}
}
codeStyle.reformat(ifStatement);
return ifStatement;
@@ -0,0 +1,20 @@
// "Invert 'if' condition" "true"
class A {
void foo(boolean b, String[] entries) {
{
try {
String filePath = "";
for (String entry : entries) {
final String rootPath = "";
if (!b) {
}
else {
break;
}
}
}
finally {
}
}
}
}
@@ -0,0 +1,18 @@
// "Invert 'if' condition" "true"
class A {
void foo(boolean b, String[] entries) {
{
try {
String filePath = "";
for (String entry : entries) {
final String rootPath = "";
i<caret>f (b) {
break;
}
}
}
finally {
}
}
}
}