Result (@NotNull PsiFile file, @NotNull Editor editor, @NotNull Ref<Integer> caretOffsetRef, @NotNull Ref<Integer> caretAdvanceRef, @NotNull DataContext dataContext, EditorActionHandler originalHandler) { int offset = editor.getCaretModel().getOffset(); PsiElement textBlock = getTextBlockAt(file, offset); if (textBlock == null) return Result.Continue; int textBlockOffset = textBlock.getTextOffset(); String text = textBlock.getText(); int offsetInTextBlock = offset - textBlockOffset; boolean isAtFirstLine = !text.substring(0, offsetInTextBlock).contains("\n"); if (!isAtFirstLine) return Result.Continue; Document document = editor.getDocument(); Project project = textBlock.getProject(); int secondLineStart = text.indexOf('\n'); if (secondLineStart == -1) { document.insertString(offset, "\n"); PsiDocumentManager.getInstance(project).commitDocument(document); CodeStyleManager.getInstance(project).reformat(textBlock); text = textBlock.getText(); int indent = getIndent(text, offsetInTextBlock + 1); if (indent == -1) return Result.Continue; editor.getCaretModel().moveToOffset(offset + 1 + indent); } else { int indent = getIndent(text, secondLineStart + 1); if (indent == -1) return Result.Continue; String newLine = '\n' + StringUtil.repeatSymbol(' ', indent); document.insertString(offset, newLine); PsiDocumentManager.getInstance(project).commitDocument(document); editor.getCaretModel().moveToOffset(offset + newLine.length()); } return Result.Stop; }