use of com.jetbrains.python.psi.impl.PyAugAssignmentStatementImpl in project intellij-community by JetBrains.
the class AugmentedAssignmentQuickFix method applyFix.
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
PsiElement element = descriptor.getPsiElement();
if (element instanceof PyAssignmentStatement && element.isWritable()) {
final PyAssignmentStatement statement = (PyAssignmentStatement) element;
final PyExpression target = statement.getLeftHandSideExpression();
final PyBinaryExpression expression = (PyBinaryExpression) statement.getAssignedValue();
if (expression == null)
return;
PyExpression leftExpression = expression.getLeftExpression();
PyExpression rightExpression = expression.getRightExpression();
if (rightExpression instanceof PyParenthesizedExpression)
rightExpression = ((PyParenthesizedExpression) rightExpression).getContainedExpression();
if (target != null && rightExpression != null) {
final String targetText = target.getText();
final String rightText = rightExpression.getText();
if (rightText.equals(targetText)) {
final PyExpression tmp = rightExpression;
rightExpression = leftExpression;
leftExpression = tmp;
}
final List<PsiComment> comments = PsiTreeUtil.getChildrenOfTypeAsList(statement, PsiComment.class);
if ((leftExpression instanceof PyReferenceExpression || leftExpression instanceof PySubscriptionExpression)) {
if (leftExpression.getText().equals(targetText)) {
final PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
final StringBuilder stringBuilder = new StringBuilder();
final PsiElement psiOperator = expression.getPsiOperator();
if (psiOperator == null)
return;
stringBuilder.append(targetText).append(" ").append(psiOperator.getText()).append("= ").append(rightExpression.getText());
final PyAugAssignmentStatementImpl augAssignment = elementGenerator.createFromText(LanguageLevel.forElement(element), PyAugAssignmentStatementImpl.class, stringBuilder.toString());
for (PsiComment comment : comments) augAssignment.add(comment);
statement.replace(augAssignment);
}
}
}
}
}
Aggregations