Fixed if-statements with else branch handling

This commit is contained in:
Valentin Kipyatkov
2014-12-17 19:33:34 +03:00
parent 3db51cfcd8
commit b1372f0d9e
6 changed files with 38 additions and 0 deletions
@@ -47,6 +47,9 @@ public class JoinBlockIntoSingleStatementHandler : JoinRawLinesHandlerDelegate {
// handle nested if's
val pparent = parent.getParent()
if (pparent is JetIfExpression && block == pparent.getThen() && statement is JetIfExpression && statement.getElse() == null) {
// if outer if has else-branch and inner does not have it, do not remove braces otherwise else-branch will belong to different if!
if (pparent.getElse() != null) return -1
val condition1 = pparent.getCondition()
val condition2 = statement.getCondition()
val body = statement.getThen()
@@ -0,0 +1,5 @@
fun foo() {
<caret>if (a) {
if (b) foo() else bar()
}
}
@@ -0,0 +1,3 @@
fun foo() {
if (a) <caret>if (b) foo() else bar()
}
@@ -0,0 +1,8 @@
fun foo() {
<caret>if (a) {
if (b) foo()
}
else {
bar()
}
}
@@ -0,0 +1,7 @@
fun foo() {
if (a) {<caret> if (b) foo()
}
else {
bar()
}
}
@@ -174,6 +174,18 @@ public class JoinLinesTestGenerated extends AbstractJoinLinesTest {
doTest(fileName);
}
@TestMetadata("InnerWithElse.kt")
public void testInnerWithElse() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/joinLines/nestedIfs/InnerWithElse.kt");
doTest(fileName);
}
@TestMetadata("OuterWithElse.kt")
public void testOuterWithElse() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/joinLines/nestedIfs/OuterWithElse.kt");
doTest(fileName);
}
@TestMetadata("Simple.kt")
public void testSimple() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/joinLines/nestedIfs/Simple.kt");