Search in sources :

Example 1 with PyExpression

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

the class PyWithFixer method doApply.

@Override
public void doApply(@NotNull Editor editor, @NotNull PySmartEnterProcessor processor, @NotNull PyWithStatement withStatement) throws IncorrectOperationException {
    final PsiElement colonToken = PyPsiUtils.getFirstChildOfType(withStatement, PyTokenTypes.COLON);
    final PsiElement withToken = PyPsiUtils.getFirstChildOfType(withStatement, PyTokenTypes.WITH_KEYWORD);
    final Document document = editor.getDocument();
    if (colonToken == null && withToken != null) {
        int insertAt = withToken.getTextRange().getEndOffset();
        String textToInsert = ":";
        final PyWithItem lastItem = ArrayUtil.getLastElement(withStatement.getWithItems());
        if (lastItem == null || lastItem.getExpression() == null) {
            textToInsert = " :";
            processor.registerUnresolvedError(insertAt + 1);
        } else {
            final PyExpression expression = lastItem.getExpression();
            insertAt = expression.getTextRange().getEndOffset();
            final PsiElement asToken = PyPsiUtils.getFirstChildOfType(lastItem, PyTokenTypes.AS_KEYWORD);
            if (asToken != null) {
                insertAt = asToken.getTextRange().getEndOffset();
                final PyExpression target = lastItem.getTarget();
                if (target != null) {
                    insertAt = target.getTextRange().getEndOffset();
                } else {
                    textToInsert = " :";
                    processor.registerUnresolvedError(insertAt + 1);
                }
            }
        }
        document.insertString(insertAt, textToInsert);
    }
}
Also used : PyExpression(com.jetbrains.python.psi.PyExpression) Document(com.intellij.openapi.editor.Document) PyWithItem(com.jetbrains.python.psi.PyWithItem) PsiElement(com.intellij.psi.PsiElement)

Example 2 with PyExpression

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

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

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

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

the class ConvertDocstringQuickFix method applyFix.

public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    PsiElement expression = descriptor.getPsiElement();
    if (expression instanceof PyStringLiteralExpression && expression.isWritable()) {
        PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
        String stringText = expression.getText();
        int prefixLength = PyStringLiteralExpressionImpl.getPrefixLength(stringText);
        String prefix = stringText.substring(0, prefixLength);
        String content = stringText.substring(prefixLength);
        if (content.startsWith("'''")) {
            content = content.substring(3, content.length() - 3);
        } else if (content.startsWith("\"\"\""))
            return;
        else {
            content = content.length() == 1 ? "" : content.substring(1, content.length() - 1);
        }
        if (content.endsWith("\""))
            content = StringUtil.replaceSubstring(content, TextRange.create(content.length() - 1, content.length()), "\\\"");
        PyExpression newString = elementGenerator.createDocstring(prefix + "\"\"\"" + content + "\"\"\"").getExpression();
        expression.replace(newString);
    }
}
Also used : PyStringLiteralExpression(com.jetbrains.python.psi.PyStringLiteralExpression) PyExpression(com.jetbrains.python.psi.PyExpression) PyElementGenerator(com.jetbrains.python.psi.PyElementGenerator) PsiElement(com.intellij.psi.PsiElement)

Aggregations

PyExpression (com.jetbrains.python.psi.PyExpression)48 PsiElement (com.intellij.psi.PsiElement)22 PyElementGenerator (com.jetbrains.python.psi.PyElementGenerator)9 Pair (com.intellij.openapi.util.Pair)8 PyAssignmentStatement (com.jetbrains.python.psi.PyAssignmentStatement)8 PyTargetExpression (com.jetbrains.python.psi.PyTargetExpression)7 PyBinaryExpression (com.jetbrains.python.psi.PyBinaryExpression)4 Nullable (org.jetbrains.annotations.Nullable)4 PyFunctionTypeAnnotation (com.jetbrains.python.codeInsight.functionTypeComments.psi.PyFunctionTypeAnnotation)3 PyClass (com.jetbrains.python.psi.PyClass)3 PyType (com.jetbrains.python.psi.types.PyType)3 ASTNode (com.intellij.lang.ASTNode)2 Document (com.intellij.openapi.editor.Document)2 PyArgumentList (com.jetbrains.python.psi.PyArgumentList)2 PyCallExpression (com.jetbrains.python.psi.PyCallExpression)2 PyPathEvaluator (com.jetbrains.python.psi.impl.PyPathEvaluator)2 TypeEvalContext (com.jetbrains.python.psi.types.TypeEvalContext)2 NotNull (org.jetbrains.annotations.NotNull)2 TemplateBuilder (com.intellij.codeInsight.template.TemplateBuilder)1 Editor (com.intellij.openapi.editor.Editor)1