From 02e9f174f09da023c180299e3a4aa53a03845ccc Mon Sep 17 00:00:00 2001 From: Alexey Sedunov Date: Fri, 12 Apr 2013 15:50:37 +0400 Subject: [PATCH] Replace intentions for folding/unfolding operations --- .../after.kt.template | 0 .../before.kt.template | 0 .../description.html | 0 .../after.kt.template | 0 .../before.kt.template | 0 .../description.html | 0 idea/src/META-INF/plugin.xml | 4 +- ...tWithIfExpressionToStatementIntention.java | 72 -------- .../CodeTransformationUtils.java | 157 ------------------ .../FoldBranchedExpressionIntention.java} | 38 ++--- .../UnfoldBranchedExpressionIntention.java | 62 +++++++ .../AbstractCodeTransformationTest.java | 10 +- 12 files changed, 89 insertions(+), 254 deletions(-) rename idea/resources/intentionDescriptions/{IfStatementWithAssignmentsToExpressionIntention => FoldBranchedExpressionIntention}/after.kt.template (100%) rename idea/resources/intentionDescriptions/{IfStatementWithAssignmentsToExpressionIntention => FoldBranchedExpressionIntention}/before.kt.template (100%) rename idea/resources/intentionDescriptions/{IfStatementWithAssignmentsToExpressionIntention => FoldBranchedExpressionIntention}/description.html (100%) rename idea/resources/intentionDescriptions/{AssignmentWithIfExpressionToStatementIntention => UnfoldBranchedExpressionIntention}/after.kt.template (100%) rename idea/resources/intentionDescriptions/{AssignmentWithIfExpressionToStatementIntention => UnfoldBranchedExpressionIntention}/before.kt.template (100%) rename idea/resources/intentionDescriptions/{AssignmentWithIfExpressionToStatementIntention => UnfoldBranchedExpressionIntention}/description.html (100%) delete mode 100644 idea/src/org/jetbrains/jet/plugin/codeInsight/codeTransformations/AssignmentWithIfExpressionToStatementIntention.java delete mode 100644 idea/src/org/jetbrains/jet/plugin/codeInsight/codeTransformations/CodeTransformationUtils.java rename idea/src/org/jetbrains/jet/plugin/codeInsight/codeTransformations/{IfStatementWithAssignmentsToExpressionIntention.java => branchedTransformations/FoldBranchedExpressionIntention.java} (51%) create mode 100644 idea/src/org/jetbrains/jet/plugin/codeInsight/codeTransformations/branchedTransformations/UnfoldBranchedExpressionIntention.java diff --git a/idea/resources/intentionDescriptions/IfStatementWithAssignmentsToExpressionIntention/after.kt.template b/idea/resources/intentionDescriptions/FoldBranchedExpressionIntention/after.kt.template similarity index 100% rename from idea/resources/intentionDescriptions/IfStatementWithAssignmentsToExpressionIntention/after.kt.template rename to idea/resources/intentionDescriptions/FoldBranchedExpressionIntention/after.kt.template diff --git a/idea/resources/intentionDescriptions/IfStatementWithAssignmentsToExpressionIntention/before.kt.template b/idea/resources/intentionDescriptions/FoldBranchedExpressionIntention/before.kt.template similarity index 100% rename from idea/resources/intentionDescriptions/IfStatementWithAssignmentsToExpressionIntention/before.kt.template rename to idea/resources/intentionDescriptions/FoldBranchedExpressionIntention/before.kt.template diff --git a/idea/resources/intentionDescriptions/IfStatementWithAssignmentsToExpressionIntention/description.html b/idea/resources/intentionDescriptions/FoldBranchedExpressionIntention/description.html similarity index 100% rename from idea/resources/intentionDescriptions/IfStatementWithAssignmentsToExpressionIntention/description.html rename to idea/resources/intentionDescriptions/FoldBranchedExpressionIntention/description.html diff --git a/idea/resources/intentionDescriptions/AssignmentWithIfExpressionToStatementIntention/after.kt.template b/idea/resources/intentionDescriptions/UnfoldBranchedExpressionIntention/after.kt.template similarity index 100% rename from idea/resources/intentionDescriptions/AssignmentWithIfExpressionToStatementIntention/after.kt.template rename to idea/resources/intentionDescriptions/UnfoldBranchedExpressionIntention/after.kt.template diff --git a/idea/resources/intentionDescriptions/AssignmentWithIfExpressionToStatementIntention/before.kt.template b/idea/resources/intentionDescriptions/UnfoldBranchedExpressionIntention/before.kt.template similarity index 100% rename from idea/resources/intentionDescriptions/AssignmentWithIfExpressionToStatementIntention/before.kt.template rename to idea/resources/intentionDescriptions/UnfoldBranchedExpressionIntention/before.kt.template diff --git a/idea/resources/intentionDescriptions/AssignmentWithIfExpressionToStatementIntention/description.html b/idea/resources/intentionDescriptions/UnfoldBranchedExpressionIntention/description.html similarity index 100% rename from idea/resources/intentionDescriptions/AssignmentWithIfExpressionToStatementIntention/description.html rename to idea/resources/intentionDescriptions/UnfoldBranchedExpressionIntention/description.html diff --git a/idea/src/META-INF/plugin.xml b/idea/src/META-INF/plugin.xml index 190e0d0f799..abc42486aad 100644 --- a/idea/src/META-INF/plugin.xml +++ b/idea/src/META-INF/plugin.xml @@ -292,12 +292,12 @@ - org.jetbrains.jet.plugin.codeInsight.codeTransformations.IfStatementWithAssignmentsToExpressionIntention + org.jetbrains.jet.plugin.codeInsight.codeTransformations.branchedTransformations.FoldBranchedExpressionIntention Kotlin - org.jetbrains.jet.plugin.codeInsight.codeTransformations.AssignmentWithIfExpressionToStatementIntention + org.jetbrains.jet.plugin.codeInsight.codeTransformations.branchedTransformations.UnfoldBranchedExpressionIntention Kotlin diff --git a/idea/src/org/jetbrains/jet/plugin/codeInsight/codeTransformations/AssignmentWithIfExpressionToStatementIntention.java b/idea/src/org/jetbrains/jet/plugin/codeInsight/codeTransformations/AssignmentWithIfExpressionToStatementIntention.java deleted file mode 100644 index fc5722b207c..00000000000 --- a/idea/src/org/jetbrains/jet/plugin/codeInsight/codeTransformations/AssignmentWithIfExpressionToStatementIntention.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Copyright 2010-2013 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.jetbrains.jet.plugin.codeInsight.codeTransformations; - -import com.intellij.codeInsight.intention.impl.BaseIntentionAction; -import com.intellij.openapi.editor.Editor; -import com.intellij.openapi.project.Project; -import com.intellij.psi.PsiElement; -import com.intellij.psi.PsiFile; -import com.intellij.psi.util.PsiTreeUtil; -import com.intellij.util.IncorrectOperationException; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.jetbrains.jet.lang.psi.JetBinaryExpression; -import org.jetbrains.jet.lang.psi.JetElement; -import org.jetbrains.jet.plugin.JetBundle; - -public class AssignmentWithIfExpressionToStatementIntention extends BaseIntentionAction { - public AssignmentWithIfExpressionToStatementIntention() { - setText(JetBundle.message("transform.assignment.with.if.expression.to.statement")); - } - - @NotNull - @Override - public String getFamilyName() { - return JetBundle.message("transform.assignment.with.if.expression.to.statement.family"); - } - - @Nullable - private static JetBinaryExpression getAssignment(@NotNull Editor editor, @NotNull PsiFile file) { - int offset = editor.getCaretModel().getOffset(); - PsiElement element = file.findElementAt(offset); - - while (element != null) { - if (!(element instanceof JetElement)) return null; - - if (CodeTransformationUtils.checkAssignmentWithIfExpression((JetElement) element)) return (JetBinaryExpression)element; - PsiElement parent = PsiTreeUtil.getParentOfType(element, JetBinaryExpression.class, false); - element = (element != parent) ? parent : null; - } - - return null; - } - - @Override - public boolean isAvailable(@NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file) { - return getAssignment(editor, file) != null; - } - - @Override - public void invoke( - @NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file - ) throws IncorrectOperationException { - JetBinaryExpression assignment = getAssignment(editor, file); - assert assignment != null; - CodeTransformationUtils.transformAssignmentWithIfExpressionToStatement(assignment); - } -} diff --git a/idea/src/org/jetbrains/jet/plugin/codeInsight/codeTransformations/CodeTransformationUtils.java b/idea/src/org/jetbrains/jet/plugin/codeInsight/codeTransformations/CodeTransformationUtils.java deleted file mode 100644 index d20b67c5c5a..00000000000 --- a/idea/src/org/jetbrains/jet/plugin/codeInsight/codeTransformations/CodeTransformationUtils.java +++ /dev/null @@ -1,157 +0,0 @@ -/* - * Copyright 2010-2013 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.jetbrains.jet.plugin.codeInsight.codeTransformations; - -import com.intellij.openapi.project.Project; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.jet.lang.psi.*; - -import java.util.ArrayList; -import java.util.List; - -public class CodeTransformationUtils { - private static List getIfExpressionOutcomes(@NotNull JetElement root) { - return root.accept( - new JetVisitor, List>() { - @Override - public List visitExpression(JetExpression expression, List data) { - data.add(expression); - return data; - } - - @Override - public List visitBlockExpression( - JetBlockExpression expression, List data) { - int n = expression.getStatements().size(); - if (n > 0) { - expression.getStatements().get(n - 1).accept(this, data); - } else { - data.add(expression); - } - - return data; - } - - @SuppressWarnings("ConstantConditions") - @Override - public List visitIfExpression( - JetIfExpression expression, List data) { - if (expression.getThen() != null) { - expression.getThen().accept(this, data); - } - if (expression.getElse() != null) { - expression.getElse().accept(this, data); - } - return data; - } - }, - new ArrayList() - ); - } - - private static Boolean checkAllIfExpressionsAreComplete(@NotNull JetElement root) { - return root.accept( - new JetVisitor() { - @Override - public Boolean visitJetElement(JetElement element, Boolean data) { - return data; - } - - @SuppressWarnings("ConstantConditions") - @Override - public Boolean visitIfExpression(JetIfExpression expression, Boolean data) { - if (data && expression.getThen() != null) { - data = expression.getThen().accept(this, data); - } else { - data = false; - } - if (data && expression.getElse() != null) { - data = expression.getElse().accept(this, data); - } else { - data = false; - } - - return data; - } - }, - true - ); - } - - private static boolean checkAllOutcomesAreCompatibleAssignments(@NotNull List outcomes) { - JetExpression lastLhs = null; - for (JetExpression outcome : outcomes) { - if (!JetPsiUtil.isAssignment(outcome)) return false; - - JetExpression currLhs = ((JetBinaryExpression)outcome).getLeft(); - if (!(currLhs instanceof JetSimpleNameExpression)) return false; - - if (lastLhs == null) { - lastLhs = currLhs; - } else if (!lastLhs.getText().equals(currLhs.getText())) return false; - } - - return true; - } - - static boolean checkIfStatementWithAssignments(@NotNull JetIfExpression ifExpression) { - if (ifExpression.getParent() == null) return false; - List outcomes = getIfExpressionOutcomes(ifExpression); - - return !outcomes.isEmpty() && checkAllIfExpressionsAreComplete(ifExpression) && checkAllOutcomesAreCompatibleAssignments(outcomes); - } - - static boolean checkAssignmentWithIfExpression(@NotNull JetElement element) { - if (!JetPsiUtil.isAssignment(element)) return false; - JetBinaryExpression assignment = (JetBinaryExpression)element; - return (assignment.getLeft() instanceof JetSimpleNameExpression) && - (assignment.getRight() instanceof JetIfExpression); - } - - @SuppressWarnings("ConstantConditions") - static void transformIfStatementWithAssignmentsToExpression(@NotNull JetIfExpression ifExpression) { - Project project = ifExpression.getProject(); - List outcomes = getIfExpressionOutcomes(ifExpression); - JetExpression lhs = ((JetBinaryExpression)outcomes.get(0)).getLeft(); - - JetBinaryExpression assignment = JetPsiFactory.createAssignment(project, lhs, ifExpression); - - assignment = (JetBinaryExpression)ifExpression.replace(assignment); - ifExpression = (JetIfExpression)assignment.getRight(); - - for (JetExpression outcome : getIfExpressionOutcomes(ifExpression)) { - outcome.replace(((JetBinaryExpression)outcome).getRight()); - } - } - - @SuppressWarnings("ConstantConditions") - static void transformAssignmentWithIfExpressionToStatement(@NotNull JetBinaryExpression assignment) { - Project project = assignment.getProject(); - String varName = assignment.getLeft().getText(); - JetIfExpression ifExpression = (JetIfExpression)assignment.getRight(); - - ifExpression = (JetIfExpression)assignment.replace(ifExpression); - - for (JetExpression outcome : getIfExpressionOutcomes(ifExpression)) { - JetBinaryExpression localAssignment = JetPsiFactory.createAssignment(project, JetPsiFactory.createExpression(project, varName), outcome); - outcome.replace(localAssignment); - } - } - - private CodeTransformationUtils() { - } -} diff --git a/idea/src/org/jetbrains/jet/plugin/codeInsight/codeTransformations/IfStatementWithAssignmentsToExpressionIntention.java b/idea/src/org/jetbrains/jet/plugin/codeInsight/codeTransformations/branchedTransformations/FoldBranchedExpressionIntention.java similarity index 51% rename from idea/src/org/jetbrains/jet/plugin/codeInsight/codeTransformations/IfStatementWithAssignmentsToExpressionIntention.java rename to idea/src/org/jetbrains/jet/plugin/codeInsight/codeTransformations/branchedTransformations/FoldBranchedExpressionIntention.java index a240d779fdd..8f25d6f40cd 100644 --- a/idea/src/org/jetbrains/jet/plugin/codeInsight/codeTransformations/IfStatementWithAssignmentsToExpressionIntention.java +++ b/idea/src/org/jetbrains/jet/plugin/codeInsight/codeTransformations/branchedTransformations/FoldBranchedExpressionIntention.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.jetbrains.jet.plugin.codeInsight.codeTransformations; +package org.jetbrains.jet.plugin.codeInsight.codeTransformations.branchedTransformations; import com.intellij.codeInsight.intention.impl.BaseIntentionAction; import com.intellij.openapi.editor.Editor; @@ -25,40 +25,40 @@ import com.intellij.psi.util.PsiTreeUtil; import com.intellij.util.IncorrectOperationException; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import org.jetbrains.jet.lang.psi.JetElement; +import org.jetbrains.jet.lang.psi.JetExpression; import org.jetbrains.jet.lang.psi.JetIfExpression; +import org.jetbrains.jet.lang.psi.JetPsiUtil; import org.jetbrains.jet.plugin.JetBundle; -public class IfStatementWithAssignmentsToExpressionIntention extends BaseIntentionAction { - public IfStatementWithAssignmentsToExpressionIntention() { - setText(JetBundle.message("transform.if.statement.with.assignments.to.expression")); +public class FoldBranchedExpressionIntention extends BaseIntentionAction { + public FoldBranchedExpressionIntention() { + setText(JetBundle.message("fold.branched.expression")); } @NotNull @Override public String getFamilyName() { - return JetBundle.message("transform.if.statement.with.assignments.to.expression.family"); + return JetBundle.message("fold.branched.expression.family"); } @Nullable - private static JetIfExpression getOriginalExpression(@NotNull Editor editor, @NotNull PsiFile file) { - int offset = editor.getCaretModel().getOffset(); - PsiElement element = file.findElementAt(offset); - return PsiTreeUtil.getParentOfType(element, JetIfExpression.class, false); + private static JetExpression getTarget(@NotNull Editor editor, @NotNull PsiFile file) { + PsiElement element = file.findElementAt(editor.getCaretModel().getOffset()); + return (JetExpression)JetPsiUtil.getParentByTypeAndPredicate(element, JetElement.class, BranchedFoldingUtils.FOLDABLE_EXPRESSION, false); } @Override - public boolean isAvailable( - @NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file) { - JetIfExpression ifExpression = getOriginalExpression(editor, file); - return (ifExpression != null) && CodeTransformationUtils.checkIfStatementWithAssignments(ifExpression); + public boolean isAvailable(@NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file) { + return getTarget(editor, file) != null; } @Override - public void invoke( - @NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file - ) throws IncorrectOperationException { - JetIfExpression ifExpression = getOriginalExpression(editor, file); - assert ifExpression != null; - CodeTransformationUtils.transformIfStatementWithAssignmentsToExpression(ifExpression); + public void invoke(@NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file) throws IncorrectOperationException { + JetExpression target = getTarget(editor, file); + + assert target != null; + + BranchedFoldingUtils.foldExpression(target); } } diff --git a/idea/src/org/jetbrains/jet/plugin/codeInsight/codeTransformations/branchedTransformations/UnfoldBranchedExpressionIntention.java b/idea/src/org/jetbrains/jet/plugin/codeInsight/codeTransformations/branchedTransformations/UnfoldBranchedExpressionIntention.java new file mode 100644 index 00000000000..61cd20886e5 --- /dev/null +++ b/idea/src/org/jetbrains/jet/plugin/codeInsight/codeTransformations/branchedTransformations/UnfoldBranchedExpressionIntention.java @@ -0,0 +1,62 @@ +/* + * Copyright 2010-2013 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.jet.plugin.codeInsight.codeTransformations.branchedTransformations; + +import com.intellij.codeInsight.intention.impl.BaseIntentionAction; +import com.intellij.openapi.editor.Editor; +import com.intellij.openapi.project.Project; +import com.intellij.psi.PsiElement; +import com.intellij.psi.PsiFile; +import com.intellij.util.IncorrectOperationException; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.jetbrains.jet.lang.psi.JetElement; +import org.jetbrains.jet.lang.psi.JetExpression; +import org.jetbrains.jet.lang.psi.JetPsiUtil; +import org.jetbrains.jet.plugin.JetBundle; + +public class UnfoldBranchedExpressionIntention extends BaseIntentionAction { + public UnfoldBranchedExpressionIntention() { + setText(JetBundle.message("unfold.branched.expression")); + } + + @NotNull + @Override + public String getFamilyName() { + return JetBundle.message("unfold.branched.expression.family"); + } + + @Nullable + private static JetExpression getTarget(@NotNull Editor editor, @NotNull PsiFile file) { + PsiElement element = file.findElementAt(editor.getCaretModel().getOffset()); + return (JetExpression)JetPsiUtil.getParentByTypeAndPredicate(element, JetElement.class, BranchedUnfoldingUtils.UNFOLDABLE_EXPRESSION, false); + } + + @Override + public boolean isAvailable(@NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file) { + return getTarget(editor, file) != null; + } + + @Override + public void invoke(@NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file) throws IncorrectOperationException { + JetExpression target = getTarget(editor, file); + + assert target != null; + + BranchedUnfoldingUtils.unfoldExpression(target); + } +} diff --git a/idea/tests/org/jetbrains/jet/plugin/codeInsight/codeTransformations/AbstractCodeTransformationTest.java b/idea/tests/org/jetbrains/jet/plugin/codeInsight/codeTransformations/AbstractCodeTransformationTest.java index 56e71ad86a9..4c904907a81 100644 --- a/idea/tests/org/jetbrains/jet/plugin/codeInsight/codeTransformations/AbstractCodeTransformationTest.java +++ b/idea/tests/org/jetbrains/jet/plugin/codeInsight/codeTransformations/AbstractCodeTransformationTest.java @@ -21,16 +21,18 @@ import com.intellij.openapi.util.io.FileUtil; import com.intellij.testFramework.LightCodeInsightTestCase; import org.jetbrains.annotations.NotNull; import org.jetbrains.jet.InTextDirectivesUtils; +import org.jetbrains.jet.plugin.codeInsight.codeTransformations.branchedTransformations.FoldBranchedExpressionIntention; +import org.jetbrains.jet.plugin.codeInsight.codeTransformations.branchedTransformations.UnfoldBranchedExpressionIntention; import java.io.File; public abstract class AbstractCodeTransformationTest extends LightCodeInsightTestCase { - public void doTestIfStatementWithAssignmentsToExpression(@NotNull String path) throws Exception { - doTest(path, new IfStatementWithAssignmentsToExpressionIntention()); + public void doTestBranchedFolding(@NotNull String path) throws Exception { + doTest(path, new FoldBranchedExpressionIntention()); } - public void doTestAssignmentWithIfExpressionToStatement(@NotNull String path) throws Exception { - doTest(path, new AssignmentWithIfExpressionToStatementIntention()); + public void doTestBranchedUnfolding(@NotNull String path) throws Exception { + doTest(path, new UnfoldBranchedExpressionIntention()); } public void doTestRemoveUnnecessaryParentheses(@NotNull String path) throws Exception {