push condition inside call action (IDEA-88814)

This commit is contained in:
anna
2012-07-17 12:09:21 +02:00
parent f21c22fd94
commit 6aaad412f0
10 changed files with 191 additions and 0 deletions

View File

@@ -0,0 +1,110 @@
/*
* Copyright 2000-2012 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 com.intellij.codeInsight.intention.impl;
import com.intellij.codeInsight.CodeInsightUtilBase;
import com.intellij.codeInsight.PsiEquivalenceUtil;
import com.intellij.codeInsight.intention.PsiElementBaseIntentionAction;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.codeStyle.CodeStyleManager;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
public class PushConditionInCallAction extends PsiElementBaseIntentionAction {
@Override
@NotNull
public String getFamilyName() {
return "Push condition inside call";
}
@Override
public boolean isAvailable(@NotNull Project project, Editor editor, @NotNull PsiElement element) {
if (element instanceof PsiCompiledElement) return false;
if (!element.getManager().isInProject(element)) return false;
if (!(element instanceof PsiJavaToken && ((PsiJavaToken)element).getTokenType() == JavaTokenType.QUEST)) return false;
final PsiConditionalExpression conditionalExpression = PsiTreeUtil.getParentOfType(element, PsiConditionalExpression.class);
if (conditionalExpression == null) return false;
final PsiExpression thenExpression = conditionalExpression.getThenExpression();
if (!(thenExpression instanceof PsiCallExpression)) return false;
final PsiMethod thenMethod = ((PsiCallExpression)thenExpression).resolveMethod();
final PsiExpressionList thenArgsList = ((PsiCallExpression)thenExpression).getArgumentList();
if (thenArgsList == null) return false;
final PsiExpression[] thenExpressions = thenArgsList.getExpressions();
final PsiExpression elseExpression = conditionalExpression.getElseExpression();
if (!(elseExpression instanceof PsiCallExpression)) return false;
final PsiMethod elseMethod = ((PsiCallExpression)elseExpression).resolveMethod();
final PsiExpressionList elseArgsList = ((PsiCallExpression)elseExpression).getArgumentList();
if (elseArgsList == null) return false;
final PsiExpression[] elseExpressions = elseArgsList.getExpressions();
if (thenMethod != elseMethod || thenMethod == null) return false;
if (thenExpressions.length != elseExpressions.length) return false;
PsiExpression tExpr = null;
PsiExpression eExpr = null;
for (int i = 0; i < thenExpressions.length; i++) {
PsiExpression lExpr = thenExpressions[i];
PsiExpression rExpr = elseExpressions[i];
if (!PsiEquivalenceUtil.areElementsEquivalent(lExpr, rExpr)) {
if (tExpr == null || eExpr == null) {
tExpr = lExpr;
eExpr = rExpr;
}
else {
return false;
}
}
}
setText("Push condition " + conditionalExpression.getCondition().getText() + " inside " +
(thenMethod.isConstructor() ? "constructor" : "method") + " call");
return true;
}
@Override
public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement element) throws IncorrectOperationException {
if (!CodeInsightUtilBase.preparePsiElementForWrite(element)) return;
final PsiConditionalExpression conditionalExpression = PsiTreeUtil.getParentOfType(element, PsiConditionalExpression.class);
final PsiExpression thenExpression = (PsiExpression)conditionalExpression.getThenExpression().copy();
final PsiExpressionList thenArgsList = ((PsiCallExpression)thenExpression).getArgumentList();
final PsiExpression[] thenExpressions = thenArgsList.getExpressions();
final PsiExpression elseExpression = conditionalExpression.getElseExpression();
final PsiExpressionList elseArgsList = ((PsiCallExpression)elseExpression).getArgumentList();
final PsiExpression[] elseExpressions = elseArgsList.getExpressions();
for (int i = 0; i < thenExpressions.length; i++) {
PsiExpression lExpr = thenExpressions[i];
PsiExpression rExpr = elseExpressions[i];
if (!PsiEquivalenceUtil.areElementsEquivalent(lExpr, rExpr)) {
lExpr.replace(JavaPsiFacade.getElementFactory(project).createExpressionFromText(
conditionalExpression.getCondition().getText() + "?" + lExpr.getText() + ":" + rExpr.getText(), lExpr));
break;
}
}
CodeStyleManager.getInstance(project).reformat(conditionalExpression.replace(thenExpression));
}
}

View File

@@ -0,0 +1,7 @@
// "Push condition b inside method call" "true"
class Foo {
void bar(boolean b){
String s = foo(b ? "true" : "false");
}
String foo(String p) {return p;}
}

View File

@@ -0,0 +1,7 @@
// "Push condition b inside method call" "true"
class Foo {
void bar(boolean b){
String s = b <caret>? foo("true") : foo("false");
}
String foo(String p) {return p;}
}

View File

@@ -0,0 +1,8 @@
// "Push condition b inside method call" "false"
class Foo {
void bar(boolean b){
String s = b <caret>? foo("true") : foo(false);
}
String foo(String p) {return p;}
String foo(boolean b) {return "false";}
}

View File

@@ -0,0 +1,7 @@
// "Push condition b inside method call" "false"
class Foo {
void bar(boolean b){
String s = b <caret>? foo("true", true) : foo("false", false);
}
String foo(String p, boolean b) {return p;}
}

View File

@@ -0,0 +1,25 @@
/*
* Copyright 2000-2012 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 com.intellij.codeInsight.daemon.quickFix;
public class PushConditionInCallTest extends LightQuickFixTestCase{
public void test() throws Exception { doAllTests(); }
@Override
protected String getBasePath() {
return "/codeInsight/daemonCodeAnalyzer/quickFix/pushConditionInCall";
}
}

View File

@@ -0,0 +1,9 @@
class X {
void bar(boolean b) {
String str = <spot>foo(b ? "true": "false")</spot>;
}
String foo(String p) {
return p;
}
}

View File

@@ -0,0 +1,9 @@
class X {
void bar(boolean b) {
String str = <spot>b ? foo("true") : foo("false")</spot>;
}
String foo(String p) {
return p;
}
}

View File

@@ -0,0 +1,5 @@
<html>
<body>
This intention pushes condition inside method call when both branches call same method and only one argument differs.
</body>
</html>

View File

@@ -621,6 +621,10 @@
<className>com.intellij.codeInsight.intention.impl.SplitDeclarationAction</className>
<category>Declaration</category>
</intentionAction>
<intentionAction>
<className>com.intellij.codeInsight.intention.impl.PushConditionInCallAction</className>
<category>Declaration</category>
</intentionAction>
<intentionAction>
<className>com.intellij.codeInsight.intention.impl.MoveInitializerToConstructorAction</className>
<category>Declaration</category>