Search in sources :

Example 21 with PyElementGenerator

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

the class UnresolvedReferenceAddParameterQuickFix method applyFix.

public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    final PsiElement element = descriptor.getPsiElement();
    final PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
    final PyNamedParameter parameter = elementGenerator.createParameter(element.getText() + "=None");
    final PyFunction function = PsiTreeUtil.getParentOfType(element, PyFunction.class);
    if (function != null) {
        final PyParameterList parameterList = function.getParameterList();
        parameterList.addParameter(parameter);
        CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(parameterList);
        final TemplateBuilder builder = TemplateBuilderFactory.getInstance().createTemplateBuilder(parameter);
        builder.replaceRange(TextRange.create(parameter.getTextLength() - 4, parameter.getTextLength()), "None");
        final VirtualFile virtualFile = function.getContainingFile().getVirtualFile();
        if (virtualFile == null)
            return;
        final Editor editor = FileEditorManager.getInstance(project).openTextEditor(new OpenFileDescriptor(project, virtualFile), true);
        if (editor == null)
            return;
        builder.run(editor, false);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PyFunction(com.jetbrains.python.psi.PyFunction) TemplateBuilder(com.intellij.codeInsight.template.TemplateBuilder) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) PyElementGenerator(com.jetbrains.python.psi.PyElementGenerator) PyNamedParameter(com.jetbrains.python.psi.PyNamedParameter) Editor(com.intellij.openapi.editor.Editor) PyParameterList(com.jetbrains.python.psi.PyParameterList) PsiElement(com.intellij.psi.PsiElement)

Example 22 with PyElementGenerator

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

the class UnresolvedReferenceAddSelfQuickFix method applyFix.

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

Example 23 with PyElementGenerator

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

the class ReplaceRaiseStatementQuickFix method applyFix.

@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    PsiElement raiseStatement = descriptor.getPsiElement();
    if (raiseStatement instanceof PyRaiseStatement) {
        PyExpression[] expressions = ((PyRaiseStatement) raiseStatement).getExpressions();
        PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
        String newExpressionText = expressions[0].getText() + "(" + expressions[1].getText() + ")";
        if (expressions.length == 2) {
            raiseStatement.replace(elementGenerator.createFromText(LanguageLevel.forElement(raiseStatement), PyRaiseStatement.class, "raise " + newExpressionText));
        } else if (expressions.length == 3) {
            raiseStatement.replace(elementGenerator.createFromText(LanguageLevel.forElement(raiseStatement), PyRaiseStatement.class, "raise " + newExpressionText + ".with_traceback(" + expressions[2].getText() + ")"));
        }
    }
}
Also used : PyExpression(com.jetbrains.python.psi.PyExpression) PyRaiseStatement(com.jetbrains.python.psi.PyRaiseStatement) PyElementGenerator(com.jetbrains.python.psi.PyElementGenerator) PsiElement(com.intellij.psi.PsiElement)

Example 24 with PyElementGenerator

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

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

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