def region(self, block, depth=0): """Return as Region (start, end) the region of the specified block. start is the block the region starts, end the block the region ends. When collapsing the block, don't hide the last block if it starts a new fold region. The depth argument specifies how deep a region may be nested. The default value 0 searches the first containing region, 1 tries to find one more above that, etc. Use -1 to get the top-most region. """ start = None start_depth = 0 count = 0 for b in cursortools.backwards(block): l = self.fold_level(b) if l.start: count += l.start if count > start_depth: start = b start_depth = count if count > depth > -1: break count += l.stop if start: count = start_depth end = None for end in cursortools.forwards(block.next()): l = self.fold_level(end) if count <= -l.stop: return Region(start, end) count += sum(l) if end: return Region(start, end)
def depth(self, block): """Return the number of active regions at the start of this block. The default implementation simply counts all the fold_events from the beginning of the document. """ count = 0 for block in cursortools.backwards(block.previous()): count += sum(self.fold_events(block)) return count