Search in sources :

Example 1 with PyExceptPart

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

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

the class TryExceptAnnotator method visitPyTryExceptStatement.

@Override
public void visitPyTryExceptStatement(final PyTryExceptStatement node) {
    PyExceptPart[] exceptParts = node.getExceptParts();
    boolean haveDefaultExcept = false;
    for (PyExceptPart part : exceptParts) {
        if (haveDefaultExcept) {
            getHolder().createErrorAnnotation(part, PyBundle.message("ANN.default.except.must.be.last"));
        }
        if (part.getExceptClass() == null) {
            haveDefaultExcept = true;
        }
    }
}
Also used : PyExceptPart(com.jetbrains.python.psi.PyExceptPart)

Example 3 with PyExceptPart

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

the class PyExceptPartNavigator method getPyExceptPartByTarget.

@Nullable
public static PyExceptPart getPyExceptPartByTarget(final PsiElement element) {
    final PyExceptPart pyExceptPart = PsiTreeUtil.getParentOfType(element, PyExceptPart.class, false);
    if (pyExceptPart == null) {
        return null;
    }
    final PyExpression expr = pyExceptPart.getTarget();
    if (expr != null && PsiTreeUtil.isAncestor(expr, element, false)) {
        return pyExceptPart;
    }
    return null;
}
Also used : PyExpression(com.jetbrains.python.psi.PyExpression) PyExceptPart(com.jetbrains.python.psi.PyExceptPart) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with PyExceptPart

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

the class PyReachingDefsDfaInstance method fun.

public DFAMap<ScopeVariable> fun(final DFAMap<ScopeVariable> map, final Instruction instruction) {
    final PsiElement element = instruction.getElement();
    if (element == null || !((PyFile) element.getContainingFile()).getLanguageLevel().isPy3K()) {
        return processReducedMap(map, instruction, element);
    }
    // Scope reduction
    final DFAMap<ScopeVariable> reducedMap = new DFAMap<>();
    for (Map.Entry<String, ScopeVariable> entry : map.entrySet()) {
        final ScopeVariable value = entry.getValue();
        // Support PEP-3110. (PY-1408)
        if (value.isParameter()) {
            final PsiElement declaration = value.getDeclarations().iterator().next();
            final PyExceptPart exceptPart = PyExceptPartNavigator.getPyExceptPartByTarget(declaration);
            if (exceptPart != null) {
                if (!PsiTreeUtil.isAncestor(exceptPart, element, false)) {
                    continue;
                }
            }
        }
        reducedMap.put(entry.getKey(), value);
    }
    return processReducedMap(reducedMap, instruction, element);
}
Also used : ScopeVariable(com.jetbrains.python.codeInsight.dataflow.scope.ScopeVariable) PyExceptPart(com.jetbrains.python.psi.PyExceptPart) DFAMap(com.intellij.codeInsight.dataflow.map.DFAMap) Map(java.util.Map) PsiElement(com.intellij.psi.PsiElement) DFAMap(com.intellij.codeInsight.dataflow.map.DFAMap)

Aggregations

PyExceptPart (com.jetbrains.python.psi.PyExceptPart)4 PsiElement (com.intellij.psi.PsiElement)2 DFAMap (com.intellij.codeInsight.dataflow.map.DFAMap)1 ASTNode (com.intellij.lang.ASTNode)1 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)1 ScopeVariable (com.jetbrains.python.codeInsight.dataflow.scope.ScopeVariable)1 PyElementGenerator (com.jetbrains.python.psi.PyElementGenerator)1 PyExpression (com.jetbrains.python.psi.PyExpression)1 PyTryExceptStatement (com.jetbrains.python.psi.PyTryExceptStatement)1 Map (java.util.Map)1 Nullable (org.jetbrains.annotations.Nullable)1