mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
ExpressionUtils#replacePolyadicWithParent: support also cases like a+[b-c] which should be reparsed like [a+b]-c
This commit is contained in:
+19
-14
@@ -1296,22 +1296,27 @@ public class ExpressionUtils {
|
||||
PsiExpression replacement,
|
||||
CommentTracker tracker) {
|
||||
PsiElement parent = expressionToReplace.getParent();
|
||||
if (parent instanceof PsiPolyadicExpression &&
|
||||
replacement instanceof PsiPolyadicExpression &&
|
||||
((PsiPolyadicExpression)parent).getOperationTokenType() == ((PsiPolyadicExpression)replacement).getOperationTokenType()) {
|
||||
int idx = ArrayUtil.indexOf(((PsiPolyadicExpression)parent).getOperands(), expressionToReplace);
|
||||
if (idx >= 0) {
|
||||
PsiPolyadicExpression copyParentPolyadic = (PsiPolyadicExpression)parent.copy();
|
||||
copyParentPolyadic.getOperands()[idx].replace(replacement);
|
||||
PsiExpression recreateCopyFromText = JavaPsiFacade.getElementFactory(parent.getProject())
|
||||
.createExpressionFromText(copyParentPolyadic.getText(), parent);
|
||||
PsiElement[] children = parent.getChildren();
|
||||
for (PsiElement child : children) {
|
||||
if (child != expressionToReplace) {
|
||||
tracker.markUnchanged(child);
|
||||
if (parent instanceof PsiPolyadicExpression && replacement instanceof PsiPolyadicExpression) {
|
||||
PsiPolyadicExpression parentPolyadic = (PsiPolyadicExpression)parent;
|
||||
PsiPolyadicExpression childPolyadic = (PsiPolyadicExpression)replacement;
|
||||
IElementType parentTokenType = parentPolyadic.getOperationTokenType();
|
||||
IElementType childTokenType = childPolyadic.getOperationTokenType();
|
||||
if (PsiPrecedenceUtil.getPrecedenceForOperator(parentTokenType) ==
|
||||
PsiPrecedenceUtil.getPrecedenceForOperator(childTokenType)) {
|
||||
int idx = ArrayUtil.indexOf(parentPolyadic.getOperands(), expressionToReplace);
|
||||
if (idx > 0 || (idx == 0 && parentTokenType == childTokenType)) {
|
||||
PsiPolyadicExpression copyParentPolyadic = (PsiPolyadicExpression)parent.copy();
|
||||
copyParentPolyadic.getOperands()[idx].replace(replacement);
|
||||
PsiExpression recreateCopyFromText = JavaPsiFacade.getElementFactory(parent.getProject())
|
||||
.createExpressionFromText(copyParentPolyadic.getText(), parent);
|
||||
PsiElement[] children = parent.getChildren();
|
||||
for (PsiElement child : children) {
|
||||
if (child != expressionToReplace) {
|
||||
tracker.markUnchanged(child);
|
||||
}
|
||||
}
|
||||
return (PsiExpression)tracker.replaceAndRestoreComments(parent, recreateCopyFromText);
|
||||
}
|
||||
return ((PsiPolyadicExpression)tracker.replaceAndRestoreComments(parent, recreateCopyFromText)).getOperands()[idx];
|
||||
}
|
||||
}
|
||||
return null;
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.siyeh.ipp.parentheses;
|
||||
|
||||
class Polyadic {
|
||||
boolean foo(int a, int b, int c) {
|
||||
return a + b<caret> - c;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.siyeh.ipp.parentheses;
|
||||
|
||||
class Polyadic {
|
||||
boolean foo(int a, int b, int c) {
|
||||
return a + (<caret>b - c);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.siyeh.ipp.parentheses;
|
||||
|
||||
class Polyadic {
|
||||
boolean foo(int a, int b, int c, int d) {
|
||||
return a + b<caret> - c + d;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.siyeh.ipp.parentheses;
|
||||
|
||||
class Polyadic {
|
||||
boolean foo(int a, int b, int c, int d) {
|
||||
return a + (<caret>b - c) + d;
|
||||
}
|
||||
}
|
||||
+2
@@ -30,6 +30,8 @@ public class UnnecessaryParenthesesQuickFixTest extends IGQuickFixesTestCase {
|
||||
}
|
||||
|
||||
public void testPolyadic() { doTest(); }
|
||||
public void testPolyadic2() { doTest(); }
|
||||
public void testPolyadic3() { doTest(); }
|
||||
public void testCommutative() { doTest(); }
|
||||
public void testWrapping() { doTest(); }
|
||||
public void testNotCommutative() { assertQuickfixNotAvailable(); }
|
||||
|
||||
Reference in New Issue
Block a user