void (@NotNull Editor editor, int startOffset, int endOffset) { ReadAction.run(() -> { FoldingModel foldingModel = editor.getFoldingModel(); FoldRegion startFoldRegion = foldingModel.getCollapsedRegionAtOffset(startOffset); if (startFoldRegion != null && (startFoldRegion.getStartOffset() == startOffset || startFoldRegion.isExpanded())) { startFoldRegion = null; } FoldRegion endFoldRegion = foldingModel.getCollapsedRegionAtOffset(endOffset); if (endFoldRegion != null && (endFoldRegion.getStartOffset() == endOffset || endFoldRegion.isExpanded())) { endFoldRegion = null; } if (startFoldRegion != null || endFoldRegion != null) { final FoldRegion finalStartFoldRegion = startFoldRegion; final FoldRegion finalEndFoldRegion = endFoldRegion; foldingModel.runBatchFoldingOperation(() -> { if (finalStartFoldRegion != null) finalStartFoldRegion.setExpanded(true); if (finalEndFoldRegion != null) finalEndFoldRegion.setExpanded(true); }); } editor.getSelectionModel().setSelection(startOffset, endOffset); }); }