Search in sources :

Example 6 with PyBinaryExpression

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

the class ChainedComparisonsQuickFix method applyFix.

public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    final PyBinaryExpression expression = as(descriptor.getPsiElement(), PyBinaryExpression.class);
    if (expression != null && expression.isWritable()) {
        final PyBinaryExpression rightExpression = as(expression.getRightExpression(), PyBinaryExpression.class);
        final PyBinaryExpression leftExpression = as(expression.getLeftExpression(), PyBinaryExpression.class);
        if (rightExpression != null && leftExpression != null && isLogicalAndExpression(expression)) {
            applyFix(goDownIfNeeded(leftExpression), rightExpression, project);
        }
    }
}
Also used : PyBinaryExpression(com.jetbrains.python.psi.PyBinaryExpression)

Example 7 with PyBinaryExpression

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

the class PyFlipComparisonIntention method isAvailable.

public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
    if (!(file instanceof PyFile)) {
        return false;
    }
    PsiElement element = file.findElementAt(editor.getCaretModel().getOffset());
    PyBinaryExpression binaryExpression = PsiTreeUtil.getParentOfType(element, PyBinaryExpression.class, false);
    while (binaryExpression != null) {
        PyElementType operator = binaryExpression.getOperator();
        if (FLIPPED_OPERATORS.containsKey(operator)) {
            String operatorText = binaryExpression.getPsiOperator().getText();
            String flippedOperatorText = FLIPPED_OPERATORS.get(operator);
            if (flippedOperatorText.equals(operatorText)) {
                setText(PyBundle.message("INTN.flip.$0", operatorText));
            } else {
                setText(PyBundle.message("INTN.flip.$0.to.$1", operatorText, flippedOperatorText));
            }
            return true;
        }
        binaryExpression = PsiTreeUtil.getParentOfType(binaryExpression, PyBinaryExpression.class);
    }
    return false;
}
Also used : PyElementType(com.jetbrains.python.psi.PyElementType) PyFile(com.jetbrains.python.psi.PyFile) PyBinaryExpression(com.jetbrains.python.psi.PyBinaryExpression) PsiElement(com.intellij.psi.PsiElement)

Example 8 with PyBinaryExpression

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

the class PyFlipComparisonIntention method doInvoke.

public void doInvoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
    PsiElement element = file.findElementAt(editor.getCaretModel().getOffset());
    PyBinaryExpression binaryExpression = PsiTreeUtil.getParentOfType(element, PyBinaryExpression.class, false);
    while (binaryExpression != null) {
        if (FLIPPED_OPERATORS.containsKey(binaryExpression.getOperator())) {
            PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
            binaryExpression.replace(elementGenerator.createBinaryExpression(FLIPPED_OPERATORS.get(binaryExpression.getOperator()), binaryExpression.getRightExpression(), binaryExpression.getLeftExpression()));
            return;
        }
        binaryExpression = PsiTreeUtil.getParentOfType(binaryExpression, PyBinaryExpression.class);
    }
}
Also used : PyElementGenerator(com.jetbrains.python.psi.PyElementGenerator) PyBinaryExpression(com.jetbrains.python.psi.PyBinaryExpression) PsiElement(com.intellij.psi.PsiElement)

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