ScopeRange (@NotNull final PsiElement elementToMove, @NotNull final Editor editor, boolean down) { final PyStatementList statementList = getStatementList(elementToMove); if (statementList == null) return null; if ((!down || statementList.getLastChild() != elementToMove) && (down || statementList.getFirstChild() != elementToMove)) { return null; } boolean addBefore = !down; final PsiElement parent = statementList.getParent(); final PyStatementPart sibling = down ? PsiTreeUtil.getNextSiblingOfType(parent, PyStatementPart.class) : PsiTreeUtil.getPrevSiblingOfType(parent, PyStatementPart.class); if (sibling != null) { final PyStatementList list = sibling.getStatementList(); return new ScopeRange(list, down ? list.getFirstChild() : list.getLastChild(), !addBefore); } else { PsiElement scope = getScopeForComment(elementToMove, editor, parent, !down); PsiElement anchor = PsiTreeUtil.getParentOfType(statementList, PyStatement.class); return scope == null || anchor == null ? null : new ScopeRange(scope, anchor, addBefore); } }