Surrounder () { return new PyExpressionSurrounder() { @Override public boolean isApplicable(@NotNull PyExpression expr) { return true; } @Override public TextRange surroundExpression(@NotNull Project project, @NotNull Editor editor, @NotNull PyExpression expression) throws IncorrectOperationException { LanguageLevel level = LanguageLevel.forElement(expression); String textToGenerate = !level.isPython2() ? "print(a)" : "print a"; PyStatement pyStatement = PyElementGenerator.getInstance(project).createFromText(level, PyStatement.class, textToGenerate); if (level.isPython2()) { pyStatement.getLastChild().replace(expression); } else { PyArgumentList argumentList = PsiTreeUtil.findChildOfType(pyStatement, PyArgumentList.class); if (argumentList == null) { return null; } argumentList.getArguments()[0].replace(expression); } pyStatement = (PyStatement)CodeStyleManager.getInstance(project).reformat(pyStatement); pyStatement = (PyStatement)expression.getParent().replace(pyStatement); return TextRange.from(pyStatement.getTextRange().getEndOffset(), 0); } @Override public String getTemplateDescription() { return DESCR; } }; }