Search in sources :

Example 31 with PyExpression

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

the class TransformClassicClassQuickFix method applyFix.

public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    PsiElement psiElement = descriptor.getPsiElement();
    psiElement = PsiTreeUtil.getParentOfType(psiElement, PyClass.class);
    if (psiElement != null) {
        PyClass pyClass = (PyClass) psiElement;
        PyExpression[] superClassExpressions = pyClass.getSuperClassExpressions();
        PyElementGenerator generator = PyElementGenerator.getInstance(project);
        if (superClassExpressions.length == 0) {
            pyClass.replace(generator.createFromText(LanguageLevel.getDefault(), PyClass.class, "class " + pyClass.getName() + "(" + PyNames.OBJECT + "):\n    " + pyClass.getStatementList().getText()));
        } else {
            StringBuilder stringBuilder = new StringBuilder("class ");
            stringBuilder.append(pyClass.getName()).append("(");
            for (PyExpression expression : superClassExpressions) {
                stringBuilder.append(expression.getText()).append(", ");
            }
            stringBuilder.append(PyNames.OBJECT).append(":\n    ");
            stringBuilder.append(pyClass.getStatementList().getText());
            pyClass.replace(generator.createFromText(LanguageLevel.getDefault(), PyClass.class, stringBuilder.toString()));
        }
    }
}
Also used : PyClass(com.jetbrains.python.psi.PyClass) PyExpression(com.jetbrains.python.psi.PyExpression) PyElementGenerator(com.jetbrains.python.psi.PyElementGenerator) PsiElement(com.intellij.psi.PsiElement)

Example 32 with PyExpression

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

the class UnresolvedRefTrueFalseQuickFix method applyFix.

public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
    PyExpression expression = elementGenerator.createExpressionFromText(newName);
    myElement.replace(expression);
}
Also used : PyExpression(com.jetbrains.python.psi.PyExpression) PyElementGenerator(com.jetbrains.python.psi.PyElementGenerator)

Example 33 with PyExpression

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

the class PyChangeBaseClassQuickFix method applyFix.

@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    PsiElement element = descriptor.getPsiElement();
    final PyClass pyClass = PsiTreeUtil.getParentOfType(element, PyClass.class);
    assert pyClass != null;
    final PyArgumentList expressionList = pyClass.getSuperClassExpressionList();
    if (expressionList != null && expressionList.getArguments().length != 0) {
        final PyExpression argument = expressionList.getArguments()[0];
        final TemplateBuilder builder = TemplateBuilderFactory.getInstance().createTemplateBuilder(argument);
        builder.replaceElement(argument, argument.getText());
        final VirtualFile virtualFile = element.getContainingFile().getVirtualFile();
        if (virtualFile != null) {
            final Editor editor = FileEditorManager.getInstance(project).openTextEditor(new OpenFileDescriptor(project, virtualFile), true);
            assert editor != null;
            builder.run(editor, false);
        }
    }
}
Also used : PyClass(com.jetbrains.python.psi.PyClass) PyArgumentList(com.jetbrains.python.psi.PyArgumentList) VirtualFile(com.intellij.openapi.vfs.VirtualFile) TemplateBuilder(com.intellij.codeInsight.template.TemplateBuilder) PyExpression(com.jetbrains.python.psi.PyExpression) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) Editor(com.intellij.openapi.editor.Editor) PsiElement(com.intellij.psi.PsiElement)

Example 34 with PyExpression

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

Example 35 with PyExpression

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

the class PyRemoveArgumentQuickFix method applyFix.

public void applyFix(@NotNull final Project project, @NotNull final ProblemDescriptor descriptor) {
    final PsiElement element = descriptor.getPsiElement();
    if (!(element instanceof PyExpression))
        return;
    final PyExpression expression = (PyExpression) element;
    final PsiElement nextSibling = PsiTreeUtil.skipSiblingsForward(expression, PsiWhiteSpace.class);
    final PsiElement prevSibling = PsiTreeUtil.skipSiblingsBackward(expression, PsiWhiteSpace.class);
    expression.delete();
    if (nextSibling != null && nextSibling.getNode().getElementType().equals(PyTokenTypes.COMMA)) {
        nextSibling.delete();
        return;
    }
    if (prevSibling != null && prevSibling.getNode().getElementType().equals(PyTokenTypes.COMMA)) {
        prevSibling.delete();
    }
}
Also used : PyExpression(com.jetbrains.python.psi.PyExpression) 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