FoldRegion (@NotNull VisualPosition visualPos) { FoldRegion[] topLevelCollapsed = myFoldingModel.fetchTopLevel(); if (topLevelCollapsed == null) return null; int start = 0; int end = topLevelCollapsed.length - 1; int i = 0; while (start <= end) { i = (start + end) / 2; FoldRegion region = topLevelCollapsed[i]; if (!region.isValid()) { // Folding model is inconsistent (update in progress). return null; } int regionVisualLine = offsetToVisualLine(region.getEndOffset() - 1); if (regionVisualLine < visualPos.line) { start = i + 1; } else { if (regionVisualLine > visualPos.line) { end = i - 1; } else { VisualPosition visFoldEnd = offsetToVisualPosition(region.getEndOffset() - 1); if (visFoldEnd.column < visualPos.column) { start = i + 1; } else { if (visFoldEnd.column > visualPos.column) { end = i - 1; } else { i--; break; } } } } } while (i >= 0 && i < topLevelCollapsed.length) { if (topLevelCollapsed[i].isValid()) break; i--; } if (i >= 0 && i < topLevelCollapsed.length) { FoldRegion region = topLevelCollapsed[i]; VisualPosition visFoldEnd = offsetToVisualPosition(region.getEndOffset() - 1); if (visFoldEnd.line > visualPos.line || visFoldEnd.line == visualPos.line && visFoldEnd.column > visualPos.column) { i--; if (i >= 0) { return topLevelCollapsed[i]; } return null; } return region; } return null; }