From 155044696fb1349abfd92df39ec32d4c8f8c5471 Mon Sep 17 00:00:00 2001 From: moculus Date: Tue, 18 Nov 2008 03:31:05 +0000 Subject: [PATCH] tab fixes --HG-- branch : pmacs2 --- tab.py | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/tab.py b/tab.py index 05c9878..f794884 100644 --- a/tab.py +++ b/tab.py @@ -240,7 +240,8 @@ class Marker2(object): class StackTabber2(Tabber): open_tokens = {'delimiter': {'{': '}', '(': ')', '[': ']'}} close_tokens = {'delimiter': {'}': '{', ')': '(', ']': '['}} - scope_tokens = {'delimiter': set(['{'])} + open_scope_tokens = {'delimiter': set(['{'])} + close_scope_tokens = {'delimiter': set(['}'])} control_tokens = {'keyword': set(['if', 'else', 'while', 'do', 'for'])} end_at_eof = True end_at_tokens = {} @@ -376,14 +377,17 @@ class StackTabber2(Tabber): # add implicit continuation XYZYXYZY name, s = t.fqname(), t.string - top = self._peek() - if (i + start == end and - (not top or top.name in self.scope_tokens.get(top.type_, {}))): + top = self._peek() + atscope = True + if top: + d = self.open_scope_tokens.get(top.type_, set()) + atscope = top.name in d + if (atscope and i + start == end): d = self.nocontinue_tokens.get(name) if d is None or d != 1 and s not in d: - if s == '}': - raise Exception, "foog: %r %r %r" % (s not in d, s, d) - self._append_unless('continue', name, self._get_next_level()) + if s not in self.close_scope_tokens.get(name, set()): + nextlvl = self._get_next_level() + self._append_unless('continue', name, nextlvl) #XYZYZYXYXY def _is_open_token(self, t): @@ -392,12 +396,15 @@ class StackTabber2(Tabber): def _handle_open_token(self, y, tokens, start, end, i, t): if i == 0 and self.stack and self.stack[-1].name == 'continue': self.stack.pop() - if t.string in self.scope_tokens.get(t.name, {}): + if t.string in self.open_scope_tokens.get(t.name, set()): self._pop_while('continue', 'control') + if i == 0 and t.string in self.open_scope_tokens.get(t.name, set()): + self._save_curr_level() + if i == end - start: level = self._get_next_level() else: - level = tokens[i + 1].x + 1 + level = tokens[i + 1].x + 1 self._append(t.string, t.name, level) def _handle_other_token(self, y, tokens, start, end, i, t): @@ -427,7 +434,9 @@ class StackTabber2(Tabber): # add implicit continuation top = self._peek() - if i + start == end and (top and top.name in self.scope_tokens.get(top.type_, {}) or not top): + if (i + start == end and + (top and top.name in self.open_scope_tokens.get(top.type_, {}) or + not top)): if self.continue_tokens: if s in self.continue_tokens.get(name, {}): self._append_unless('continue', name, self._get_next_level())