import tab, point class CTabber(tab.TokenStackTabber): close_tags = {')': '(', ']': '[', '}': '{'} def stack_append_const(self, c): self.stack_append((c, self.tab_stack[-1][1] + 4)) def stack_append_unique_const(self, c): if self.tab_stack[-1][0] != c: self.stack_append((c, self.tab_stack[-1][1] + 4)) def stack_pop_const(self, *c_args): if self.tab_stack[-1][0] in c_args: self.stack_pop() def stack_pop_all_const(self, *c_args): while self.tab_stack[-1][0] in c_args: self.stack_pop() def handle_token(self, prev_token, token, next_token, y=None): buffer = self.mode.window.buffer name = token.name s = token.string if name == "c comment": if self.tab_stack[-1][0] != "c comment": self.stack_append(("c comment", self.tab_stack[-1][1])) else: self.line_depth += 1 p = point.Point(len(buffer.lines[self.y]), self.y) offset = buffer.get_point_offset(p) if token.end <= offset or next_token is not None: self.stack_pop() elif name == "macro": self.line_depth -= 4 elif name == "operator" and next_token is None: self.stack_append_const_unique("cont") elif name == "label": self.line_depth -= 4 #self.line_depth = 0 elif name == "keyword": if (s == "do" or s == "else" or s == "for" or s == "if" or s == "while"): self.stack_append_const("block") elif s == "case": if prev_token is None: self.line_depth -= 4 elif name == "delimiter": if s == "{" or s == "(" or s == "[": if s == "{": if prev_token is None and self.tab_stack[-1][0] == "block": self.line_depth -= 4 self.stack_pop_const("block") #self.stack_pop_const("block", "cont") else: self.stack_pop_const("cont") if next_token is None: self.stack_append((s, self.tab_stack[-1][1] + 4)) else: p = buffer.get_offset_point(next_token.start) self.stack_append((s, p.x)) elif s == "}" or s == ")" or s == "]": if s == "}": self.stack_pop_all_const("block", "cont") else: self.stack_pop_all_const("cont") if self.tab_stack[-1][0] == self.close_tags[s]: self.stack_pop() if prev_token is None: self.line_depth = self.tab_stack[-1][1] elif self.errors is False: err = "tag mismatch, line %d: expected %r, got %r" % \ (self.y, self.tab_stack[-1][0], s) self.mode.window.application.set_error(err) self.errors = True if s == "}": self.stack_pop_all_const("block", "cont") elif (s == "=" or s == "?") and next_token is None: self.stack_append_const_unique("cont") elif s == ',': self.stack_pop_all_const("cont") elif s == ';': self.stack_pop_all_const("block", "cont")