mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
(no message)
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
package com.siyeh.ipp.concatenation;
|
||||
|
||||
import com.intellij.psi.*;
|
||||
import com.siyeh.ipp.base.PsiElementPredicate;
|
||||
|
||||
class AppendChainPredicate implements PsiElementPredicate{
|
||||
public boolean satisfiedBy(PsiElement element){
|
||||
if(!(element instanceof PsiMethodCallExpression)){
|
||||
return false;
|
||||
}
|
||||
final PsiMethodCallExpression call = (PsiMethodCallExpression) element;
|
||||
if(!AppendUtil.isAppend(call))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
final PsiReferenceExpression methodExpression =
|
||||
call.getMethodExpression();
|
||||
final PsiExpression qualifier = methodExpression.getQualifierExpression();
|
||||
if(!(qualifier instanceof PsiMethodCallExpression)){
|
||||
return false;
|
||||
}
|
||||
final PsiMethodCallExpression qualifierCall = (PsiMethodCallExpression) element;
|
||||
if(!AppendUtil.isAppend(qualifierCall)){
|
||||
return false;
|
||||
}
|
||||
final PsiElement parent = element.getParent();
|
||||
if(parent instanceof PsiExpressionStatement)
|
||||
return true;
|
||||
if(parent instanceof PsiLocalVariable && parent.getParent() instanceof PsiDeclarationStatement &&
|
||||
( (PsiDeclarationStatement)(parent.getParent())).getDeclaredElements().length == 1)
|
||||
return true;
|
||||
if(parent instanceof PsiAssignmentExpression &&
|
||||
parent.getParent() instanceof PsiExpressionStatement)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.siyeh.ipp.concatenation;
|
||||
|
||||
import com.intellij.psi.PsiMethodCallExpression;
|
||||
import com.intellij.psi.PsiReferenceExpression;
|
||||
import com.intellij.psi.PsiMethod;
|
||||
import com.intellij.psi.PsiClass;
|
||||
|
||||
public class AppendUtil{
|
||||
public static boolean isAppend(PsiMethodCallExpression call)
|
||||
{
|
||||
final PsiReferenceExpression methodExpression =
|
||||
call.getMethodExpression();
|
||||
if(methodExpression == null){
|
||||
return false;
|
||||
}
|
||||
final String callName = methodExpression.getReferenceName();
|
||||
if(!"append".equals(callName)){
|
||||
return false;
|
||||
}
|
||||
final PsiMethod method = call.resolveMethod();
|
||||
if(method == null){
|
||||
return false;
|
||||
}
|
||||
final PsiClass containingClass = method.getContainingClass();
|
||||
if(containingClass == null){
|
||||
return false;
|
||||
}
|
||||
final String name = containingClass.getQualifiedName();
|
||||
return "java.lang.StringBuffer".equals(name)||
|
||||
"java.lang.StringBuilder".equals(name);
|
||||
}
|
||||
}
|
||||
+107
@@ -0,0 +1,107 @@
|
||||
package com.siyeh.ipp.concatenation;
|
||||
|
||||
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.util.IncorrectOperationException;
|
||||
import com.siyeh.ipp.base.Intention;
|
||||
import com.siyeh.ipp.base.PsiElementPredicate;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
public class MakeAppendChainIntoAppendSequenceIntention extends Intention{
|
||||
protected PsiElementPredicate getElementPredicate(){
|
||||
return new AppendChainPredicate();
|
||||
}
|
||||
|
||||
public String getText(){
|
||||
return "Make .append() chain into .append() sequence";
|
||||
}
|
||||
|
||||
public String getFamilyName(){
|
||||
return "Make Append Chain Into Append Sequence";
|
||||
}
|
||||
|
||||
public void invoke(Project project, Editor editor, PsiFile file)
|
||||
throws IncorrectOperationException{
|
||||
if(isFileReadOnly(project, file)){
|
||||
return;
|
||||
}
|
||||
|
||||
PsiExpression call =
|
||||
(PsiExpression) findMatchingElement(file, editor);
|
||||
final List argsList = new ArrayList();
|
||||
PsiExpression currentCall = call;
|
||||
while(currentCall instanceof PsiMethodCallExpression &&
|
||||
AppendUtil.isAppend((PsiMethodCallExpression) currentCall)){
|
||||
final PsiExpressionList args =
|
||||
((PsiMethodCallExpression) currentCall).getArgumentList();
|
||||
final String argText = args.getText();
|
||||
argsList.add(argText);
|
||||
final PsiReferenceExpression methodExpression =
|
||||
((PsiMethodCallExpression) currentCall).getMethodExpression();
|
||||
currentCall = methodExpression.getQualifierExpression();
|
||||
}
|
||||
final String targetText;
|
||||
final String firstTargetText;
|
||||
final PsiManager mgr = PsiManager.getInstance(project);
|
||||
final PsiElementFactory factory = mgr.getElementFactory();
|
||||
final CodeStyleManager codeStyleManager = mgr.getCodeStyleManager();
|
||||
final PsiStatement statement;
|
||||
final String firstStatementPreamble;
|
||||
if(call.getParent() instanceof PsiExpressionStatement){
|
||||
targetText = currentCall.getText();
|
||||
firstTargetText = currentCall.getText();
|
||||
statement = (PsiStatement) call.getParent();
|
||||
firstStatementPreamble = "";
|
||||
} else if(call.getParent() instanceof PsiAssignmentExpression &&
|
||||
call.getParent()
|
||||
.getParent() instanceof PsiExpressionStatement){
|
||||
statement = (PsiStatement) call.getParent().getParent();
|
||||
final PsiAssignmentExpression assignment =
|
||||
(PsiAssignmentExpression) call.getParent();
|
||||
targetText = assignment.getLExpression().getText();
|
||||
firstTargetText = currentCall.getText();
|
||||
firstStatementPreamble =
|
||||
assignment.getLExpression().getText() +
|
||||
assignment.getOperationSign().getText();
|
||||
} else{
|
||||
statement = (PsiStatement) call.getParent().getParent();
|
||||
final PsiDeclarationStatement declaration =
|
||||
(PsiDeclarationStatement) statement;
|
||||
final PsiVariable variable =
|
||||
(PsiVariable) declaration.getDeclaredElements()[0];
|
||||
targetText = variable.getName();
|
||||
firstTargetText = currentCall.getText();
|
||||
if(variable.hasModifierProperty(PsiModifier.FINAL)){
|
||||
firstStatementPreamble = "final " +
|
||||
variable.getType().getPresentableText() +
|
||||
' ' + variable.getName() + '=';
|
||||
} else{
|
||||
firstStatementPreamble =
|
||||
variable.getType().getPresentableText() +
|
||||
' ' + variable.getName() + '=';
|
||||
}
|
||||
}
|
||||
|
||||
for(Iterator iterator = argsList.iterator(); iterator.hasNext();){
|
||||
final String arg = (String) iterator.next();
|
||||
final String append;
|
||||
if(iterator.hasNext()){
|
||||
append = targetText + ".append" + arg + ';';
|
||||
} else{
|
||||
append = firstStatementPreamble +
|
||||
firstTargetText + ".append" + arg + ';';
|
||||
}
|
||||
final PsiStatement newCall =
|
||||
factory.createStatementFromText(append, null);
|
||||
final PsiElement insertedElement = statement.getParent()
|
||||
.addAfter(newCall, statement);
|
||||
codeStyleManager.reformat(insertedElement);
|
||||
}
|
||||
statement.delete();
|
||||
}
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
public class X {
|
||||
void f(String a, String b) {
|
||||
StringBuffer buffer = new StringBuffer
|
||||
buffer.append(a);
|
||||
buffer.append(b);
|
||||
}
|
||||
}}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
public class X {
|
||||
void f(String a, String b) {
|
||||
StringBuffer buffer = new StringBuffer
|
||||
<spot>buffer.append(a).append(b)</spot>;
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
<html>
|
||||
<body>
|
||||
<font face="verdana" size="-1">This intention chains of StringBuffer.append() calls with equivalent
|
||||
sequences of call statements.<br>
|
||||
</font>
|
||||
|
||||
<br><br>
|
||||
<font face="verdana" size="-2">Powered by IntentionPowerPack </font>
|
||||
</body>
|
||||
</html>
|
||||
@@ -1216,6 +1216,15 @@ public class ManualTestClass
|
||||
buffer.append("foo" + "bar" + "baz");
|
||||
}
|
||||
|
||||
public void testStringBufferSequencing()
|
||||
{
|
||||
StringBuffer buf = new StringBuffer();
|
||||
buf.append("foo").append("bar").append("baz");
|
||||
StringBuffer buf2;
|
||||
buf2 = new StringBuffer().append("foo").append("bar").append("baz");
|
||||
final StringBuffer buf3 = new StringBuffer().append("foo").append("bar").append("baz");
|
||||
}
|
||||
|
||||
public boolean testExpandBoolean()
|
||||
{
|
||||
boolean foo = true;
|
||||
|
||||
Reference in New Issue
Block a user