void (@NotNull Project project, @NotNull final Editor editor, @NotNull PsiFile file) { editor.getFoldingModel().runBatchFoldingOperation( () -> { final EditorFoldingInfo info = EditorFoldingInfo.get(editor); FoldingModelEx foldingModel = (FoldingModelEx) editor.getFoldingModel(); int start = editor.getSelectionModel().getSelectionStart(); int end = editor.getSelectionModel().getSelectionEnd(); if (start < end) { Document doc = editor.getDocument(); if (doc.getCharsSequence().charAt(end - 1) == '\n') { end--; } FoldRegion region; if ((region = FoldingUtil.findFoldRegion(editor, start, end)) != null) { if (info.getPsiElement(region) == null) { editor.getFoldingModel().removeFoldRegion(region); info.removeRegion(region); } else { HintManager.getInstance().showInformationHint(editor, CodeInsightBundle.message("collapse.selection.existing.autogenerated.region")); } } else { if (foldingModel.intersectsRegion(start, end)) { if (Messages.showDialog(project, CodeInsightBundle.message("collapse.selection.overlapping.warning.text"), CodeInsightBundle.message("collapse.selection.overlapping.warning.title"), new String[]{ CodeInsightBundle.message("collapse.selection.overlapping.warning.ok"), CodeInsightBundle.message("collapse.selection.overlapping.warning.cancel")}, 1, Messages.getWarningIcon()) != 0) { return; } for (FoldRegion r : foldingModel.getAllFoldRegions()) { if (r.getStartOffset() < start && r.getEndOffset() > start && r.getEndOffset() < end || r.getStartOffset() > start && r.getStartOffset() < end && r.getEndOffset() > end) { foldingModel.removeFoldRegion(r); } } } if (start < end) { region = foldingModel.addFoldRegion(start, end, ourPlaceHolderText); LOG.assertTrue(region != null, "Fold region is not created for (" +start+","+end+ "). Folding model: " + foldingModel + foldingModel.getClass()); region.setExpanded(false); int offset = Math.min(start + ourPlaceHolderText.length(), doc.getTextLength()); editor.getCaretModel().moveToOffset(offset); } } } else { FoldRegion[] regions = FoldingUtil.getFoldRegionsAtOffset(editor, editor.getCaretModel().getOffset()); if (regions.length > 0) { FoldRegion region = regions[0]; if (info.getPsiElement(region) == null) { editor.getFoldingModel().removeFoldRegion(region); info.removeRegion(region); } else { region.setExpanded(!region.isExpanded()); } } } } ); }