class Context(object): """This object stores and updates contextual metadata about the buffer.""" def __init__(self, mode): self.mode = mode self.names = None self.namelines = None def region_added(self, p, newlines): self.adjust_name_map(p, len(newlines) - 1) self.rebuild_name_map(p.y, p.y + len(newlines)) def region_removed(self, p1, p2): self.adjust_name_map(p2, p1.y - p2.y) self.rebuild_name_map(p1.y, p1.y + 1) def adjust_name_map(self, p, delta): if delta == 0: return elif delta > 0: self.namelines.extend([None] * delta) for i in reversed(range(p.y + 1, len(self.mode.window.buffer.lines))): self.namelines[i] = self.namelines[i - delta] for i in range(p.y, p.y + delta): self.namelines[i] = None else: for i in range(p.y + 1, len(self.mode.window.buffer.lines)): self.namelines[i + delta] = self.namelines[i] def _init_name_map(self): self.names = {} self.namelines = [None] * len(self.mode.window.buffer.lines) def build_name_map(self): self._init_name_map() self._build_name_map(0, len(self.mode.window.buffer.lines), None, None, []) def rebuild_name_map(self, y1, y2): for y in range(y1, y2): name = self.namelines[y] if name: if name in self.names: del self.names[name] if name in self.classes: del self.classes[name] if name in self.functions: del self.functions[name] self.namelines[y] = None if y1 == 0: self._build_name_map(y1, y2, None, None, []) else: last, curr, stack = None, self.namelines[y1 - 1], [] count = 0 if curr: for part in curr.split('.'): stack.append([count, part]) count += self.mode.tabwidth self._build_name_map(y1, y2, last, curr, stack) def _build_name_map(self, y1, y2, last, curr, stack): raise Exception, 'unimplemented' def get_line_name(self, y): if self.namelines is None: self.build_name_map() if y < len(self.namelines): return self.namelines[y] else: return None def get_names(self): if self.names is None: self.build_name_map() return self.names def get_name_list(self): return self._ordered_dict(self.get_names()) def _ordered_dict(self, d): pairs = [[d[key], key] for key in d] pairs.sort() return [x[1] for x in pairs]