void (Editor editor, Random random) { Document document = editor.getDocument(); int textLength = document.getTextLength(); if (textLength <= 0) return; int from = random.nextInt(textLength + 1); int to = random.nextInt(textLength + 1); CharSequence text = generateText(random); WriteCommandAction.runWriteCommandAction(getProject(), () -> document.replaceString(Math.min(from, to), Math.max(from, to), text)); }