void (@NotNull final Editor editor, @Nullable Caret caret, DataContext dataContext) { Project project = editor.getProject(); assert project != null; FoldingModel foldingModel = editor.getFoldingModel(); CodeFoldingManager codeFoldingManager = CodeFoldingManager.getInstance(project); final List<FoldRegion> regions = getFoldRegionsForSelection(editor, caret); List<FoldRegion> expandedRegions = new ArrayList<>(); foldingModel.runBatchFoldingOperation(() -> { for (FoldRegion region : regions) { // try to restore to default state at first Boolean collapsedByDefault = codeFoldingManager.isCollapsedByDefault(region); if (!region.isExpanded() && !region.shouldNeverExpand() && (collapsedByDefault == null || !collapsedByDefault)) { region.setExpanded(true); expandedRegions.add(region); } } }); for (FoldRegion expandedRegion : expandedRegions) { FoldRegion collapsedRegion = foldingModel.getCollapsedRegionAtOffset(expandedRegion.getStartOffset()); if (collapsedRegion == null || !collapsedRegion.shouldNeverExpand()) { // restoring to default state produced visible change return; } } foldingModel.runBatchFoldingOperation(() -> { for (FoldRegion region : regions) { region.setExpanded(true); } }); }