[java] BlockJoinLinesHandler: handle more tricky cases from IDEA-263507

GitOrigin-RevId: 1661c82421ea73a3a18c349330889c309769e053
This commit is contained in:
Tagir Valeev
2021-03-09 12:07:43 +00:00
committed by intellij-monorepo-bot
parent ca0ca6e53b
commit b4c90cfd11
4 changed files with 39 additions and 1 deletions
@@ -58,7 +58,7 @@ public class BlockJoinLinesHandler implements JoinLinesHandlerDelegate {
}
if (foundStatement == null) return -1;
PsiElement parent = codeBlock.getParent();
if (foundStatement instanceof PsiIfStatement && parent instanceof PsiBlockStatement) {
if (isPotentialShortIf(foundStatement) && parent instanceof PsiBlockStatement) {
PsiElement grandParent = parent.getParent();
if (grandParent instanceof PsiIfStatement &&
((PsiIfStatement)grandParent).getThenBranch() == parent &&
@@ -81,6 +81,20 @@ public class BlockJoinLinesHandler implements JoinLinesHandlerDelegate {
return -1;
}
private static boolean isPotentialShortIf(PsiElement statement) {
while (true) {
// JLS 14.5
if (statement instanceof PsiLabeledStatement) {
statement = ((PsiLabeledStatement)statement).getStatement();
}
else if (statement instanceof PsiForStatement || statement instanceof PsiForeachStatement || statement instanceof PsiWhileStatement) {
statement = ((PsiLoopStatement)statement).getBody();
}
else break;
}
return statement instanceof PsiIfStatement;
}
private static int getForceBraceSetting(PsiElement statement) {
CodeStyleSettings settings = CodeStyle.getSettings(statement.getContainingFile());
final CommonCodeStyleSettings codeStyleSettings = settings.getCommonSettings(JavaLanguage.INSTANCE);
@@ -0,0 +1,12 @@
class C {
private static void fn(boolean condA, boolean condB) {
if (condA) {<caret>
for(int i=0; i<10; i++)
if (condB) {
System.out.println("condA && condB");
}
} else {
System.out.println("!condA");
}
}
}
@@ -0,0 +1,11 @@
class C {
private static void fn(boolean condA, boolean condB) {
if (condA) { for(int i=0; i<10; i++)
if (condB) {
System.out.println("condA && condB");
}
} else {
System.out.println("!condA");
}
}
}
@@ -197,6 +197,7 @@ public class JoinLinesTest extends LightJavaCodeInsightTestCase {
}
public void testUnwrapCodeBlockIfElse() { doTest(); }
public void testUnwrapCodeBlockIfElse2() { doTest(); }
public void testAssignmentExpression() { doTest(); }
public void testAssignmentExpression2() { doTest(); }
public void testAssignmentExpressionPrecedence() { doTest(); }