Search in sources :

Example 1 with PyTryExceptStatement

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

Aggregations

ASTNode (com.intellij.lang.ASTNode)1 PsiElement (com.intellij.psi.PsiElement)1 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)1 PyElementGenerator (com.jetbrains.python.psi.PyElementGenerator)1 PyExceptPart (com.jetbrains.python.psi.PyExceptPart)1 PyTryExceptStatement (com.jetbrains.python.psi.PyTryExceptStatement)1