Search in sources :

Example 6 with PyElementGenerator

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

the class ReplaceExceptPartQuickFix method applyFix.

@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    PsiElement exceptPart = descriptor.getPsiElement();
    if (exceptPart instanceof PyExceptPart) {
        PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
        PsiElement element = ((PyExceptPart) exceptPart).getExceptClass().getNextSibling();
        while (element instanceof PsiWhiteSpace) {
            element = element.getNextSibling();
        }
        assert element != null;
        PyTryExceptStatement newElement = elementGenerator.createFromText(LanguageLevel.forElement(exceptPart), PyTryExceptStatement.class, "try:  pass except a as b:  pass");
        ASTNode node = newElement.getExceptParts()[0].getNode().findChildByType(PyTokenTypes.AS_KEYWORD);
        assert node != null;
        element.replace(node.getPsi());
    }
}
Also used : ASTNode(com.intellij.lang.ASTNode) PyExceptPart(com.jetbrains.python.psi.PyExceptPart) PyElementGenerator(com.jetbrains.python.psi.PyElementGenerator) PsiElement(com.intellij.psi.PsiElement) PyTryExceptStatement(com.jetbrains.python.psi.PyTryExceptStatement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace)

Example 7 with PyElementGenerator

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

the class ReplaceOctalNumericLiteralQuickFix method applyFix.

@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    PsiElement numericLiteralExpression = descriptor.getPsiElement();
    if (numericLiteralExpression instanceof PyNumericLiteralExpression) {
        PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
        String text = numericLiteralExpression.getText();
        numericLiteralExpression.replace(elementGenerator.createExpressionFromText("0o" + text.substring(1)));
    }
}
Also used : PyNumericLiteralExpression(com.jetbrains.python.psi.PyNumericLiteralExpression) PyElementGenerator(com.jetbrains.python.psi.PyElementGenerator) PsiElement(com.intellij.psi.PsiElement)

Example 8 with PyElementGenerator

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

the class UnresolvedRefAddFutureImportQuickFix method applyFix.

public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    PsiElement element = descriptor.getPsiElement();
    PyFile file = (PyFile) element.getContainingFile();
    PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
    PyFromImportStatement statement = elementGenerator.createFromText(LanguageLevel.forElement(element), PyFromImportStatement.class, "from __future__ import with_statement");
    file.addBefore(statement, file.getStatements().get(0));
}
Also used : PyFromImportStatement(com.jetbrains.python.psi.PyFromImportStatement) PyFile(com.jetbrains.python.psi.PyFile) PyElementGenerator(com.jetbrains.python.psi.PyElementGenerator) PsiElement(com.intellij.psi.PsiElement)

Example 9 with PyElementGenerator

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

the class RemovePrefixQuickFix method applyFix.

@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    final PyStringLiteralExpression pyString = as(descriptor.getPsiElement(), PyStringLiteralExpression.class);
    if (pyString != null) {
        final PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
        for (ASTNode node : pyString.getStringNodes()) {
            final String nodeText = node.getText();
            final int prefixLength = PyStringLiteralExpressionImpl.getPrefixLength(nodeText);
            if (nodeText.substring(0, prefixLength).equalsIgnoreCase(myPrefix)) {
                final PyStringLiteralExpression replacement = elementGenerator.createStringLiteralAlreadyEscaped(nodeText.substring(prefixLength));
                node.getPsi().replace(replacement.getFirstChild());
            }
        }
    }
}
Also used : PyStringLiteralExpression(com.jetbrains.python.psi.PyStringLiteralExpression) ASTNode(com.intellij.lang.ASTNode) PyElementGenerator(com.jetbrains.python.psi.PyElementGenerator)

Example 10 with PyElementGenerator

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

the class RemoveTrailingLQuickFix method applyFix.

@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    PsiElement numericLiteralExpression = descriptor.getPsiElement();
    if (numericLiteralExpression instanceof PyNumericLiteralExpression) {
        PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
        String text = numericLiteralExpression.getText();
        numericLiteralExpression.replace(elementGenerator.createExpressionFromText(text.substring(0, text.length() - 1)));
    }
}
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