String (@NotNull PsiFile file, @NotNull String text) { if (!fragmentBeginsWithBlockStatement(text)) { return text; } final PyExpressionCodeFragmentImpl fragment = new PyExpressionCodeFragmentImpl(file.getProject(), "dummy.py", text, false); //fragment.setContext(file); final PyStatementListContainer statement = as(fragment.getFirstChild(), PyStatementListContainer.class); if (statement == null) { return text; } final String statementIndent = PyIndentUtil.getElementIndent(statement); if (!statementIndent.isEmpty()) { return text; } final String indentStep = PyIndentUtil.getIndentFromSettings(file); final String bodyIndent = PyIndentUtil.getElementIndent(statement.getStatementList()); final String expectedBodyIndent = statementIndent + indentStep; if (bodyIndent.startsWith(expectedBodyIndent)) { return bodyIndent.substring(0, bodyIndent.length() - indentStep.length()) + text; } return text; }