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