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 visible_blocks(edit, rect=None): """Yield the visible blocks in the specified rectangle. If no rectangle is given, the edit's viewport() is used. """ if rect is None: rect = edit.viewport().rect() offset = edit.contentOffset() for block in cursortools.forwards(edit.firstVisibleBlock()): if block.isVisible(): geom = edit.blockBoundingGeometry(block).translated(offset).toRect() if geom.top() >= rect.bottom(): return elif geom.bottom() >= rect.top(): yield block
def fold(self, block, depth=0): """Fold the region the block is in. 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. """ r = self.region(block, depth) if not r: return # if the last block starts a new region, don't hide it count = 0 end = r.end.previous() if self.fold_level(r.end).start else r.end # don't hide the first block of the region for block in cursortools.forwards(r.start.next(), end): block.setVisible(False) self.mark(r.start, True) self.document().markContentsDirty(r.start.next().position(), end.position())
def fold(self, block, depth=0): """Fold the region the block is in. 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. """ r = self.region(block, depth) if not r: return # if the last block starts a new region, don't hide it count = 0 end = r.end.previous() if self.fold_level(r.end).start else r.end # don't hide the first block of the region for block in cursortools.forwards(r.start.next(), end): block.setVisible(False) self.mark(r.start, True) self.document().markContentsDirty(r.start.next().position(), end.position()) self._all_visible = False
def unfold(self, block, depth=0, full=False): """Unfolds the region the block is in. (Most times the block will be the first block of the region.) If multiple regions start at the same starting block, they will unfold all. 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. If full is False (the default) sub-regions that were collapsed remain collapsed (provided that the mark() method is implemented). """ r = self.region(block, depth) if not r: return blocks = cursortools.forwards(r.start, r.end) for block in blocks: # is there a sub-region? then skip if marked as collapsed if block not in r: l = self.fold_level(block) if l.start: if full: self.mark(block, False) elif self.mark(block): count = l.start for b in blocks: l = self.fold_level(b) if count <= -l.stop: break count += sum(l) block.setVisible(True) self.mark(r.start, False) start = r.start.position() self.document().markContentsDirty(start, r.end.position() - start)