mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
new "Merge sequential method calls into call chain" intention
This commit is contained in:
@@ -370,6 +370,10 @@
|
||||
<className>com.siyeh.ipp.concatenation.MakeCallChainIntoCallSequenceIntention</className>
|
||||
<categoryKey>intention.category.other</categoryKey>
|
||||
</intentionAction>
|
||||
<intentionAction>
|
||||
<className>com.siyeh.ipp.concatenation.MergeCallSequenceToChainIntention</className>
|
||||
<categoryKey>intention.category.other</categoryKey>
|
||||
</intentionAction>
|
||||
<intentionAction>
|
||||
<className>com.siyeh.ipp.exceptions.DetailExceptionsIntention</className>
|
||||
<categoryKey>intention.category.other</categoryKey>
|
||||
|
||||
@@ -83,6 +83,8 @@ replace.equality.with.equals.intention.name=Replace '==' with '.equals()'
|
||||
replace.equality.with.equals.intention.family.name=Replace Equality with Equals
|
||||
make.call.chain.into.call.sequence.intention.name=Make method call chain into method call sequence
|
||||
make.call.chain.into.call.sequence.intention.family.name=Make Call Chain Into Call Sequence
|
||||
merge.call.sequence.to.chain.intention.name=Merge sequential method calls into call chain
|
||||
merge.call.sequence.to.chain.intention.family.name=Merge Sequential Method Calls into Call Chain
|
||||
detail.exceptions.intention.name=Detail exceptions
|
||||
detail.exceptions.intention.family.name=Detail Exceptions
|
||||
flip.conditional.intention.name=Flip '?:'
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
/*
|
||||
* 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.siyeh.ipp.concatenation;
|
||||
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.siyeh.ipp.base.PsiElementPredicate;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
/**
|
||||
* @author Bas Leijdekkers
|
||||
*/
|
||||
public class CallSequencePredicate implements PsiElementPredicate {
|
||||
|
||||
@Override
|
||||
public boolean satisfiedBy(PsiElement element) {
|
||||
if (!(element instanceof PsiExpressionStatement)) {
|
||||
return false;
|
||||
}
|
||||
final PsiStatement statement = (PsiStatement)element;
|
||||
final PsiStatement nextSibling = PsiTreeUtil.getNextSiblingOfType(statement, PsiStatement.class);
|
||||
if (nextSibling == null) {
|
||||
return false;
|
||||
}
|
||||
final PsiVariable variable1 = getVariable(statement);
|
||||
if (variable1 == null) {
|
||||
return false;
|
||||
}
|
||||
final PsiVariable variable2 = getVariable(nextSibling);
|
||||
return variable1.equals(variable2);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static PsiVariable getVariable(PsiStatement statement) {
|
||||
if (!(statement instanceof PsiExpressionStatement)) {
|
||||
return null;
|
||||
}
|
||||
final PsiExpressionStatement expressionStatement = (PsiExpressionStatement)statement;
|
||||
final PsiExpression expression = expressionStatement.getExpression();
|
||||
if (!(expression instanceof PsiMethodCallExpression)) {
|
||||
return null;
|
||||
}
|
||||
final PsiMethodCallExpression methodCallExpression = (PsiMethodCallExpression)expression;
|
||||
return getVariable(methodCallExpression);
|
||||
}
|
||||
@Nullable
|
||||
private static PsiVariable getVariable(PsiMethodCallExpression methodCallExpression) {
|
||||
final PsiType type = methodCallExpression.getType();
|
||||
if (!(type instanceof PsiClassType)) {
|
||||
return null;
|
||||
}
|
||||
final PsiClassType classType = (PsiClassType)type;
|
||||
final PsiClass aClass = classType.resolve();
|
||||
if (aClass == null) {
|
||||
return null;
|
||||
}
|
||||
final PsiMethod method = methodCallExpression.resolveMethod();
|
||||
if (method == null) {
|
||||
return null;
|
||||
}
|
||||
final PsiClass containingClass = method.getContainingClass();
|
||||
if (!aClass.equals(containingClass)) {
|
||||
return null;
|
||||
}
|
||||
final PsiReferenceExpression methodExpression = methodCallExpression.getMethodExpression();
|
||||
final PsiExpression qualifierExpression = methodExpression.getQualifierExpression();
|
||||
if (qualifierExpression instanceof PsiMethodCallExpression) {
|
||||
final PsiMethodCallExpression expression = (PsiMethodCallExpression)qualifierExpression;
|
||||
return getVariable(expression);
|
||||
} else if (!(qualifierExpression instanceof PsiReferenceExpression)) {
|
||||
return null;
|
||||
}final PsiReferenceExpression referenceExpression = (PsiReferenceExpression)qualifierExpression;
|
||||
final PsiElement target = referenceExpression.resolve();
|
||||
if (!(target instanceof PsiVariable)) {
|
||||
return null;
|
||||
}
|
||||
return (PsiVariable)target;
|
||||
}
|
||||
}
|
||||
+78
@@ -0,0 +1,78 @@
|
||||
/*
|
||||
* 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.siyeh.ipp.concatenation;
|
||||
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import com.siyeh.ipp.base.Intention;
|
||||
import com.siyeh.ipp.base.PsiElementPredicate;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* @author Bas Leijdekkers
|
||||
*/
|
||||
public class MergeCallSequenceToChainIntention extends Intention {
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
protected PsiElementPredicate getElementPredicate() {
|
||||
return new CallSequencePredicate();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void processIntention(@NotNull PsiElement element) throws IncorrectOperationException {
|
||||
if (!(element instanceof PsiExpressionStatement)) {
|
||||
return;
|
||||
}
|
||||
final PsiExpressionStatement statement = (PsiExpressionStatement)element;
|
||||
final PsiExpressionStatement nextSibling = PsiTreeUtil.getNextSiblingOfType(statement, PsiExpressionStatement.class);
|
||||
if (nextSibling == null) {
|
||||
return;
|
||||
}
|
||||
final PsiExpression expression = statement.getExpression();
|
||||
final StringBuilder newMethodCallExpression = new StringBuilder(expression.getText());
|
||||
final PsiExpression expression1 = nextSibling.getExpression();
|
||||
if (!(expression1 instanceof PsiMethodCallExpression)) {
|
||||
return;
|
||||
}
|
||||
PsiMethodCallExpression methodCallExpression = getRootMethodCallExpression((PsiMethodCallExpression)expression1);
|
||||
while (true) {
|
||||
final PsiExpressionList argumentList = methodCallExpression.getArgumentList();
|
||||
final PsiReferenceExpression methodExpression = methodCallExpression.getMethodExpression();
|
||||
final String methodName = methodExpression.getReferenceName();
|
||||
newMethodCallExpression.append('.').append(methodName).append(argumentList.getText());
|
||||
final PsiElement parent = methodCallExpression.getParent();
|
||||
final PsiElement grandParent = parent.getParent();
|
||||
if (!(grandParent instanceof PsiMethodCallExpression)) {
|
||||
break;
|
||||
}
|
||||
methodCallExpression = (PsiMethodCallExpression)grandParent;
|
||||
}
|
||||
replaceExpression(newMethodCallExpression.toString(), expression);
|
||||
nextSibling.delete();
|
||||
}
|
||||
|
||||
public static PsiMethodCallExpression getRootMethodCallExpression(PsiMethodCallExpression expression) {
|
||||
final PsiReferenceExpression methodExpression = expression.getMethodExpression();
|
||||
final PsiExpression qualifierExpression = methodExpression.getQualifierExpression();
|
||||
if (qualifierExpression instanceof PsiMethodCallExpression) {
|
||||
final PsiMethodCallExpression methodCallExpression = (PsiMethodCallExpression)qualifierExpression;
|
||||
return getRootMethodCallExpression(methodCallExpression);
|
||||
}
|
||||
return expression;
|
||||
}
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
public class X {
|
||||
void f(String a, String b) {
|
||||
StringBuffer buffer = new StringBuffer();
|
||||
buffer.append(a).append(b);
|
||||
}
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
public class X {
|
||||
void f(String a, String b) {
|
||||
StringBuffer buffer = new StringBuffer();
|
||||
<spot>buffer.append(a)</spot>;
|
||||
buffer.append(b);
|
||||
}
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
<html>
|
||||
<body>
|
||||
This intention replaces a sequence of two method call statements with the equivalent chain of method calls (may alter semantics).
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.siyeh.ipp.concatenation.merge_sequence;
|
||||
|
||||
class Append {
|
||||
|
||||
void foo(StringBuilder s) {
|
||||
s.append(1).app<caret>end(2);
|
||||
s.append(3).append(4);
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
package com.siyeh.ipp.concatenation.merge_sequence;
|
||||
|
||||
class Append {
|
||||
|
||||
void foo(StringBuilder s) {
|
||||
s.append(1).append(2).append(3).append(4);
|
||||
}
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
|
||||
package com.siyeh.ipp.concatenation;
|
||||
|
||||
import com.siyeh.IntentionPowerPackBundle;
|
||||
import com.siyeh.ipp.IPPTestCase;
|
||||
|
||||
/**
|
||||
* @author Bas Leijdekkers
|
||||
*/
|
||||
public class MergeCallSequenceToChainIntentionTest extends IPPTestCase {
|
||||
|
||||
public void testAppend() { doTest(); }
|
||||
|
||||
@Override
|
||||
protected String getIntentionName() {
|
||||
return IntentionPowerPackBundle.message("merge.call.sequence.to.chain.intention.name");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getRelativePath() {
|
||||
return "concatenation/merge_sequence";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user