KT-5927 Join Lines could merge nested if's
#KT-5927 Fixed
This commit is contained in:
@@ -300,12 +300,11 @@ public class JetPsiFactory(private val project: Project) {
|
|||||||
return createExpression("$" + fieldName)
|
return createExpression("$" + fieldName)
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun createBinaryExpression(lhs: String, op: String, rhs: String): JetBinaryExpression {
|
public fun createBinaryExpression(lhs: JetExpression, op: String, rhs: JetExpression): JetBinaryExpression {
|
||||||
return createExpression(lhs + " " + op + " " + rhs) as JetBinaryExpression
|
val expression = createExpression("a $op b") as JetBinaryExpression
|
||||||
}
|
expression.getLeft().replace(lhs)
|
||||||
|
expression.getRight().replace(rhs)
|
||||||
public fun createBinaryExpression(lhs: JetExpression?, op: String, rhs: JetExpression?): JetBinaryExpression {
|
return expression
|
||||||
return createBinaryExpression(JetPsiUtil.getText(lhs), op, JetPsiUtil.getText(rhs))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun createTypeCodeFragment(text: String, context: PsiElement?): JetTypeCodeFragment {
|
public fun createTypeCodeFragment(text: String, context: PsiElement?): JetTypeCodeFragment {
|
||||||
|
|||||||
@@ -24,6 +24,8 @@ import org.jetbrains.jet.lexer.JetTokens
|
|||||||
import org.jetbrains.jet.lang.psi.JetBlockExpression
|
import org.jetbrains.jet.lang.psi.JetBlockExpression
|
||||||
import org.jetbrains.jet.lang.psi.JetContainerNode
|
import org.jetbrains.jet.lang.psi.JetContainerNode
|
||||||
import org.jetbrains.jet.lang.psi.JetWhenEntry
|
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 {
|
public class JoinBlockIntoSingleStatementHandler : JoinRawLinesHandlerDelegate {
|
||||||
override fun tryJoinRawLines(document: Document, file: PsiFile, start: Int, end: Int): Int {
|
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 (parent !is JetContainerNode && parent !is JetWhenEntry) return -1
|
||||||
if (block.getNode().getChildren(JetTokens.COMMENTS).isNotEmpty()) return -1 // otherwise we will loose comments
|
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)
|
val newStatement = block.replace(statement)
|
||||||
return newStatement.getTextRange()!!.getStartOffset()
|
return newStatement.getTextRange()!!.getStartOffset()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
fun foo() {
|
||||||
|
<caret>if (a) {
|
||||||
|
if (b) {
|
||||||
|
foo()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
fun foo() {
|
||||||
|
if (a && b) <caret>{
|
||||||
|
foo()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
fun foo() {
|
||||||
|
<caret>if (a && b) {
|
||||||
|
if (c && d) foo()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
fun foo() {
|
||||||
|
if (a && b && c && d) <caret>foo()
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
fun foo() {
|
||||||
|
<caret>if (a && b) {
|
||||||
|
if (c || d) foo()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
fun foo() {
|
||||||
|
if (a && b && (c || d)) <caret>foo()
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
fun foo() {
|
||||||
|
<caret>if (a) {
|
||||||
|
if (b) foo()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
fun foo() {
|
||||||
|
if (a && b) <caret>foo()
|
||||||
|
}
|
||||||
+34
-1
@@ -30,7 +30,7 @@ import java.util.regex.Pattern;
|
|||||||
@SuppressWarnings("all")
|
@SuppressWarnings("all")
|
||||||
@TestMetadata("idea/testData/joinLines")
|
@TestMetadata("idea/testData/joinLines")
|
||||||
@TestDataPath("$PROJECT_ROOT")
|
@TestDataPath("$PROJECT_ROOT")
|
||||||
@InnerTestClasses({JoinLinesTestGenerated.DeclarationAndAssignment.class, JoinLinesTestGenerated.RemoveBraces.class})
|
@InnerTestClasses({JoinLinesTestGenerated.DeclarationAndAssignment.class, JoinLinesTestGenerated.NestedIfs.class, JoinLinesTestGenerated.RemoveBraces.class})
|
||||||
@RunWith(JUnit3RunnerWithInners.class)
|
@RunWith(JUnit3RunnerWithInners.class)
|
||||||
public class JoinLinesTestGenerated extends AbstractJoinLinesTest {
|
public class JoinLinesTestGenerated extends AbstractJoinLinesTest {
|
||||||
public void testAllFilesPresentInJoinLines() throws Exception {
|
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")
|
@TestMetadata("idea/testData/joinLines/removeBraces")
|
||||||
@TestDataPath("$PROJECT_ROOT")
|
@TestDataPath("$PROJECT_ROOT")
|
||||||
@RunWith(JUnit3RunnerWithInners.class)
|
@RunWith(JUnit3RunnerWithInners.class)
|
||||||
|
|||||||
Reference in New Issue
Block a user