void (@NotNull Environment env) { PsiFile psiFile = getFile(); int startOffset = generatePsiOffset(env, null); int endOffset = Math.min(psiFile.getTextLength(), startOffset + env.generateValue(Generator.integers(IntDistribution.geometric(10)).noShrink(), null)); PsiElement start = psiFile.findElementAt(startOffset); PsiElement end = psiFile.findElementAt(endOffset); if (start == null || end == null) return; PsiElement commonParent = PsiTreeUtil.findCommonParent(start, end); if (commonParent == null || commonParent.getTextRange() == null) { // directory; for multi-root files return; } TextRange range = commonParent.getTextRange().intersection(TextRange.from(0, getDocument().getTextLength())); if (range != null && !range.isEmpty()) { env.logMessage("Delete " + range); WriteCommandAction.runWriteCommandAction(getProject(), () -> getDocument().deleteString(range.getStartOffset(), range.getEndOffset())); } }