TextRange (@NotNull Editor editor, @NotNull String text) { Caret caret = editor.getCaretModel().getCurrentCaret(); CaretLineState caretLineState = CaretLineState.create(caret); int lineStartOffset = caretLineState.lineStartOffset; if (!text.endsWith("\n")) text += "\n"; editor.getDocument().insertString(lineStartOffset, text); caretLineState.moveCaretAfterInsertionIfNeeded(lineStartOffset, text.length()); EditorModificationUtilEx.scrollToCaret(editor); return TextRange.from(lineStartOffset, text.length()); }