Search in sources :

Example 1 with PyRaiseStatement

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

Aggregations

PsiElement (com.intellij.psi.PsiElement)1 PyElementGenerator (com.jetbrains.python.psi.PyElementGenerator)1 PyExpression (com.jetbrains.python.psi.PyExpression)1 PyRaiseStatement (com.jetbrains.python.psi.PyRaiseStatement)1