void (@NotNull final Editor editor, @Nullable Caret caret, DataContext dataContext) { final int[] lines = editor.getCaretModel().getAllCarets().stream() .map(Caret::getLogicalPosition) .mapToInt(it -> it.line) .toArray(); Runnable processor = () -> { for (int line : lines) { FoldRegion region = FoldingUtil.findFoldRegionStartingAtLine(editor, line); if (region != null && region.isExpanded()) { region.setExpanded(false); } else { int offset = editor.getCaretModel().getOffset(); FoldRegion[] regions = FoldingUtil.getFoldRegionsAtOffset(editor, offset); for (FoldRegion region1 : regions) { if (region1.isExpanded()) { region1.setExpanded(false); break; } } } } }; editor.getFoldingModel().runBatchFoldingOperation(processor); }