diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetPsiFactory.kt b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetPsiFactory.kt index 4e60bc31ca6..281e2c207ae 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetPsiFactory.kt +++ b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetPsiFactory.kt @@ -300,12 +300,11 @@ public class JetPsiFactory(private val project: Project) { return createExpression("$" + fieldName) } - public fun createBinaryExpression(lhs: String, op: String, rhs: String): JetBinaryExpression { - return createExpression(lhs + " " + op + " " + rhs) as JetBinaryExpression - } - - public fun createBinaryExpression(lhs: JetExpression?, op: String, rhs: JetExpression?): JetBinaryExpression { - return createBinaryExpression(JetPsiUtil.getText(lhs), op, JetPsiUtil.getText(rhs)) + public fun createBinaryExpression(lhs: JetExpression, op: String, rhs: JetExpression): JetBinaryExpression { + val expression = createExpression("a $op b") as JetBinaryExpression + expression.getLeft().replace(lhs) + expression.getRight().replace(rhs) + return expression } public fun createTypeCodeFragment(text: String, context: PsiElement?): JetTypeCodeFragment { diff --git a/idea/src/org/jetbrains/jet/plugin/joinLines/JoinBlockIntoSingleStatementHandler.kt b/idea/src/org/jetbrains/jet/plugin/joinLines/JoinBlockIntoSingleStatementHandler.kt index df240a94fa6..e9a9bef79dc 100644 --- a/idea/src/org/jetbrains/jet/plugin/joinLines/JoinBlockIntoSingleStatementHandler.kt +++ b/idea/src/org/jetbrains/jet/plugin/joinLines/JoinBlockIntoSingleStatementHandler.kt @@ -24,6 +24,8 @@ import org.jetbrains.jet.lexer.JetTokens import org.jetbrains.jet.lang.psi.JetBlockExpression import org.jetbrains.jet.lang.psi.JetContainerNode import org.jetbrains.jet.lang.psi.JetWhenEntry +import org.jetbrains.jet.lang.psi.JetIfExpression +import org.jetbrains.jet.lang.psi.JetPsiFactory public class JoinBlockIntoSingleStatementHandler : JoinRawLinesHandlerDelegate { override fun tryJoinRawLines(document: Document, file: PsiFile, start: Int, end: Int): Int { @@ -42,6 +44,20 @@ public class JoinBlockIntoSingleStatementHandler : JoinRawLinesHandlerDelegate { if (parent !is JetContainerNode && parent !is JetWhenEntry) return -1 if (block.getNode().getChildren(JetTokens.COMMENTS).isNotEmpty()) return -1 // otherwise we will loose comments + // handle nested if's + val pparent = parent.getParent() + if (pparent is JetIfExpression && block == pparent.getThen() && statement is JetIfExpression && statement.getElse() == null) { + val condition1 = pparent.getCondition() + val condition2 = statement.getCondition() + val body = statement.getThen() + if (condition1 != null && condition2 != null && body != null) { + val newCondition = JetPsiFactory(pparent).createBinaryExpression(condition1, "&&", condition2) + condition1.replace(newCondition) + val newBody = block.replace(body) + return newBody.getTextRange()!!.getStartOffset() + } + } + val newStatement = block.replace(statement) return newStatement.getTextRange()!!.getStartOffset() } diff --git a/idea/testData/joinLines/nestedIfs/BlockBody.kt b/idea/testData/joinLines/nestedIfs/BlockBody.kt new file mode 100644 index 00000000000..5faa4cc76e1 --- /dev/null +++ b/idea/testData/joinLines/nestedIfs/BlockBody.kt @@ -0,0 +1,7 @@ +fun foo() { + if (a) { + if (b) { + foo() + } + } +} diff --git a/idea/testData/joinLines/nestedIfs/BlockBody.kt.after b/idea/testData/joinLines/nestedIfs/BlockBody.kt.after new file mode 100644 index 00000000000..204840952c9 --- /dev/null +++ b/idea/testData/joinLines/nestedIfs/BlockBody.kt.after @@ -0,0 +1,5 @@ +fun foo() { + if (a && b) { + foo() + } +} diff --git a/idea/testData/joinLines/nestedIfs/ComplexCondition1.kt b/idea/testData/joinLines/nestedIfs/ComplexCondition1.kt new file mode 100644 index 00000000000..622a8d39cea --- /dev/null +++ b/idea/testData/joinLines/nestedIfs/ComplexCondition1.kt @@ -0,0 +1,5 @@ +fun foo() { + if (a && b) { + if (c && d) foo() + } +} diff --git a/idea/testData/joinLines/nestedIfs/ComplexCondition1.kt.after b/idea/testData/joinLines/nestedIfs/ComplexCondition1.kt.after new file mode 100644 index 00000000000..0cb1292da4d --- /dev/null +++ b/idea/testData/joinLines/nestedIfs/ComplexCondition1.kt.after @@ -0,0 +1,3 @@ +fun foo() { + if (a && b && c && d) foo() +} diff --git a/idea/testData/joinLines/nestedIfs/ComplexCondition2.kt b/idea/testData/joinLines/nestedIfs/ComplexCondition2.kt new file mode 100644 index 00000000000..df3cf0943c1 --- /dev/null +++ b/idea/testData/joinLines/nestedIfs/ComplexCondition2.kt @@ -0,0 +1,5 @@ +fun foo() { + if (a && b) { + if (c || d) foo() + } +} diff --git a/idea/testData/joinLines/nestedIfs/ComplexCondition2.kt.after b/idea/testData/joinLines/nestedIfs/ComplexCondition2.kt.after new file mode 100644 index 00000000000..87653685b7c --- /dev/null +++ b/idea/testData/joinLines/nestedIfs/ComplexCondition2.kt.after @@ -0,0 +1,3 @@ +fun foo() { + if (a && b && (c || d)) foo() +} diff --git a/idea/testData/joinLines/nestedIfs/Simple.kt b/idea/testData/joinLines/nestedIfs/Simple.kt new file mode 100644 index 00000000000..ca208bd2750 --- /dev/null +++ b/idea/testData/joinLines/nestedIfs/Simple.kt @@ -0,0 +1,5 @@ +fun foo() { + if (a) { + if (b) foo() + } +} diff --git a/idea/testData/joinLines/nestedIfs/Simple.kt.after b/idea/testData/joinLines/nestedIfs/Simple.kt.after new file mode 100644 index 00000000000..eb3e4e4d9a3 --- /dev/null +++ b/idea/testData/joinLines/nestedIfs/Simple.kt.after @@ -0,0 +1,3 @@ +fun foo() { + if (a && b) foo() +} diff --git a/idea/tests/org/jetbrains/jet/plugin/intentions/declarations/JoinLinesTestGenerated.java b/idea/tests/org/jetbrains/jet/plugin/intentions/declarations/JoinLinesTestGenerated.java index f08b4dd02eb..2011213ea7d 100644 --- a/idea/tests/org/jetbrains/jet/plugin/intentions/declarations/JoinLinesTestGenerated.java +++ b/idea/tests/org/jetbrains/jet/plugin/intentions/declarations/JoinLinesTestGenerated.java @@ -30,7 +30,7 @@ import java.util.regex.Pattern; @SuppressWarnings("all") @TestMetadata("idea/testData/joinLines") @TestDataPath("$PROJECT_ROOT") -@InnerTestClasses({JoinLinesTestGenerated.DeclarationAndAssignment.class, JoinLinesTestGenerated.RemoveBraces.class}) +@InnerTestClasses({JoinLinesTestGenerated.DeclarationAndAssignment.class, JoinLinesTestGenerated.NestedIfs.class, JoinLinesTestGenerated.RemoveBraces.class}) @RunWith(JUnit3RunnerWithInners.class) public class JoinLinesTestGenerated extends AbstractJoinLinesTest { public void testAllFilesPresentInJoinLines() throws Exception { @@ -148,6 +148,39 @@ public class JoinLinesTestGenerated extends AbstractJoinLinesTest { } } + @TestMetadata("idea/testData/joinLines/nestedIfs") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class NestedIfs extends AbstractJoinLinesTest { + public void testAllFilesPresentInNestedIfs() throws Exception { + JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/joinLines/nestedIfs"), Pattern.compile("^(.+)\\.kt$"), true); + } + + @TestMetadata("BlockBody.kt") + public void testBlockBody() throws Exception { + String fileName = JetTestUtils.navigationMetadata("idea/testData/joinLines/nestedIfs/BlockBody.kt"); + doTest(fileName); + } + + @TestMetadata("ComplexCondition1.kt") + public void testComplexCondition1() throws Exception { + String fileName = JetTestUtils.navigationMetadata("idea/testData/joinLines/nestedIfs/ComplexCondition1.kt"); + doTest(fileName); + } + + @TestMetadata("ComplexCondition2.kt") + public void testComplexCondition2() throws Exception { + String fileName = JetTestUtils.navigationMetadata("idea/testData/joinLines/nestedIfs/ComplexCondition2.kt"); + doTest(fileName); + } + + @TestMetadata("Simple.kt") + public void testSimple() throws Exception { + String fileName = JetTestUtils.navigationMetadata("idea/testData/joinLines/nestedIfs/Simple.kt"); + doTest(fileName); + } + } + @TestMetadata("idea/testData/joinLines/removeBraces") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class)