preserve comments: change new operator type

This commit is contained in:
Anna.Kozlova
2017-12-07 15:51:06 +01:00
parent ef5565903b
commit bacdd1ada8
3 changed files with 23 additions and 10 deletions
@@ -30,6 +30,7 @@ import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiUtil;
import com.intellij.psi.util.TypeConversionUtil;
import com.intellij.util.IncorrectOperationException;
import com.siyeh.ig.psiutils.CommentTracker;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -75,12 +76,13 @@ public class ChangeNewOperatorTypeFix implements IntentionAction {
PsiElementFactory factory = JavaPsiFacade.getInstance(originalExpression.getProject()).getElementFactory();
int caretOffset;
TextRange selection;
CommentTracker commentTracker = new CommentTracker();
if (toType instanceof PsiArrayType) {
final PsiExpression[] originalExpressionArrayDimensions = originalExpression.getArrayDimensions();
caretOffset = 0;
@NonNls String text = "new " + toType.getDeepComponentType().getCanonicalText() + "[";
if (originalExpressionArrayDimensions.length > 0) {
text += originalExpressionArrayDimensions[0].getText();
text += commentTracker.markUnchanged(originalExpressionArrayDimensions[0]).getText();
}
else {
text += "0";
@@ -91,7 +93,7 @@ public class ChangeNewOperatorTypeFix implements IntentionAction {
text += "[";
String arrayDimension = "";
if (originalExpressionArrayDimensions.length > i) {
arrayDimension = originalExpressionArrayDimensions[i].getText();
arrayDimension = commentTracker.markUnchanged(originalExpressionArrayDimensions[i]).getText();
text += arrayDimension;
}
text += "]";
@@ -112,7 +114,7 @@ public class ChangeNewOperatorTypeFix implements IntentionAction {
newExpression = (PsiNewExpression)factory.createExpressionFromText("new " + toType.getCanonicalText() + "()" + (anonymousClass != null ? "{}" : ""), originalExpression);
PsiExpressionList argumentList = originalExpression.getArgumentList();
if (argumentList == null) return;
newExpression.getArgumentList().replace(argumentList);
newExpression.getArgumentList().replace(commentTracker.markUnchanged(argumentList));
if (anonymousClass == null) { //just to prevent useless inference
if (PsiDiamondTypeUtil.canCollapseToDiamond(newExpression, originalExpression, toType)) {
final PsiElement paramList = PsiDiamondTypeUtil.replaceExplicitWithDiamond(newExpression.getClassOrAnonymousClassReference().getParameterList());
@@ -124,13 +126,19 @@ public class ChangeNewOperatorTypeFix implements IntentionAction {
PsiAnonymousClass newAnonymousClass = newExpression.getAnonymousClass();
final PsiElement childInside = anonymousClass.getLBrace().getNextSibling();
if (childInside != null) {
PsiElement element = childInside;
do {
commentTracker.markUnchanged(element);
}
while ((element = element.getNextSibling()) != null);
newAnonymousClass.addRange(childInside, anonymousClass.getRBrace().getPrevSibling());
}
}
selection = null;
caretOffset = -1;
}
PsiElement element = originalExpression.replace(newExpression);
PsiElement element = commentTracker.replaceAndRestoreComments(originalExpression, newExpression);
editor.getCaretModel().moveToOffset(element.getTextRange().getEndOffset() + caretOffset);
editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
if (selection != null) {
@@ -1,9 +1,11 @@
// "Change 'new Runnable() {...}' to 'new StringBuffer()'" "true"
class X {
public StringBuffer buf = new StringBuffer() {
public void run(){
System.out.println("smth");
}
};
//comment0
//comment1
public StringBuffer buf = new StringBuffer() {//comment2
public void run(){
System.out.println("smth");
}
};
}
@@ -1,7 +1,10 @@
// "Change 'new Runnable() {...}' to 'new StringBuffer()'" "true"
class X {
public StringBuffer buf = <caret>new Runnable(){
public StringBuffer buf = <caret>new Runnable//comment0
()
//comment1
{//comment2
public void run(){
System.out.println("smth");
}