void (final EditorEx editor, Random random) { DocumentEx document = editor.getDocument(); if (document.isInBulkUpdate()) return; final FoldingModel foldingModel = editor.getFoldingModel(); FoldRegion[] foldRegions = foldingModel.getAllFoldRegions(); if (foldRegions.length == 0) return; int operations = random.nextInt(foldRegions.length) + 1; foldingModel.runBatchFoldingOperation(() -> { for (int i = 0; i < operations; i++) { FoldRegion region = foldRegions[random.nextInt(foldRegions.length)]; region.setExpanded(!region.isExpanded()); } }); }