Search in sources :

Example 1 with PyBinaryExpression

use of com.jetbrains.python.psi.PyBinaryExpression in project intellij-community by JetBrains.

the class ChainedComparisonsQuickFix method applyFix.

private void applyFix(@NotNull PyBinaryExpression leftExpression, @NotNull PyBinaryExpression rightExpression, @NotNull Project project) {
    final PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
    final PyExpression newLeftExpression, newRightExpression;
    final String operator;
    if (myCommonIsInLeftLeft) {
        newLeftExpression = invertExpression(leftExpression, elementGenerator);
    } else {
        newLeftExpression = leftExpression;
    }
    if (myCommonIsInRightLeft) {
        operator = getLeftestOperator(rightExpression).getText();
        newRightExpression = getLargeRightExpression(rightExpression, project);
    } else {
        operator = invertOperator(assertNotNull(rightExpression.getPsiOperator()));
        final PyExpression rightLeftExpr = rightExpression.getLeftExpression();
        if (rightLeftExpr instanceof PyBinaryExpression) {
            newRightExpression = invertExpression((PyBinaryExpression) rightLeftExpr, elementGenerator);
        } else {
            newRightExpression = rightLeftExpr;
        }
    }
    final PyBinaryExpression binaryExpression = elementGenerator.createBinaryExpression(operator, newLeftExpression, newRightExpression);
    leftExpression.replace(binaryExpression);
    rightExpression.delete();
}
Also used : PyExpression(com.jetbrains.python.psi.PyExpression) PyElementGenerator(com.jetbrains.python.psi.PyElementGenerator) PyBinaryExpression(com.jetbrains.python.psi.PyBinaryExpression)

Example 2 with PyBinaryExpression

use of com.jetbrains.python.psi.PyBinaryExpression in project intellij-community by JetBrains.

the class ChainedComparisonsQuickFix method invertExpression.

@NotNull
private static PyExpression invertExpression(@NotNull PyBinaryExpression expression, @NotNull PyElementGenerator elementGenerator) {
    if (isComparisonExpression(expression)) {
        final PyExpression left = expression.getLeftExpression();
        final PyExpression right = expression.getRightExpression();
        final String newOperator = invertOperator(assertNotNull(expression.getPsiOperator()));
        final PyExpression newRight = isComparisonExpression(left) ? invertExpression((PyBinaryExpression) left, elementGenerator) : left;
        return elementGenerator.createBinaryExpression(newOperator, right, newRight);
    } else {
        return expression;
    }
}
Also used : PyExpression(com.jetbrains.python.psi.PyExpression) PyBinaryExpression(com.jetbrains.python.psi.PyBinaryExpression) ObjectUtils.assertNotNull(com.intellij.util.ObjectUtils.assertNotNull) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with PyBinaryExpression

use of com.jetbrains.python.psi.PyBinaryExpression in project intellij-community by JetBrains.

the class ComparisonWithNoneQuickFix method applyFix.

public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    PsiElement problemElement = descriptor.getPsiElement();
    if (problemElement instanceof PyBinaryExpression) {
        PyBinaryExpression binaryExpression = (PyBinaryExpression) problemElement;
        PyElementType operator = binaryExpression.getOperator();
        PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
        String temp;
        temp = (operator == PyTokenTypes.EQEQ) ? "is" : "is not";
        PyExpression expression = elementGenerator.createBinaryExpression(temp, binaryExpression.getLeftExpression(), binaryExpression.getRightExpression());
        binaryExpression.replace(expression);
    }
}
Also used : PyElementType(com.jetbrains.python.psi.PyElementType) PyExpression(com.jetbrains.python.psi.PyExpression) PyElementGenerator(com.jetbrains.python.psi.PyElementGenerator) PyBinaryExpression(com.jetbrains.python.psi.PyBinaryExpression) PsiElement(com.intellij.psi.PsiElement)

Example 4 with PyBinaryExpression

use of com.jetbrains.python.psi.PyBinaryExpression in project intellij-community by JetBrains.

the class ReplaceNotEqOperatorQuickFix method applyFix.

@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    PsiElement binaryExpression = descriptor.getPsiElement();
    if (binaryExpression instanceof PyBinaryExpression) {
        PsiElement operator = ((PyBinaryExpression) binaryExpression).getPsiOperator();
        if (operator != null) {
            PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
            operator.replace(elementGenerator.createFromText(LanguageLevel.forElement(binaryExpression), LeafPsiElement.class, "!="));
        }
    }
}
Also used : LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) PyElementGenerator(com.jetbrains.python.psi.PyElementGenerator) PyBinaryExpression(com.jetbrains.python.psi.PyBinaryExpression) PsiElement(com.intellij.psi.PsiElement) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement)

Example 5 with PyBinaryExpression

use of com.jetbrains.python.psi.PyBinaryExpression in project intellij-community by JetBrains.

the class ChainedComparisonsQuickFix method getLargeRightExpression.

@Nullable
private static PyExpression getLargeRightExpression(@NotNull PyBinaryExpression expression, @NotNull Project project) {
    final PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
    PyExpression left = expression.getLeftExpression();
    PyExpression right = expression.getRightExpression();
    PsiElement operator = expression.getPsiOperator();
    while (isComparisonExpression(left)) {
        assert operator != null;
        right = elementGenerator.createBinaryExpression(operator.getText(), ((PyBinaryExpression) left).getRightExpression(), right);
        operator = ((PyBinaryExpression) left).getPsiOperator();
        left = ((PyBinaryExpression) left).getLeftExpression();
    }
    return right;
}
Also used : PyExpression(com.jetbrains.python.psi.PyExpression) PyElementGenerator(com.jetbrains.python.psi.PyElementGenerator) PyBinaryExpression(com.jetbrains.python.psi.PyBinaryExpression) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

PyBinaryExpression (com.jetbrains.python.psi.PyBinaryExpression)8 PsiElement (com.intellij.psi.PsiElement)5 PyElementGenerator (com.jetbrains.python.psi.PyElementGenerator)5 PyExpression (com.jetbrains.python.psi.PyExpression)4 PyElementType (com.jetbrains.python.psi.PyElementType)2 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)1 ObjectUtils.assertNotNull (com.intellij.util.ObjectUtils.assertNotNull)1 PyFile (com.jetbrains.python.psi.PyFile)1 NotNull (org.jetbrains.annotations.NotNull)1 Nullable (org.jetbrains.annotations.Nullable)1