FoldedBlock (@NotNull Data data, @NotNull Data.Block block, boolean expanded) { FoldRegion[] regions = new FoldRegion[myCount]; String[] cachedDescriptions = null; for (int i = 0; i < myCount; i++) { LineRange range = block.ranges[i]; if (range != null) regions[i] = addFolding(myEditors[i], range.start, range.end, expanded); } boolean hasFolding = ContainerUtil.or(regions, Objects::nonNull); boolean hasExpanded = ContainerUtil.or(regions, region -> region != null && region.isExpanded()); // do not desync regions on runBatchFoldingOperationDoNotCollapseCaret if (hasExpanded && !expanded) { for (FoldRegion region : regions) { if (region != null) region.setExpanded(true); } } if (!hasExpanded && !expanded) { cachedDescriptions = new String[myCount]; for (int i = 0; i < myCount; i++) { LineRange range = block.ranges[i]; if (range != null) { cachedDescriptions[i] = myExpandSuggester.getCachedDescription(range.start, range.end, i); } } } return hasFolding ? new FoldedBlock(regions, data.descriptionComputer, cachedDescriptions) : null; }