restore comments on Insert New

This commit is contained in:
Anna Kozlova
2018-05-28 13:08:22 +03:00
parent 7e5bd0001b
commit 4dfc2dbe6c
3 changed files with 14 additions and 4 deletions
@@ -22,6 +22,7 @@ import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.util.IncorrectOperationException;
import com.siyeh.ig.psiutils.CommentTracker;
import org.jetbrains.annotations.NotNull;
public class InsertNewFix implements IntentionAction {
@@ -61,13 +62,14 @@ public class InsertNewFix implements IntentionAction {
PsiElementFactory factory = JavaPsiFacade.getInstance(myMethodCall.getProject()).getElementFactory();
PsiNewExpression newExpression = (PsiNewExpression)factory.createExpressionFromText("new X()",null);
CommentTracker tracker = new CommentTracker();
PsiJavaCodeReferenceElement classReference = newExpression.getClassReference();
assert classReference != null;
classReference.replace(factory.createClassReferenceElement(myClass));
PsiExpressionList argumentList = newExpression.getArgumentList();
assert argumentList != null;
argumentList.replace(myMethodCall.getArgumentList());
myMethodCall.replace(newExpression);
argumentList.replace(tracker.markUnchanged(myMethodCall.getArgumentList()));
tracker.replaceAndRestoreComments(myMethodCall, newExpression);
}
@Override
@@ -1,7 +1,11 @@
// "Insert new" "true"
class a {
void f() {
<caret>new RuntimeException();
//c1
new RuntimeException(
//c2
)//c3
;
}
}
@@ -1,7 +1,11 @@
// "Insert new" "true"
class a {
void f() {
<caret>RuntimeException();
<caret>RuntimeException//c1
(
//c2
)//c3
;
}
}