(no message)

This commit is contained in:
Dave Griffith
2005-03-21 18:54:44 +03:00
parent 7e5fcd63bf
commit 8878fce850
12 changed files with 202 additions and 26 deletions
@@ -194,7 +194,7 @@
<module fileurl="file://$PROJECT_DIR$/IntentionPowerPackSVN.iml" filepath="$PROJECT_DIR$/IntentionPowerPackSVN.iml" />
</modules>
</component>
<component name="ProjectRootManager" version="2" assert-keyword="false" jdk-15="false" project-jdk-name="IDEA 3245" />
<component name="ProjectRootManager" version="2" assert-keyword="false" jdk-15="false" project-jdk-name="IDEA 3260" />
<component name="RmicSettings">
<option name="IS_EANABLED" value="false" />
<option name="DEBUGGING_INFO" value="true" />
@@ -131,8 +131,9 @@ public abstract class Intention implements IntentionAction{
}
protected static boolean isFileReadOnly(Project project, PsiFile file){
final VirtualFile virtualFile = file.getVirtualFile();
return ReadonlyStatusHandler.getInstance(project)
.ensureFilesWritable(new VirtualFile[]{file.getVirtualFile()})
.ensureFilesWritable(new VirtualFile[]{virtualFile})
.hasReadonlyFiles();
}
}
@@ -26,7 +26,8 @@ class AppendChainPredicate implements PsiElementPredicate{
final PsiElement parent = element.getParent();
if(parent instanceof PsiExpressionStatement)
return true;
if(parent instanceof PsiLocalVariable && parent.getParent() instanceof PsiDeclarationStatement &&
if(parent instanceof PsiLocalVariable &&
parent.getParent() instanceof PsiDeclarationStatement &&
( (PsiDeclarationStatement)(parent.getParent())).getDeclaredElements().length == 1)
return true;
if(parent instanceof PsiAssignmentExpression &&
@@ -5,7 +5,11 @@ import com.intellij.psi.PsiReferenceExpression;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.PsiClass;
public class AppendUtil{
class AppendUtil{
private AppendUtil(){
super();
}
public static boolean isAppend(PsiMethodCallExpression call)
{
final PsiReferenceExpression methodExpression =
@@ -31,14 +31,14 @@ public class MakeAppendChainIntoAppendSequenceIntention extends Intention{
return;
}
PsiExpression call =
final 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();
((PsiCall) currentCall).getArgumentList();
final String argText = args.getText();
argsList.add(argText);
final PsiReferenceExpression methodExpression =
@@ -46,17 +46,15 @@ public class MakeAppendChainIntoAppendSequenceIntention extends Intention{
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;
final String firstStatement;
if(call.getParent() instanceof PsiExpressionStatement){
targetText = currentCall.getText();
firstTargetText = currentCall.getText();
statement = (PsiStatement) call.getParent();
firstStatementPreamble = "";
firstStatement = null;
} else if(call.getParent() instanceof PsiAssignmentExpression &&
call.getParent()
.getParent() instanceof PsiExpressionStatement){
@@ -64,10 +62,10 @@ public class MakeAppendChainIntoAppendSequenceIntention extends Intention{
final PsiAssignmentExpression assignment =
(PsiAssignmentExpression) call.getParent();
targetText = assignment.getLExpression().getText();
firstTargetText = currentCall.getText();
firstStatementPreamble =
firstStatement =
assignment.getLExpression().getText() +
assignment.getOperationSign().getText();
assignment.getOperationSign().getText() + currentCall.getText() +
';';
} else{
statement = (PsiStatement) call.getParent().getParent();
final PsiDeclarationStatement declaration =
@@ -75,33 +73,37 @@ public class MakeAppendChainIntoAppendSequenceIntention extends Intention{
final PsiVariable variable =
(PsiVariable) declaration.getDeclaredElements()[0];
targetText = variable.getName();
firstTargetText = currentCall.getText();
if(variable.hasModifierProperty(PsiModifier.FINAL)){
firstStatementPreamble = "final " +
firstStatement = "final " +
variable.getType().getPresentableText() +
' ' + variable.getName() + '=';
' ' + variable.getName() + '=' + currentCall.getText() +
';';
} else{
firstStatementPreamble =
firstStatement =
variable.getType().getPresentableText() +
' ' + variable.getName() + '=';
' ' + variable.getName() + '=' + currentCall.getText() +
';';
}
}
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 + ';';
}
append = targetText + ".append" + arg + ';';
final PsiStatement newCall =
factory.createStatementFromText(append, null);
final PsiElement insertedElement = statement.getParent()
.addAfter(newCall, statement);
codeStyleManager.reformat(insertedElement);
}
if(firstStatement!=null)
{
final PsiStatement newCall =
factory.createStatementFromText(firstStatement, null);
final PsiElement insertedElement = statement.getParent()
.addAfter(newCall, statement);
codeStyleManager.reformat(insertedElement);
}
statement.delete();
}
}
@@ -0,0 +1,23 @@
package com.siyeh.ipp.forloop;
import com.intellij.psi.JavaTokenType;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiJavaToken;
import com.intellij.psi.PsiForeachStatement;
import com.intellij.psi.tree.IElementType;
import com.siyeh.ipp.base.PsiElementPredicate;
class ForEachLoopPredicate implements PsiElementPredicate{
public boolean satisfiedBy(PsiElement element){
if(!(element instanceof PsiJavaToken)){
return false;
}
final PsiJavaToken token = (PsiJavaToken) element;
final IElementType tokenType = token.getTokenType();
if(!JavaTokenType.FOR_KEYWORD.equals(tokenType)){
return false;
}
final PsiElement parent = element.getParent();
return parent instanceof PsiForeachStatement;
}
}
@@ -0,0 +1,116 @@
package com.siyeh.ipp.forloop;
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;
public class ReplaceForEachLoopWithForLoopIntention extends Intention{
public String getText(){
return "Replace for-each loop with old-style for loop";
}
public String getFamilyName(){
return "Replace For Each Loop";
}
public PsiElementPredicate getElementPredicate(){
return new ForEachLoopPredicate();
}
public void invoke(Project project, Editor editor, PsiFile file)
throws IncorrectOperationException{
if(isFileReadOnly(project, file)){
return;
}
final PsiElement forKeyword = findMatchingElement(file, editor);
final PsiForeachStatement statement = (PsiForeachStatement) forKeyword.getParent();
final StringBuffer newStatement = new StringBuffer();
final CodeStyleManager codeStyleManager =
CodeStyleManager.getInstance(project);
final PsiExpression iteratedValue = statement.getIteratedValue();
if(iteratedValue.getType() instanceof PsiArrayType){
final String index = codeStyleManager.suggestUniqueVariableName("i",
statement,
true);
newStatement.append("for(int ");
newStatement.append(index);
newStatement.append(" = 0;");
newStatement.append(index);
newStatement.append('<');
newStatement.append(iteratedValue.getText());
newStatement.append(".length;");
newStatement.append(index);
newStatement.append("++)");
newStatement.append("{ ");
newStatement.append(statement.getIterationParameter().getType()
.getPresentableText());
newStatement.append(' ');
newStatement.append(statement.getIterationParameter().getName());
newStatement.append(" = ");
newStatement.append(iteratedValue.getText());
newStatement.append('[');
newStatement.append(index);
newStatement.append("];");
final PsiStatement body = statement.getBody();
if(body instanceof PsiBlockStatement){
final PsiCodeBlock block =
((PsiBlockStatement) body).getCodeBlock();
final PsiElement[] children =
block.getChildren();
for(int i = 1; i < children.length - 1; i++){
//skip the braces
newStatement.append(children[i].getText());
}
} else{
newStatement.append(body.getText());
}
newStatement.append('}');
} else{
final String iterator =
codeStyleManager.suggestUniqueVariableName("it", statement,
true);
final String typeText = statement.getIterationParameter()
.getType()
.getPresentableText();
newStatement.append("for(java.util.Iterator<");
newStatement.append(typeText);
newStatement.append("> ");
newStatement.append(iterator);
newStatement.append(" = ");
newStatement.append(iteratedValue.getText());
newStatement.append(".iterator();");
newStatement.append(iterator);
newStatement.append(".hasNext();)");
newStatement.append('{');
newStatement.append(typeText);
newStatement.append(' ');
newStatement.append(statement.getIterationParameter().getName());
newStatement.append(" = ");
newStatement.append(iterator);
newStatement.append(".next();");
final PsiStatement body = statement.getBody();
if(body instanceof PsiBlockStatement){
final PsiCodeBlock block =
((PsiBlockStatement) body).getCodeBlock();
final PsiElement[] children = block.getChildren();
for(int i = 1; i < children.length - 1; i++){
//skip the braces
newStatement.append(children[i].getText());
}
} else{
newStatement.append(body.getText());
}
newStatement.append('}');
}
replaceStatement(project, newStatement.toString(), statement );
}
}
@@ -32,7 +32,7 @@ public class BoolUtils{
final PsiPrefixExpression prefixAncestor =
(PsiPrefixExpression) ancestor.getParent();
final PsiJavaToken sign = prefixAncestor.getOperationSign();
if(sign.getTokenType() == JavaTokenType.EXCL){
if(JavaTokenType.EXCL.equals(sign.getTokenType())){
return prefixAncestor;
}
}
@@ -192,7 +192,7 @@ class CaseUtil{
final IElementType operation = sign.getTokenType();
final PsiExpression lOperand = binaryExp.getLOperand();
final PsiExpression rhs = binaryExp.getROperand();
if(!(operation != JavaTokenType.OROR)){
if(operation.equals(JavaTokenType.OROR)){
return canBeMadeIntoCase(lOperand, caseExpression) &&
canBeMadeIntoCase(rhs, caseExpression);
} else if(operation.equals(JavaTokenType.EQEQ)){
@@ -0,0 +1,10 @@
public class X {
void f(String a, String b) {
String[] array = new String[]{"foo", "bar", "baz"};
for(int i = 0;i<array.length;i++)
{
String content = array[i];
System.out.println(content);
}
}
}
@@ -0,0 +1,9 @@
public class X {
void f(String a, String b) {
String[] array = new String[]{"foo", "bar", "baz"};
<spot>for</spot>(String content: array)
{
System.out.println(content);
}
}
}
@@ -0,0 +1,10 @@
<html>
<body>
<font face="verdana" size="-1">This intention replaces a J2SDK 5.0 for-each loop with
an equivalent for loop that does not use the for-each construction.<br>
</font>
<br><br>
<font face="verdana" size="-2">Powered by IntentionPowerPack </font>
</body>
</html>