void (@NotNull EditorFoldingInfo info, @NotNull FoldingModelEx foldingModel, @NotNull Map<TextRange, Boolean> rangeToExpandStatusMap) { List<FoldRegion> toRemove = new ArrayList<>(); Ref<FoldingUpdate.RegionInfo> infoRef = Ref.create(); Set<FoldingGroup> processedGroups = new HashSet<>(); List<FoldingUpdate.RegionInfo> matchedInfos = new ArrayList<>(); for (FoldRegion region : foldingModel.getAllFoldRegions()) { FoldingGroup group = region.getGroup(); if (group != null && !processedGroups.add(group)) continue; List<FoldRegion> regionsToProcess = group == null ? Collections.singletonList(region) : foldingModel.getGroupedRegions(group); matchedInfos.clear(); boolean shouldRemove = false; boolean isLight = true; for (FoldRegion regionToProcess : regionsToProcess) { if (!regionToProcess.isValid() || shouldRemoveRegion(regionToProcess, info, rangeToExpandStatusMap, infoRef)) { shouldRemove = true; } isLight &= regionToProcess.getUserData(SIGNATURE) == null; FoldingUpdate.RegionInfo regionInfo = infoRef.get(); matchedInfos.add(regionInfo); } if (!shouldRemove && group != null && !isLight) { FoldingGroup requestedGroup = null; for (FoldingUpdate.RegionInfo matchedInfo : matchedInfos) { if (matchedInfo == null) { shouldRemove = true; break; } FoldingGroup g = matchedInfo.descriptor.getGroup(); if (g == null) { shouldRemove = true; break; } if (requestedGroup == null) { requestedGroup = g; } else if (!requestedGroup.equals(g)) { shouldRemove = true; break; } } if (myGroupedRegionInfos.get(requestedGroup).size() != matchedInfos.size()) { shouldRemove = true; } } if (shouldRemove) { for (FoldRegion r : regionsToProcess) { rangeToExpandStatusMap.putIfAbsent(r.getTextRange(), r.isExpanded()); } toRemove.addAll(regionsToProcess); } else { for (FoldingUpdate.RegionInfo matchedInfo : matchedInfos) { if (matchedInfo != null) { myElementsToFoldMap.remove(matchedInfo.element, matchedInfo); myRegionInfos.remove(matchedInfo); } } } } for (final FoldRegion region : toRemove) { foldingModel.removeFoldRegion(region); info.removeRegion(region); } }