Java inspection: Preserve comments from replaced and removed statements in EqualsReplaceableByObjectsCallInspection (IDEA-161076)

This commit is contained in:
Pavel Dolgov
2016-09-16 17:26:41 +03:00
parent 63131ab1b2
commit 40e1b7f91d
7 changed files with 60 additions and 53 deletions
@@ -18,7 +18,6 @@ package com.intellij.codeInspection.intermediaryVariable;
import com.intellij.codeInspection.*;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.*;
import com.intellij.psi.codeStyle.CodeStyleManager;
import com.intellij.psi.controlFlow.*;
@@ -218,7 +217,7 @@ public class ReturnSeparatedFromComputationInspection extends BaseJavaBatchLocal
inlineAssignment((PsiAssignmentExpression)e, context.returnStatement);
}
});
mover.removeCompletely.forEach(e -> removeElementKeepComment(e));
mover.removeCompletely.forEach(e -> removeElementKeepComments(e));
if (removeReturn) {
removeReturn(context);
}
@@ -228,7 +227,7 @@ public class ReturnSeparatedFromComputationInspection extends BaseJavaBatchLocal
Set<PsiElement> skippedEmptyStatements = new THashSet<>();
getPrevNonEmptyStatement(context.returnStatement, skippedEmptyStatements);
skippedEmptyStatements.forEach(PsiElement::delete);
removeElementKeepComment(context.returnStatement);
removeElementKeepComments(context.returnStatement);
}
private static void inlineAssignment(PsiAssignmentExpression assignmentExpression, PsiReturnStatement returnStatement) {
@@ -244,25 +243,14 @@ public class ReturnSeparatedFromComputationInspection extends BaseJavaBatchLocal
}
private static void replaceStatementKeepComments(PsiStatement replacedStatement, PsiReturnStatement returnStatement) {
List<PsiComment> keptComments = new ArrayList<>();
for (PsiElement element = replacedStatement.getFirstChild(); element != null; element = element.getNextSibling()) {
if (element instanceof PsiComment) {
keptComments.add((PsiComment)element);
}
}
List<PsiComment> keptComments = getComments(replacedStatement);
if (!keptComments.isEmpty()) {
returnStatement = (PsiReturnStatement)returnStatement.copy();
PsiElement lastReturnChild = returnStatement.getLastChild();
Project project = returnStatement.getProject();
PsiParserFacade parserFacade = PsiParserFacade.SERVICE.getInstance(project);
PsiElementFactory elementFactory = JavaPsiFacade.getInstance(project).getElementFactory();
if (lastReturnChild instanceof PsiComment && ((PsiComment)lastReturnChild).getTokenType() == JavaTokenType.END_OF_LINE_COMMENT) {
String commentText = StringUtil.trimStart(lastReturnChild.getText(), "//");
PsiComment inlineComment = elementFactory.createCommentFromText("/* " + commentText + " */", returnStatement);
lastReturnChild = lastReturnChild.replace(inlineComment);
}
for (PsiComment comment : keptComments) {
lastReturnChild = returnStatement.addAfter(parserFacade.createWhiteSpaceFromText(" "), lastReturnChild);
lastReturnChild = returnStatement.addAfter(comment, lastReturnChild);
}
CodeStyleManager.getInstance(project).reformat(returnStatement, true);
@@ -270,22 +258,38 @@ public class ReturnSeparatedFromComputationInspection extends BaseJavaBatchLocal
replacedStatement.replace(returnStatement);
}
private static void removeElementKeepComment(PsiElement element) {
PsiComment comment = null;
for (PsiElement child = element.getLastChild(); child != null; child = child.getPrevSibling()) {
if (child instanceof PsiComment) {
comment = (PsiComment)child;
break;
private static void removeElementKeepComments(PsiElement removedElement) {
List<PsiComment> keptComments = getComments(removedElement);
if (!keptComments.isEmpty()) {
PsiComment firstComment = keptComments.get(0);
PsiElement lastComment = removedElement.replace(firstComment);
PsiElement parent = lastComment.getParent();
Project project = parent.getProject();
CodeStyleManager styleManager = CodeStyleManager.getInstance(project);
styleManager.reformat(lastComment, true);
if (keptComments.size() > 1) {
for (PsiComment comment : keptComments.subList(1, keptComments.size())) {
lastComment = parent.addAfter(comment, lastComment);
styleManager.reformat(lastComment, true);
}
}
}
if (comment != null) {
element.replace(comment);
}
else {
element.delete();
removedElement.delete();
}
}
private static List<PsiComment> getComments(PsiElement commentedElement) {
final List<PsiComment> comments = new ArrayList<>();
commentedElement.accept(new JavaRecursiveElementVisitor() {
@Override
public void visitComment(PsiComment comment) {
comments.add(comment);
}
});
return comments;
}
private static class Mover {
final ControlFlow flow;
final PsiStatement enclosingStatement;
@@ -5,13 +5,13 @@ class T {
for (int i=0; i<a.length; i++) {
if (n < a[i]) n = a[i];
if (n > 100) {
return n; // at the end 1
return /* return 1 */ n /* return 2 */;// at the end 1
}
if (n < 0) {
return 0; // at the end 2
return /* return 1 */ 0 /* return 2 */;// at the end 2
/* inline */
}
}
return n;
return /* return 1 */ n /* return 2 */;
}
}
@@ -3,13 +3,16 @@ class T {
String f(String a) {
String s = a;
if (s == null) {
return ""; /* return comment */ // end of line
return ""; // return comment
// end of line
}
else if (s.startsWith("@")) {
return s.substring(1); // return comment
/* inline 1 *//* inline 2 */
}
else if (s.startsWith("#")) {
return "#"; /* return comment */ /* inline */
return "#"; // return comment
/* inline */
}
return s; // return comment
}
@@ -3,15 +3,15 @@ class T {
int f(int[][] a) {
int n = -1;
myLabel:
for (int i = 0; i < a.length; i++) {
if (a[i].length == 0) {
return -i - 1;
}
for(int j = 0; j < a[i].length; j++) {
n = j;
if (a[i][j] == 0) return n;
}
}
return n;
for (int i = 0; i < a.length; i++) {
if (a[i].length == 0) {
return -i - 1;
}
for(int j = 0; j < a[i].length; j++) {
n = j;
if (a[i][j] == 0) return n;
}
}
return n;
}
}
@@ -12,6 +12,6 @@ class T {
break; /* inline */
}
}
ret<caret>urn n;
ret<caret>urn /* return 1 */ n /* return 2 */;
}
}
@@ -6,7 +6,7 @@ class T {
s = ""; // end of line
}
else if (s.startsWith("@")) {
s = s.substring(1);
s = /* inline 1 */ s.substring(1); /* inline 2 */
}
else if (s.startsWith("#")) {
s = "#"; /* inline */
@@ -3,16 +3,16 @@ class T {
int f(int[][] a) {
int n = -1;
myLabel:
for (int i = 0; i < a.length; i++) {
if (a[i].length == 0) {
n = -i - 1;
break myLabel;
}
for(int j = 0; j < a[i].length; j++) {
n = j;
if (a[i][j] == 0) break myLabel;
}
}
re<caret>turn n;
for (int i = 0; i < a.length; i++) {
if (a[i].length == 0) {
n = -i - 1;
break myLabel;
}
for(int j = 0; j < a[i].length; j++) {
n = j;
if (a[i][j] == 0) break myLabel;
}
}
re<caret>turn n;
}
}