expressions presentation tweaks, new expression type changes should not delete anonymous class (IDEA-58290)

This commit is contained in:
anna
2010-10-28 11:50:35 +04:00
parent 52a74890d3
commit 7ae3e5f625
6 changed files with 41 additions and 12 deletions
@@ -26,6 +26,7 @@ import com.intellij.openapi.util.TextRange;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiUtil;
import com.intellij.psi.util.TypeConversionUtil;
import com.intellij.refactoring.introduceVariable.PsiExpressionTrimRenderer;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
@@ -42,7 +43,7 @@ public class ChangeNewOperatorTypeFix implements IntentionAction {
@NotNull
public String getText() {
return QuickFixBundle.message("change.new.operator.type.text", myExpression.getText(), myType.getPresentableText(), myType instanceof PsiArrayType ? "" : "()");
return QuickFixBundle.message("change.new.operator.type.text", new PsiExpressionTrimRenderer.RenderFunction().fun(myExpression), myType.getPresentableText(), myType instanceof PsiArrayType ? "" : "()");
}
@NotNull
@@ -81,10 +82,17 @@ public class ChangeNewOperatorTypeFix implements IntentionAction {
selection = new TextRange(caretOffset, caretOffset+1);
}
else {
newExpression = (PsiNewExpression)factory.createExpressionFromText("new " + toType.getCanonicalText() + "()", originalExpression);
final PsiAnonymousClass anonymousClass = originalExpression.getAnonymousClass();
newExpression = (PsiNewExpression)factory.createExpressionFromText("new " + toType.getCanonicalText() + "()" + (anonymousClass != null ? "{}" : ""), originalExpression);
PsiExpressionList argumentList = originalExpression.getArgumentList();
if (argumentList == null) return;
newExpression.getArgumentList().replace(argumentList);
if (anonymousClass != null) {
final PsiAnonymousClass newAnonymousClass = (PsiAnonymousClass)newExpression.getAnonymousClass().replace(anonymousClass);
final PsiClass aClass = PsiUtil.resolveClassInType(toType);
assert aClass != null;
newAnonymousClass.getBaseClassReference().replace(factory.createClassReferenceElement(aClass));
}
selection = null;
caretOffset = -1;
}
@@ -190,12 +190,15 @@ public class PsiExpressionTrimRenderer extends JavaRecursiveElementWalkingVisito
myBuf.append(PsiKeyword.NEW).append(" ").append(reference.getText());
final PsiExpression[] arrayDimensions = expr.getArrayDimensions();
final PsiType type = expr.getType();
final int dimensions = type != null ? type.getArrayDimensions() : arrayDimensions.length;
if (arrayDimensions.length > 0) myBuf.append("[");
boolean first = true;
for (PsiExpression dimension : arrayDimensions) {
if (!first) myBuf.append(", ");
first = false;
dimension.accept(this);
for (int i = 0, arrayDimensionsLength = arrayDimensions.length; i < dimensions; i++) {
final PsiExpression dimension = i < arrayDimensionsLength ? arrayDimensions[i] : null;
if (i > 0) myBuf.append("][");
if (dimension != null) {
dimension.accept(this);
}
}
if (arrayDimensions.length > 0) myBuf.append("]");
@@ -221,4 +224,4 @@ public class PsiExpressionTrimRenderer extends JavaRecursiveElementWalkingVisito
return buf.toString();
}
}
}
}
@@ -0,0 +1,9 @@
// "Change 'new Runnable() {...}' to 'new StringBuffer()'" "true"
class X {
public StringBuffer buf = new StringBuffer(){
public void run(){
System.out.println("smth");
}
};
}
@@ -1,8 +1,8 @@
// "Change 'new ArrayList<Integer>(2)' to 'new ArrayList<String>()'" "true"
// "Change 'new ArrayList<Integer>(...)' to 'new ArrayList<String>()'" "true"
import java.util.*;
class RRR {
void f() {
List<String> l = new ArrayList<String>(2<caret>);
}
}
}
@@ -0,0 +1,9 @@
// "Change 'new Runnable() {...}' to 'new StringBuffer()'" "true"
class X {
public StringBuffer buf = <caret>new Runnable(){
public void run(){
System.out.println("smth");
}
};
}
@@ -1,8 +1,8 @@
// "Change 'new ArrayList<Integer>(2)' to 'new ArrayList<String>()'" "true"
// "Change 'new ArrayList<Integer>(...)' to 'new ArrayList<String>()'" "true"
import java.util.*;
class RRR {
void f() {
List<String> l = new <caret>ArrayList<Integer>(2);
}
}
}