int (@NotNull Editor editor, int startOffset, boolean stopAtInvisibleFoldRegions) { return ReadAction.compute(() -> { int offset = startOffset; while (true) { offset = DocumentUtil.getLineStartOffset(offset, editor.getDocument()); FoldRegion foldRegion = editor.getFoldingModel().getCollapsedRegionAtOffset(offset - 1); if (foldRegion == null || stopAtInvisibleFoldRegions && foldRegion.getPlaceholderText().isEmpty() || foldRegion.getStartOffset() >= offset) { break; } offset = foldRegion.getStartOffset(); } return offset; }); }