void (final @NotNull Project project, final @NotNull Editor editor, final @NotNull RangeMarker bounds, int caretOffset, @NotNull Ref<? super Boolean> indented, final @NotNull List<? extends FoldingTransferableData> values) { assert values.size() == 1; final FoldingTransferableData value = values.get(0); if (value.getData().length == 0 || indented.get() != null) { // if `indented` is TRUE or FALSE, the pasted text was changes and folding offsets are not valid return; } final CodeFoldingManagerImpl foldingManager = (CodeFoldingManagerImpl)CodeFoldingManager.getInstance(project); if (foldingManager == null) return; // default project Runnable operation = () -> { final FoldingModel model = editor.getFoldingModel(); final int docLength = editor.getDocument().getTextLength(); for (FoldingData data : value.getData()) { final int start = data.startOffset + bounds.getStartOffset(); final int end = data.endOffset + bounds.getStartOffset(); if (start >= 0 && end <= docLength && start <= end) { final FoldRegion region = model.addFoldRegion(start, end, data.placeholderText); if (region != null) { foldingManager.markForUpdate(region); region.setExpanded(data.isExpanded); } } } }; int verticalPositionBefore = editor.getScrollingModel().getVisibleAreaOnScrollingFinished().y; editor.getFoldingModel().runBatchFoldingOperation(operation); EditorUtil.runWithAnimationDisabled(editor, () -> editor.getScrollingModel().scrollVertically(verticalPositionBefore)); }