void (final String bytecode, final int offset) { DocumentUtil.writeInRunUndoTransparentAction(() -> { Document fragmentDoc = myEditor.getDocument(); fragmentDoc.setReadOnly(false); try { fragmentDoc.replaceString(0, fragmentDoc.getTextLength(), bytecode); } finally { fragmentDoc.setReadOnly(true); } myEditor.getCaretModel().moveToOffset(offset); myEditor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE); }); }