Search in sources :

Example 1 with PyElementType

use of com.jetbrains.python.psi.PyElementType 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 2 with PyElementType

use of com.jetbrains.python.psi.PyElementType 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)

Aggregations

PsiElement (com.intellij.psi.PsiElement)2 PyBinaryExpression (com.jetbrains.python.psi.PyBinaryExpression)2 PyElementType (com.jetbrains.python.psi.PyElementType)2 PyElementGenerator (com.jetbrains.python.psi.PyElementGenerator)1 PyExpression (com.jetbrains.python.psi.PyExpression)1 PyFile (com.jetbrains.python.psi.PyFile)1