Search in sources :

Example 11 with PyElementGenerator

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

the class ReplaceBackquoteExpressionQuickFix method applyFix.

@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    PsiElement problemElement = descriptor.getPsiElement();
    if (problemElement instanceof PyReprExpression) {
        if (((PyReprExpression) problemElement).getExpression() != null) {
            PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
            problemElement.replace(elementGenerator.createExpressionFromText("repr(" + ((PyReprExpression) problemElement).getExpression().getText() + ")"));
        }
    }
}
Also used : PyReprExpression(com.jetbrains.python.psi.PyReprExpression) PyElementGenerator(com.jetbrains.python.psi.PyElementGenerator) PsiElement(com.intellij.psi.PsiElement)

Example 12 with PyElementGenerator

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

the class ReplaceListComprehensionsQuickFix method applyFix.

@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    PsiElement expression = descriptor.getPsiElement();
    if (expression instanceof PyTupleExpression) {
        PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
        PyExpressionStatement statement = elementGenerator.createFromText(LanguageLevel.forElement(expression), PyExpressionStatement.class, "(" + expression.getText() + ")");
        expression.replace(statement.getExpression());
    }
}
Also used : PyExpressionStatement(com.jetbrains.python.psi.PyExpressionStatement) PyTupleExpression(com.jetbrains.python.psi.PyTupleExpression) PyElementGenerator(com.jetbrains.python.psi.PyElementGenerator) PsiElement(com.intellij.psi.PsiElement)

Example 13 with PyElementGenerator

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

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

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

the class PyRemoveUnderscoresInNumericLiteralsQuickFix method applyFix.

@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    final PsiElement element = descriptor.getPsiElement();
    if (element instanceof PyNumericLiteralExpression) {
        final PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
        final String text = element.getText();
        element.replace(elementGenerator.createExpressionFromText(LanguageLevel.forElement(element), text.replaceAll("_", "")));
    }
}
Also used : PyNumericLiteralExpression(com.jetbrains.python.psi.PyNumericLiteralExpression) PyElementGenerator(com.jetbrains.python.psi.PyElementGenerator) PsiElement(com.intellij.psi.PsiElement)

Aggregations

PyElementGenerator (com.jetbrains.python.psi.PyElementGenerator)26 PsiElement (com.intellij.psi.PsiElement)18 PyExpression (com.jetbrains.python.psi.PyExpression)9 PyBinaryExpression (com.jetbrains.python.psi.PyBinaryExpression)5 ASTNode (com.intellij.lang.ASTNode)4 PyStringLiteralExpression (com.jetbrains.python.psi.PyStringLiteralExpression)4 PyNumericLiteralExpression (com.jetbrains.python.psi.PyNumericLiteralExpression)3 Editor (com.intellij.openapi.editor.Editor)2 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)2 LanguageLevel (com.jetbrains.python.psi.LanguageLevel)2 PyFunction (com.jetbrains.python.psi.PyFunction)2 TemplateBuilder (com.intellij.codeInsight.template.TemplateBuilder)1 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)1 Document (com.intellij.openapi.editor.Document)1 LogicalPosition (com.intellij.openapi.editor.LogicalPosition)1 FileEditor (com.intellij.openapi.fileEditor.FileEditor)1 OpenFileDescriptor (com.intellij.openapi.fileEditor.OpenFileDescriptor)1 TextEditor (com.intellij.openapi.fileEditor.TextEditor)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PsiComment (com.intellij.psi.PsiComment)1