mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-01-06 03:21:12 +07:00
Java: Improved inspection "Join Declaration And Assignment" - handle C-style array declarations (IDEA-177132)
This commit is contained in:
@@ -10,6 +10,7 @@ import com.intellij.psi.*;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import com.intellij.util.ObjectUtils;
|
||||
import com.intellij.util.containers.ContainerUtil;
|
||||
import com.siyeh.ig.psiutils.CommentTracker;
|
||||
import com.siyeh.ig.psiutils.ExpressionUtils;
|
||||
import com.siyeh.ig.psiutils.SideEffectChecker;
|
||||
@@ -231,11 +232,22 @@ public class JoinDeclarationAndAssignmentJavaInspection extends AbstractBaseJava
|
||||
PsiExpression initializer = DeclarationJoinLinesHandler.getInitializerExpression(context.myVariable, context.myAssignment);
|
||||
PsiElement elementToReplace = context.myAssignment.getParent();
|
||||
if (initializer != null && elementToReplace != null) {
|
||||
CommentTracker tracker = new CommentTracker();
|
||||
tracker.markUnchanged(initializer);
|
||||
String text = context.getDeclarationText(initializer);
|
||||
tracker.delete(context.myVariable);
|
||||
tracker.replaceAndRestoreComments(elementToReplace, text);
|
||||
// Don't normalize the original declaration: it may declare many variables
|
||||
PsiElement declCopy = context.myVariable.getParent().copy();
|
||||
PsiLocalVariable varCopy = (PsiLocalVariable)ContainerUtil.find(
|
||||
declCopy.getChildren(), e -> e instanceof PsiLocalVariable && context.myName.equals(((PsiLocalVariable)e).getName()));
|
||||
|
||||
if (varCopy != null) {
|
||||
varCopy.setInitializer(initializer);
|
||||
varCopy.normalizeDeclaration();
|
||||
String text = varCopy.getText();
|
||||
|
||||
CommentTracker tracker = new CommentTracker();
|
||||
tracker.markUnchanged(initializer);
|
||||
tracker.markUnchanged(context.myVariable);
|
||||
tracker.delete(context.myVariable);
|
||||
tracker.replaceAndRestoreComments(elementToReplace, text);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -253,18 +265,5 @@ public class JoinDeclarationAndAssignmentJavaInspection extends AbstractBaseJava
|
||||
myIsUpdate = !JavaTokenType.EQ.equals(myAssignment.getOperationTokenType()) ||
|
||||
findNextAssignment(myAssignment.getParent(), myVariable) != null;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private String getDeclarationText(@NotNull PsiExpression initializer) {
|
||||
StringJoiner joiner = new StringJoiner(" ");
|
||||
if (myVariable.hasModifierProperty(PsiModifier.FINAL)) {
|
||||
joiner.add(PsiKeyword.FINAL + ' ');
|
||||
}
|
||||
for (PsiAnnotation annotation : myVariable.getAnnotations()) {
|
||||
joiner.add(annotation.getText() + ' ');
|
||||
}
|
||||
joiner.add(myVariable.getTypeElement().getText() + ' ' + myName + '=' + initializer.getText() + ';');
|
||||
return joiner.toString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
// "Join declaration and assignment" "GENERIC_ERROR_OR_WARNING"
|
||||
class Test {
|
||||
{
|
||||
int[] a = new int[0];
|
||||
}
|
||||
}
|
||||
@@ -2,9 +2,9 @@
|
||||
class Test {
|
||||
{
|
||||
// comment A
|
||||
/*comment B*/ /*comment 1*/ /*comment 2*/ /*comment 3*/ // comment 4
|
||||
/*comment B*/ // comment 4
|
||||
/*comment C*/
|
||||
/*comment E*/
|
||||
String ss = "hello" + /*comment D*/ " world"; // comment F
|
||||
String /*comment 1*/ ss /*comment 2*/ /*comment 3*/ = "hello" + /*comment D*/ " world"; // comment F
|
||||
}
|
||||
}
|
||||
@@ -5,10 +5,8 @@ class C {
|
||||
bar();
|
||||
// comment A
|
||||
/*comment B*/
|
||||
/*comment 2*/
|
||||
/*comment 3*/
|
||||
//comment 4
|
||||
int n = 1; // comment C
|
||||
int /*comment 2*/ n /*comment 3*/ = 1; // comment C
|
||||
}
|
||||
void bar(){}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
// "Join declaration and assignment" "GENERIC_ERROR_OR_WARNING"
|
||||
class Test {
|
||||
{
|
||||
int a[];
|
||||
<caret>a = new int[0];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user