void (final Editor editor, final String text, final int relativeOffset) { editor.getCaretModel().runForEachCaret(caret -> { int caretOffset = caret.getOffset(); int startOffset = Math.max(0, caretOffset - relativeOffset); editor.getDocument().replaceString(startOffset, caretOffset, text); caret.moveToOffset(startOffset + text.length()); }); }