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() + ")"));
}
}
}
Aggregations