mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
fixed (PY-2484) "Replace assignment with augmented assignment" - variables uncovered
This commit is contained in:
@@ -40,7 +40,11 @@ public class AugmentedAssignmentQuickFix implements LocalQuickFix {
|
||||
PyBinaryExpression expression = (PyBinaryExpression)statement.getAssignedValue();
|
||||
PyExpression leftExpression = expression.getLeftExpression();
|
||||
PyExpression rightExpression = expression.getRightExpression();
|
||||
|
||||
if (rightExpression.getText().equals(target.getText())) {
|
||||
PyExpression tmp = rightExpression;
|
||||
rightExpression = leftExpression;
|
||||
leftExpression = tmp;
|
||||
}
|
||||
if (leftExpression != null
|
||||
&& (leftExpression instanceof PyReferenceExpression || leftExpression instanceof PySubscriptionExpression)) {
|
||||
if (leftExpression.getText().equals(target.getText())) {
|
||||
@@ -58,22 +62,6 @@ public class AugmentedAssignmentQuickFix implements LocalQuickFix {
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (rightExpression != null &&
|
||||
(rightExpression instanceof PyReferenceExpression || rightExpression instanceof PySubscriptionExpression)) {
|
||||
if (rightExpression.getText().equals(target.getText())) {
|
||||
if (leftExpression instanceof PyNumericLiteralExpression ||
|
||||
leftExpression instanceof PyStringLiteralExpression) {
|
||||
|
||||
PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
stringBuilder.append(target.getText()).append(" ").
|
||||
append(expression.getPsiOperator().getText()).append("= ").append(leftExpression.getText());
|
||||
PyAugAssignmentStatementImpl augAssignment = elementGenerator.createFromText(LanguageLevel.getDefault(),
|
||||
PyAugAssignmentStatementImpl.class, stringBuilder.toString());
|
||||
statement.replace(augAssignment);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import com.jetbrains.python.PyBundle;
|
||||
import com.jetbrains.python.PyTokenTypes;
|
||||
import com.jetbrains.python.actions.AugmentedAssignmentQuickFix;
|
||||
import com.jetbrains.python.psi.*;
|
||||
import com.jetbrains.python.psi.types.PyType;
|
||||
import org.jetbrains.annotations.Nls;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
@@ -41,6 +42,11 @@ public class PyAugmentAssignmentInspection extends PyInspection {
|
||||
PyBinaryExpression expression = (PyBinaryExpression)node.getAssignedValue();
|
||||
PyExpression leftExpression = expression.getLeftExpression();
|
||||
PyExpression rightExpression = expression.getRightExpression();
|
||||
if (rightExpression.getText().equals(target.getText())) {
|
||||
PyExpression tmp = rightExpression;
|
||||
rightExpression = leftExpression;
|
||||
leftExpression = tmp;
|
||||
}
|
||||
if (PyTokenTypes.ADDITIVE_OPERATIONS.contains(expression.getOperator()) ||
|
||||
PyTokenTypes.MULTIPLICATIVE_OPERATIONS.contains(expression.getOperator())) {
|
||||
if (leftExpression != null
|
||||
@@ -54,17 +60,11 @@ public class PyAugmentAssignmentInspection extends PyInspection {
|
||||
registerProblem(node, "Assignment can be replaced with augmented assignment", quickFix);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (rightExpression != null &&
|
||||
(rightExpression instanceof PyReferenceExpression || rightExpression instanceof PySubscriptionExpression)) {
|
||||
if (rightExpression.getText().equals(target.getText())) {
|
||||
if (leftExpression instanceof PyNumericLiteralExpression) {
|
||||
PyElementType op = expression.getOperator();
|
||||
if (op == PyTokenTypes.PLUS || op == PyTokenTypes.MINUS ||
|
||||
op == PyTokenTypes.MULT || op == PyTokenTypes.DIV) {
|
||||
AugmentedAssignmentQuickFix quickFix = new AugmentedAssignmentQuickFix();
|
||||
registerProblem(node, "Assignment can be replaced with augmented assignment", quickFix);
|
||||
else {
|
||||
PyType type = rightExpression.getType(myTypeEvalContext);
|
||||
if (type != null) {
|
||||
if (type.getName().equals("int") || type.getName().equals("str"))
|
||||
registerProblem(node, "Assignment can be replaced with augmented assignment", new AugmentedAssignmentQuickFix());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,12 +3,12 @@
|
||||
|
||||
str = "string"
|
||||
|
||||
var_2 = var_2 + str
|
||||
<warning descr="Assignment can be replaced with augmented assignment">var_2 = var_2 + str</warning>
|
||||
|
||||
var_3 = var_3
|
||||
var_4 = 1
|
||||
var_5 = var_5 + "string"
|
||||
var_6 = var_6 + var_4
|
||||
<warning descr="Assignment can be replaced with augmented assignment">var_5 = var_5 + "string"</warning>
|
||||
<warning descr="Assignment can be replaced with augmented assignment">var_6 = var_6 + var_4</warning>
|
||||
|
||||
var_7 += 2
|
||||
|
||||
|
||||
Reference in New Issue
Block a user