PsiElement (@Nullable PsiElement source, Editor editor) { if (source != null && source.getLanguage() instanceof PythonLanguage) { final PsiElement breakOrContinue = source.getParent(); final PyLoopStatement loop = PyUtil.getCorrespondingLoop(breakOrContinue); if (loop != null) { if (breakOrContinue instanceof PyContinueStatement) { return loop; } else { PsiElement outer_element = loop; PsiElement after_cycle; while (true) { after_cycle = outer_element.getNextSibling(); if (after_cycle != null) { if (after_cycle instanceof PsiWhiteSpace) { after_cycle = after_cycle.getNextSibling(); } if (after_cycle instanceof PyStatement) return after_cycle; } outer_element = outer_element.getParent(); if (PsiTreeUtil.instanceOf(outer_element, PsiFile.class, PyFunction.class, PyClass.class)) { break; } } // cycle is the last statement in the flow of execution. its last element is our best bet. return PsiTreeUtil.getDeepestLast(loop); } } } return null; }