Add support of comments in asymmetric returns

This commit is contained in:
Alexey Sedunov
2013-04-15 16:01:37 +04:00
parent 8757e3763b
commit d5e78c3bd5
5 changed files with 239 additions and 118 deletions
@@ -20,8 +20,10 @@ import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.Lists;
import com.intellij.lang.ASTNode;
import com.intellij.psi.PsiComment;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiWhiteSpace;
import com.intellij.psi.impl.CheckUtil;
import com.intellij.psi.impl.source.codeStyle.CodeEditUtil;
import com.intellij.psi.tree.IElementType;
@@ -691,6 +693,10 @@ public class JetPsiUtil {
return (elseCount == 1);
}
public static PsiElement skipTrailingWhitespacesAndComments(PsiElement element) {
return PsiTreeUtil.skipSiblingsForward(element, PsiWhiteSpace.class, PsiComment.class);
}
public static final Predicate<JetElement> ANY_JET_ELEMENT = new Predicate<JetElement>() {
@Override
public boolean apply(@Nullable JetElement input) {
@@ -19,8 +19,6 @@ package org.jetbrains.jet.plugin.codeInsight.codeTransformations.branchedTransfo
import com.google.common.base.Predicate;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiWhiteSpace;
import com.intellij.psi.util.PsiTreeUtil;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.psi.*;
@@ -136,7 +134,7 @@ public class BranchedFoldingUtils {
return false;
}
PsiElement nextElement = PsiTreeUtil.skipSiblingsForward(ifExpression, PsiWhiteSpace.class);
PsiElement nextElement = JetPsiUtil.skipTrailingWhitespacesAndComments(ifExpression);
return (nextElement instanceof JetExpression) && checkAndGetFoldableBranchedReturn((JetExpression)nextElement) != null;
}
@@ -218,7 +216,7 @@ public class BranchedFoldingUtils {
JetExpression condition = ifExpression.getCondition();
JetExpression thenRoot = ifExpression.getThen();
JetExpression elseRoot = (JetExpression)PsiTreeUtil.skipSiblingsForward(ifExpression, PsiWhiteSpace.class);
JetExpression elseRoot = (JetExpression)JetPsiUtil.skipTrailingWhitespacesAndComments(ifExpression);
assert condition != null;
assert thenRoot != null;
@@ -228,7 +226,7 @@ public class BranchedFoldingUtils {
JetReturnExpression newReturnExpr = JetPsiFactory.createReturn(project, newIfExpr);
newReturnExpr = (JetReturnExpression) ifExpression.replace(newReturnExpr);
JetReturnExpression oldReturn = (JetReturnExpression)PsiTreeUtil.skipSiblingsForward(newReturnExpr, PsiWhiteSpace.class);
JetReturnExpression oldReturn = (JetReturnExpression)JetPsiUtil.skipTrailingWhitespacesAndComments(newReturnExpr);
assert oldReturn != null;
@@ -0,0 +1,5 @@
fun test(n: Int): String {
<caret>if (n == 1) return "one"
// comment
return "two"
}
@@ -0,0 +1,4 @@
fun test(n: Int): String {
<caret>return if (n == 1) "one" else "two"
// comment
}
@@ -30,142 +30,250 @@ import org.jetbrains.jet.plugin.codeInsight.codeTransformations.AbstractCodeTran
/** This class is generated by {@link org.jetbrains.jet.generators.tests.GenerateTests}. DO NOT MODIFY MANUALLY */
@SuppressWarnings("all")
@InnerTestClasses({CodeTransformationsTestGenerated.IfStatementWithAssignmentsToExpression.class, CodeTransformationsTestGenerated.AssignmentWithIfExpressionToStatement.class})
@InnerTestClasses({CodeTransformationsTestGenerated.Folding.class, CodeTransformationsTestGenerated.Unfolding.class})
public class CodeTransformationsTestGenerated extends AbstractCodeTransformationTest {
@TestMetadata("idea/testData/codeInsight/codeTransformations/ifStatementWithAssignmentsToExpression")
public static class IfStatementWithAssignmentsToExpression extends AbstractCodeTransformationTest {
public void testAllFilesPresentInIfStatementWithAssignmentsToExpression() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/codeInsight/codeTransformations/ifStatementWithAssignmentsToExpression"), Pattern.compile("^(.+)\\.kt$"), true);
@TestMetadata("idea/testData/codeInsight/codeTransformations/branched/folding")
@InnerTestClasses({Folding.Assignment.class, Folding.AsymmetricReturn.class, Folding.Return.class})
public static class Folding extends AbstractCodeTransformationTest {
public void testAllFilesPresentInFolding() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/codeInsight/codeTransformations/branched/folding"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("innerIfTransformed.kt")
public void testInnerIfTransformed() throws Exception {
doTestIfStatementWithAssignmentsToExpression("idea/testData/codeInsight/codeTransformations/ifStatementWithAssignmentsToExpression/innerIfTransformed.kt");
@TestMetadata("idea/testData/codeInsight/codeTransformations/branched/folding/assignment")
public static class Assignment extends AbstractCodeTransformationTest {
public void testAllFilesPresentInAssignment() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/codeInsight/codeTransformations/branched/folding/assignment"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("innerIfTransformed.kt")
public void testInnerIfTransformed() throws Exception {
doTestBranchedFolding("idea/testData/codeInsight/codeTransformations/branched/folding/assignment/innerIfTransformed.kt");
}
@TestMetadata("innerWhenTransformed.kt")
public void testInnerWhenTransformed() throws Exception {
doTestBranchedFolding("idea/testData/codeInsight/codeTransformations/branched/folding/assignment/innerWhenTransformed.kt");
}
@TestMetadata("simpleIf.kt")
public void testSimpleIf() throws Exception {
doTestBranchedFolding("idea/testData/codeInsight/codeTransformations/branched/folding/assignment/simpleIf.kt");
}
@TestMetadata("simpleIfWithAugmentedAssignment.kt")
public void testSimpleIfWithAugmentedAssignment() throws Exception {
doTestBranchedFolding("idea/testData/codeInsight/codeTransformations/branched/folding/assignment/simpleIfWithAugmentedAssignment.kt");
}
@TestMetadata("simpleIfWithBlocks.kt")
public void testSimpleIfWithBlocks() throws Exception {
doTestBranchedFolding("idea/testData/codeInsight/codeTransformations/branched/folding/assignment/simpleIfWithBlocks.kt");
}
@TestMetadata("simpleIfWithShadowedVar.kt")
public void testSimpleIfWithShadowedVar() throws Exception {
doTestBranchedFolding("idea/testData/codeInsight/codeTransformations/branched/folding/assignment/simpleIfWithShadowedVar.kt");
}
@TestMetadata("simpleIfWithUnmatchedAssignmentOps.kt")
public void testSimpleIfWithUnmatchedAssignmentOps() throws Exception {
doTestBranchedFolding("idea/testData/codeInsight/codeTransformations/branched/folding/assignment/simpleIfWithUnmatchedAssignmentOps.kt");
}
@TestMetadata("simpleIfWithUnmatchedAssignments.kt")
public void testSimpleIfWithUnmatchedAssignments() throws Exception {
doTestBranchedFolding("idea/testData/codeInsight/codeTransformations/branched/folding/assignment/simpleIfWithUnmatchedAssignments.kt");
}
@TestMetadata("simpleIfWithoutElse.kt")
public void testSimpleIfWithoutElse() throws Exception {
doTestBranchedFolding("idea/testData/codeInsight/codeTransformations/branched/folding/assignment/simpleIfWithoutElse.kt");
}
@TestMetadata("simpleIfWithoutTerminatingAssignment.kt")
public void testSimpleIfWithoutTerminatingAssignment() throws Exception {
doTestBranchedFolding("idea/testData/codeInsight/codeTransformations/branched/folding/assignment/simpleIfWithoutTerminatingAssignment.kt");
}
@TestMetadata("simpleWhen.kt")
public void testSimpleWhen() throws Exception {
doTestBranchedFolding("idea/testData/codeInsight/codeTransformations/branched/folding/assignment/simpleWhen.kt");
}
@TestMetadata("simpleWhenWithBlocks.kt")
public void testSimpleWhenWithBlocks() throws Exception {
doTestBranchedFolding("idea/testData/codeInsight/codeTransformations/branched/folding/assignment/simpleWhenWithBlocks.kt");
}
@TestMetadata("simpleWhenWithShadowedVar.kt")
public void testSimpleWhenWithShadowedVar() throws Exception {
doTestBranchedFolding("idea/testData/codeInsight/codeTransformations/branched/folding/assignment/simpleWhenWithShadowedVar.kt");
}
@TestMetadata("simpleWhenWithUnmatchedAssignments.kt")
public void testSimpleWhenWithUnmatchedAssignments() throws Exception {
doTestBranchedFolding("idea/testData/codeInsight/codeTransformations/branched/folding/assignment/simpleWhenWithUnmatchedAssignments.kt");
}
@TestMetadata("simpleWhenWithoutTerminatingAssignment.kt")
public void testSimpleWhenWithoutTerminatingAssignment() throws Exception {
doTestBranchedFolding("idea/testData/codeInsight/codeTransformations/branched/folding/assignment/simpleWhenWithoutTerminatingAssignment.kt");
}
}
@TestMetadata("nestedIfs.kt")
public void testNestedIfs() throws Exception {
doTestIfStatementWithAssignmentsToExpression("idea/testData/codeInsight/codeTransformations/ifStatementWithAssignmentsToExpression/nestedIfs.kt");
@TestMetadata("idea/testData/codeInsight/codeTransformations/branched/folding/asymmetricReturn")
public static class AsymmetricReturn extends AbstractCodeTransformationTest {
public void testAllFilesPresentInAsymmetricReturn() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/codeInsight/codeTransformations/branched/folding/asymmetricReturn"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("simpleIf.kt")
public void testSimpleIf() throws Exception {
doTestBranchedFolding("idea/testData/codeInsight/codeTransformations/branched/folding/asymmetricReturn/simpleIf.kt");
}
@TestMetadata("simpleIfWithBlocks.kt")
public void testSimpleIfWithBlocks() throws Exception {
doTestBranchedFolding("idea/testData/codeInsight/codeTransformations/branched/folding/asymmetricReturn/simpleIfWithBlocks.kt");
}
@TestMetadata("simpleIfWithComments.kt")
public void testSimpleIfWithComments() throws Exception {
doTestBranchedFolding("idea/testData/codeInsight/codeTransformations/branched/folding/asymmetricReturn/simpleIfWithComments.kt");
}
}
@TestMetadata("simpleIf.kt")
public void testSimpleIf() throws Exception {
doTestIfStatementWithAssignmentsToExpression("idea/testData/codeInsight/codeTransformations/ifStatementWithAssignmentsToExpression/simpleIf.kt");
@TestMetadata("idea/testData/codeInsight/codeTransformations/branched/folding/return")
public static class Return extends AbstractCodeTransformationTest {
public void testAllFilesPresentInReturn() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/codeInsight/codeTransformations/branched/folding/return"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("innerIfTransformed.kt")
public void testInnerIfTransformed() throws Exception {
doTestBranchedFolding("idea/testData/codeInsight/codeTransformations/branched/folding/return/innerIfTransformed.kt");
}
@TestMetadata("innerWhenTransformed.kt")
public void testInnerWhenTransformed() throws Exception {
doTestBranchedFolding("idea/testData/codeInsight/codeTransformations/branched/folding/return/innerWhenTransformed.kt");
}
@TestMetadata("simpleIf.kt")
public void testSimpleIf() throws Exception {
doTestBranchedFolding("idea/testData/codeInsight/codeTransformations/branched/folding/return/simpleIf.kt");
}
@TestMetadata("simpleIfWithBlocks.kt")
public void testSimpleIfWithBlocks() throws Exception {
doTestBranchedFolding("idea/testData/codeInsight/codeTransformations/branched/folding/return/simpleIfWithBlocks.kt");
}
@TestMetadata("simpleWhen.kt")
public void testSimpleWhen() throws Exception {
doTestBranchedFolding("idea/testData/codeInsight/codeTransformations/branched/folding/return/simpleWhen.kt");
}
@TestMetadata("simpleWhenWithBlocks.kt")
public void testSimpleWhenWithBlocks() throws Exception {
doTestBranchedFolding("idea/testData/codeInsight/codeTransformations/branched/folding/return/simpleWhenWithBlocks.kt");
}
}
@TestMetadata("simpleIfWithoutElse.kt")
public void testSimpleIfWithoutElse() throws Exception {
doTestIfStatementWithAssignmentsToExpression("idea/testData/codeInsight/codeTransformations/ifStatementWithAssignmentsToExpression/simpleIfWithoutElse.kt");
public static Test innerSuite() {
TestSuite suite = new TestSuite("Folding");
suite.addTestSuite(Folding.class);
suite.addTestSuite(Assignment.class);
suite.addTestSuite(AsymmetricReturn.class);
suite.addTestSuite(Return.class);
return suite;
}
@TestMetadata("simpleIfWithoutTerminatingAssignment.kt")
public void testSimpleIfWithoutTerminatingAssignment() throws Exception {
doTestIfStatementWithAssignmentsToExpression("idea/testData/codeInsight/codeTransformations/ifStatementWithAssignmentsToExpression/simpleIfWithoutTerminatingAssignment.kt");
}
}
@TestMetadata("idea/testData/codeInsight/codeTransformations/assignmentWithIfExpressionToStatement")
public static class AssignmentWithIfExpressionToStatement extends AbstractCodeTransformationTest {
public void testAllFilesPresentInAssignmentWithIfExpressionToStatement() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/codeInsight/codeTransformations/assignmentWithIfExpressionToStatement"), Pattern.compile("^(.+)\\.kt$"), true);
@TestMetadata("idea/testData/codeInsight/codeTransformations/branched/unfolding")
@InnerTestClasses({Unfolding.Assignment.class, Unfolding.Return.class})
public static class Unfolding extends AbstractCodeTransformationTest {
public void testAllFilesPresentInUnfolding() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/codeInsight/codeTransformations/branched/unfolding"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("innerIfTransformed.kt")
public void testInnerIfTransformed() throws Exception {
doTestAssignmentWithIfExpressionToStatement("idea/testData/codeInsight/codeTransformations/assignmentWithIfExpressionToStatement/innerIfTransformed.kt");
@TestMetadata("idea/testData/codeInsight/codeTransformations/branched/unfolding/assignment")
public static class Assignment extends AbstractCodeTransformationTest {
public void testAllFilesPresentInAssignment() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/codeInsight/codeTransformations/branched/unfolding/assignment"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("innerIfTransformed.kt")
public void testInnerIfTransformed() throws Exception {
doTestBranchedUnfolding("idea/testData/codeInsight/codeTransformations/branched/unfolding/assignment/innerIfTransformed.kt");
}
@TestMetadata("nestedIfs.kt")
public void testNestedIfs() throws Exception {
doTestBranchedUnfolding("idea/testData/codeInsight/codeTransformations/branched/unfolding/assignment/nestedIfs.kt");
}
@TestMetadata("simpleIf.kt")
public void testSimpleIf() throws Exception {
doTestBranchedUnfolding("idea/testData/codeInsight/codeTransformations/branched/unfolding/assignment/simpleIf.kt");
}
@TestMetadata("simpleIfWithAugmentedAssignment.kt")
public void testSimpleIfWithAugmentedAssignment() throws Exception {
doTestBranchedUnfolding("idea/testData/codeInsight/codeTransformations/branched/unfolding/assignment/simpleIfWithAugmentedAssignment.kt");
}
@TestMetadata("simpleIfWithBlocks.kt")
public void testSimpleIfWithBlocks() throws Exception {
doTestBranchedUnfolding("idea/testData/codeInsight/codeTransformations/branched/unfolding/assignment/simpleIfWithBlocks.kt");
}
@TestMetadata("simpleIfWithoutAssignment.kt")
public void testSimpleIfWithoutAssignment() throws Exception {
doTestBranchedUnfolding("idea/testData/codeInsight/codeTransformations/branched/unfolding/assignment/simpleIfWithoutAssignment.kt");
}
}
@TestMetadata("nestedIfs.kt")
public void testNestedIfs() throws Exception {
doTestAssignmentWithIfExpressionToStatement("idea/testData/codeInsight/codeTransformations/assignmentWithIfExpressionToStatement/nestedIfs.kt");
@TestMetadata("idea/testData/codeInsight/codeTransformations/branched/unfolding/return")
public static class Return extends AbstractCodeTransformationTest {
public void testAllFilesPresentInReturn() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/codeInsight/codeTransformations/branched/unfolding/return"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("innerIfTransformed.kt")
public void testInnerIfTransformed() throws Exception {
doTestBranchedUnfolding("idea/testData/codeInsight/codeTransformations/branched/unfolding/return/innerIfTransformed.kt");
}
@TestMetadata("simpleIf.kt")
public void testSimpleIf() throws Exception {
doTestBranchedUnfolding("idea/testData/codeInsight/codeTransformations/branched/unfolding/return/simpleIf.kt");
}
@TestMetadata("simpleIfWithBlocks.kt")
public void testSimpleIfWithBlocks() throws Exception {
doTestBranchedUnfolding("idea/testData/codeInsight/codeTransformations/branched/unfolding/return/simpleIfWithBlocks.kt");
}
}
@TestMetadata("simpleIf.kt")
public void testSimpleIf() throws Exception {
doTestAssignmentWithIfExpressionToStatement("idea/testData/codeInsight/codeTransformations/assignmentWithIfExpressionToStatement/simpleIf.kt");
public static Test innerSuite() {
TestSuite suite = new TestSuite("Unfolding");
suite.addTestSuite(Unfolding.class);
suite.addTestSuite(Assignment.class);
suite.addTestSuite(Return.class);
return suite;
}
@TestMetadata("simpleIfWithoutAssignment.kt")
public void testSimpleIfWithoutAssignment() throws Exception {
doTestAssignmentWithIfExpressionToStatement("idea/testData/codeInsight/codeTransformations/assignmentWithIfExpressionToStatement/simpleIfWithoutAssignment.kt");
}
}
@TestMetadata("idea/testData/codeInsight/codeTransformations/removeUnnecessaryParentheses")
public static class RemoveUnnecessaryParentheses extends AbstractCodeTransformationTest {
public void testAllFilesPresentInRemoveUnnecessaryParentheses() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/codeInsight/codeTransformations/removeUnnecessaryParentheses"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("necessaryParentheses1.kt")
public void testNecessaryParentheses1() throws Exception {
doTestRemoveUnnecessaryParentheses("idea/testData/codeInsight/codeTransformations/removeUnnecessaryParentheses/necessaryParentheses1.kt");
}
@TestMetadata("necessaryParentheses2.kt")
public void testNecessaryParentheses2() throws Exception {
doTestRemoveUnnecessaryParentheses("idea/testData/codeInsight/codeTransformations/removeUnnecessaryParentheses/necessaryParentheses2.kt");
}
@TestMetadata("necessaryParentheses3.kt")
public void testNecessaryParentheses3() throws Exception {
doTestRemoveUnnecessaryParentheses("idea/testData/codeInsight/codeTransformations/removeUnnecessaryParentheses/necessaryParentheses3.kt");
}
@TestMetadata("necessaryParentheses4.kt")
public void testNecessaryParentheses4() throws Exception {
doTestRemoveUnnecessaryParentheses("idea/testData/codeInsight/codeTransformations/removeUnnecessaryParentheses/necessaryParentheses4.kt");
}
@TestMetadata("necessaryParentheses5.kt")
public void testNecessaryParentheses5() throws Exception {
doTestRemoveUnnecessaryParentheses("idea/testData/codeInsight/codeTransformations/removeUnnecessaryParentheses/necessaryParentheses5.kt");
}
@TestMetadata("unnecessaryParentheses1.kt")
public void testUnnecessaryParentheses1() throws Exception {
doTestRemoveUnnecessaryParentheses("idea/testData/codeInsight/codeTransformations/removeUnnecessaryParentheses/unnecessaryParentheses1.kt");
}
@TestMetadata("unnecessaryParentheses2.kt")
public void testUnnecessaryParentheses2() throws Exception {
doTestRemoveUnnecessaryParentheses("idea/testData/codeInsight/codeTransformations/removeUnnecessaryParentheses/unnecessaryParentheses2.kt");
}
@TestMetadata("unnecessaryParentheses3.kt")
public void testUnnecessaryParentheses3() throws Exception {
doTestRemoveUnnecessaryParentheses("idea/testData/codeInsight/codeTransformations/removeUnnecessaryParentheses/unnecessaryParentheses3.kt");
}
@TestMetadata("unnecessaryParentheses4.kt")
public void testUnnecessaryParentheses4() throws Exception {
doTestRemoveUnnecessaryParentheses("idea/testData/codeInsight/codeTransformations/removeUnnecessaryParentheses/unnecessaryParentheses4.kt");
}
@TestMetadata("unnecessaryParentheses5.kt")
public void testUnnecessaryParentheses5() throws Exception {
doTestRemoveUnnecessaryParentheses("idea/testData/codeInsight/codeTransformations/removeUnnecessaryParentheses/unnecessaryParentheses5.kt");
}
@TestMetadata("unnecessaryParentheses6.kt")
public void testUnnecessaryParentheses6() throws Exception {
doTestRemoveUnnecessaryParentheses("idea/testData/codeInsight/codeTransformations/removeUnnecessaryParentheses/unnecessaryParentheses6.kt");
}
@TestMetadata("unnecessaryParentheses7.kt")
public void testUnnecessaryParentheses7() throws Exception {
doTestRemoveUnnecessaryParentheses("idea/testData/codeInsight/codeTransformations/removeUnnecessaryParentheses/unnecessaryParentheses7.kt");
}
}
public static Test suite() {
TestSuite suite = new TestSuite("CodeTransformationsTestGenerated");
suite.addTestSuite(IfStatementWithAssignmentsToExpression.class);
suite.addTestSuite(AssignmentWithIfExpressionToStatement.class);
suite.addTestSuite(RemoveUnnecessaryParentheses.class);
suite.addTest(Folding.innerSuite());
suite.addTest(Unfolding.innerSuite());
return suite;
}
}