Replace intentions for folding/unfolding operations

This commit is contained in:
Alexey Sedunov
2013-04-12 15:50:37 +04:00
parent 51658c65de
commit 02e9f174f0
12 changed files with 89 additions and 254 deletions
+2 -2
View File
@@ -292,12 +292,12 @@
</intentionAction>
<intentionAction>
<className>org.jetbrains.jet.plugin.codeInsight.codeTransformations.IfStatementWithAssignmentsToExpressionIntention</className>
<className>org.jetbrains.jet.plugin.codeInsight.codeTransformations.branchedTransformations.FoldBranchedExpressionIntention</className>
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.jet.plugin.codeInsight.codeTransformations.AssignmentWithIfExpressionToStatementIntention</className>
<className>org.jetbrains.jet.plugin.codeInsight.codeTransformations.branchedTransformations.UnfoldBranchedExpressionIntention</className>
<category>Kotlin</category>
</intentionAction>
@@ -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);
}
}
@@ -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<JetExpression> getIfExpressionOutcomes(@NotNull JetElement root) {
return root.accept(
new JetVisitor<List<JetExpression>, List<JetExpression>>() {
@Override
public List<JetExpression> visitExpression(JetExpression expression, List<JetExpression> data) {
data.add(expression);
return data;
}
@Override
public List<JetExpression> visitBlockExpression(
JetBlockExpression expression, List<JetExpression> 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<JetExpression> visitIfExpression(
JetIfExpression expression, List<JetExpression> data) {
if (expression.getThen() != null) {
expression.getThen().accept(this, data);
}
if (expression.getElse() != null) {
expression.getElse().accept(this, data);
}
return data;
}
},
new ArrayList<JetExpression>()
);
}
private static Boolean checkAllIfExpressionsAreComplete(@NotNull JetElement root) {
return root.accept(
new JetVisitor<Boolean, Boolean>() {
@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<JetExpression> 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<JetExpression> 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<JetExpression> 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() {
}
}
@@ -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);
}
}
@@ -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);
}
}
@@ -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 {