List<FoldRegion> (@NotNull Editor editor, @Nullable Caret caret) { FoldRegion[] allRegions = editor.getFoldingModel().getAllFoldRegions(); if (caret == null) { caret = editor.getCaretModel().getPrimaryCaret(); } if (caret.hasSelection()) { List<FoldRegion> result = new ArrayList<>(); for (FoldRegion region : allRegions) { if (region.getStartOffset() >= caret.getSelectionStart() && region.getEndOffset() <= caret.getSelectionEnd()) { result.add(region); } } if (!result.isEmpty()) { return result; } } return Arrays.asList(allRegions); }