PsiElement (@NotNull PsiElement element, @NotNull PyStatementList statementList, boolean toTheBeginning) { final PsiElement prevElem = PyPsiUtils.getPrevNonWhitespaceSibling(statementList); // If statement list is on the same line as previous element (supposedly colon), move its only statement on the next line if (prevElem != null && PyUtil.onSameLine(statementList, prevElem)) { final PsiDocumentManager manager = PsiDocumentManager.getInstance(statementList.getProject()); final Document document = manager.getDocument(statementList.getContainingFile()); if (document != null) { final PyStatementListContainer container = (PyStatementListContainer)statementList.getParent(); manager.doPostponedOperationsAndUnblockDocument(document); final String indentation = "\n" + PyIndentUtil.getElementIndent(statementList); // If statement list was empty initially, we need to add some anchor statement ("pass"), so that preceding new line was not // parsed as following entire StatementListContainer (e.g. function). It's going to be replaced anyway. final String text = statementList.getStatements().length == 0 ? indentation + PyNames.PASS : indentation; document.insertString(statementList.getTextRange().getStartOffset(), text); manager.commitDocument(document); statementList = container.getStatementList(); } } final PsiElement firstChild = statementList.getFirstChild(); if (firstChild == statementList.getLastChild() && firstChild instanceof PyPassStatement) { element = firstChild.replace(element); } else { final PyStatement[] statements = statementList.getStatements(); if (toTheBeginning && statements.length > 0) { final PyDocStringOwner docStringOwner = PsiTreeUtil.getParentOfType(statementList, PyDocStringOwner.class); PyStatement anchor = statements[0]; if (docStringOwner != null && anchor instanceof PyExpressionStatement && ((PyExpressionStatement)anchor).getExpression() == docStringOwner.getDocStringExpression()) { final PyStatement next = PsiTreeUtil.getNextSiblingOfType(anchor, PyStatement.class); if (next == null) { return statementList.addAfter(element, anchor); } anchor = next; } while (anchor instanceof PyExpressionStatement) { final PyExpression expression = ((PyExpressionStatement)anchor).getExpression(); if (expression instanceof PyCallExpression) { final PyExpression callee = ((PyCallExpression)expression).getCallee(); if ((PyUtil.isSuperCall((PyCallExpression)expression) || (callee != null && PyNames.INIT.equals(callee.getName())))) { final PyStatement next = PsiTreeUtil.getNextSiblingOfType(anchor, PyStatement.class); if (next == null) { return statementList.addAfter(element, anchor); } anchor = next; continue; } } break; } element = statementList.addBefore(element, anchor); } else { element = statementList.add(element); } } return element; }