List<FoldRegion> (@NotNull Editor editor, @Nullable Caret caret, boolean toCollapse) { if (caret == null) { caret = editor.getCaretModel().getPrimaryCaret(); } int offset = caret.getOffset(); FoldRegion rootRegion = FoldingUtil.findFoldRegionStartingAtLine(editor, editor.getDocument().getLineNumber(offset)); if (rootRegion == null || toCollapse && !rootRegion.isExpanded()) { rootRegion = null; FoldRegion[] regions = FoldingUtil.getFoldRegionsAtOffset(editor, offset); for (FoldRegion region : regions) { if (region.isExpanded() == toCollapse) { rootRegion = region; break; } } } List<FoldRegion> result = new ArrayList<>(); if (rootRegion != null) { FoldRegion[] allRegions = editor.getFoldingModel().getAllFoldRegions(); for (FoldRegion region : allRegions) { if (region.getStartOffset() >= rootRegion.getStartOffset() && region.getEndOffset() <= rootRegion.getEndOffset()) { result.add(region); } } } return result; }