From 72bdc0915abc552ea13d1758b9d4ab981bbe5ac5 Mon Sep 17 00:00:00 2001 From: Erik Osheim Date: Wed, 19 Aug 2009 23:25:47 -0400 Subject: [PATCH] a bunch of fixes to c-tabber and stacktabber2 --HG-- branch : pmacs2 --- mode/c.py | 19 ++++++++++++------- tab.py | 9 ++++++++- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/mode/c.py b/mode/c.py index f733f2d..adcdde4 100644 --- a/mode/c.py +++ b/mode/c.py @@ -100,29 +100,34 @@ class CTabber2(tab.StackTabber2): end_free_tokens = {'c.string.end': 'c.string.start'} start_macro_tokens = {'c.macro.start': 'c.macro.end'} end_macro_tokens = {'c.macro.end': 'c.macro.start'} - def is_base(self, y): + def _is_base(self, y): if y == 0: return True + + # if there are no tokens we don't really have any info tokens = self._get_tokens(y) + if not tokens: + return False # this assumes that people aren't gonna use these macros inside of # blocks, which is probably ok. t = tokens[0] - if t.fqmatchs('c.macro.start', '#define', '#include'): + if t.fqmatchs('c.macro.start', ('#define', '#include')): return True - + # detecting function declarations is annoying; this assumes that people # won't put a variable type and name on different lines, but that they # might do that for function return type and name. # # unfortunately, valid function return types might include any of the - # four types of tokens below + # types of tokens below decl = False for t in tokens: - if t.isa('c.keyword', 'c.identifier', 'c.type'): + if decl and self._is_ignored(t): + pass + elif t.isa('c.keyword', 'c.identifier', 'c.type'): decl = True - continue - if decl and t.name == 'c.function': + elif decl and t.name == 'c.function': break else: decl = False diff --git a/tab.py b/tab.py index 0d82c17..481afb8 100644 --- a/tab.py +++ b/tab.py @@ -386,6 +386,11 @@ class StackTabber2(Tabber): if self.end_at_eof: return + # ok, if we are closing a block then this will end a "single-statement" + # block. e.g. else switch() { ... } + if t.string in self.close_scope_tokens.get(t.fqname(), set()): + self._pop('control') + # if we do want implicit continuation, see if we need it. name, s = t.fqname(), t.string top = self._peek() @@ -406,7 +411,8 @@ class StackTabber2(Tabber): if i == 0 and self.stack and self.stack[-1].name == 'continue': self.stack.pop() if t.string in self.open_scope_tokens.get(t.name, set()): - self._pop_while('continue', 'control') + #self._pop_while('continue', 'control') + self._pop_while('continue') if i == 0 and t.string in self.open_scope_tokens.get(t.name, set()): self._save_curr_level() @@ -465,4 +471,5 @@ class StackTabber2(Tabber): # handle control keywords if i == start: self._save_curr_level() + self._pop_while('continue'); self._append('control', name, self._get_next_level(), y)