LineRange (@NotNull final PsiElement elementToMove, @NotNull final Editor editor, @NotNull final PsiElement rawElement, boolean down) { final Document document = editor.getDocument(); PsiElement element = getCommentOrStatement(document, rawElement); final PyStatementList statementList = getStatementList(elementToMove); final PsiElement scopeForComment = statementList == null ? null : getScopeForComment(elementToMove, editor, elementToMove, down); PyStatementList statementList2 = getStatementList(element); final int start1 = elementToMove.getTextOffset() - document.getLineStartOffset(document.getLineNumber(elementToMove.getTextOffset())); final int start2 = element.getTextOffset() - document.getLineStartOffset(document.getLineNumber(element.getTextOffset())); if (start1 != start2) { PyStatementList parent2 = PsiTreeUtil.getParentOfType(statementList2, PyStatementList.class); while (parent2 != scopeForComment && parent2 != null) { element = PsiTreeUtil.getParentOfType(statementList2, PyStatement.class); statementList2 = parent2; parent2 = PsiTreeUtil.getParentOfType(parent2, PyStatementList.class); } } if (statementList2 != null && scopeForComment != statementList2 && (statementList2.getLastChild() == element || statementList2.getLastChild() == elementToMove) && element != null) { return new ScopeRange(statementList2, element, false); } return null; }