From 909f017b3f5f616371b0521e570597f920e413f2 Mon Sep 17 00:00:00 2001 From: moculus Date: Fri, 21 Mar 2008 06:29:03 +0000 Subject: [PATCH] improved python function support --HG-- branch : pmacs2 --- buffer.py | 8 ++++---- mode/python.py | 22 ++++++++++++++++++++-- regex.py | 3 ++- 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/buffer.py b/buffer.py index 6db7b52..28e930f 100644 --- a/buffer.py +++ b/buffer.py @@ -615,9 +615,7 @@ class PathListBuffer(DirBuffer): def name(self): return self._name -class AboutBuffer(DataBuffer): - btype = 'about' - data = ''' +ABOUT_DATA = ''' ================================================================================ ************ ********** ****** ****** **** ******** ********* @@ -636,6 +634,8 @@ class AboutBuffer(DataBuffer): ================================================================================ ''' +class AboutBuffer(DataBuffer): + btype = 'about' modename = 'about' def __init__(self): - DataBuffer.__init__(self, '*About*', self.data) + DataBuffer.__init__(self, '*About*', ABOUT_DATA) diff --git a/mode/python.py b/mode/python.py index 4beba27..494b3c4 100644 --- a/mode/python.py +++ b/mode/python.py @@ -219,11 +219,29 @@ class Python(mode.Fundamental): self.functions = None def build_function_map(self): b = self.window.buffer + scope_stack = [] self.functions = {} for i in range(0, len(b.lines)): - m = regex.python_function.match(b.lines[i]) + if regex.whitespace.match(b.lines[i]): + continue + m = regex.python_indent.match(b.lines[i]) + assert m + lvl = len(m.group(1)) + while scope_stack: + if lvl <= scope_stack[-1][0]: + scope_stack.pop(-1) + else: + break + m = regex.python_scope.match(b.lines[i]) if m: - self.functions[m.group(1)] = i + (ws, typ, name) = m.groups() + lvl = len(ws) + if scope_stack: + prefix = '.'.join([x[1] for x in scope_stack]) + self.functions['%s.%s' % (prefix, name)] = i + else: + self.functions[name] = i + scope_stack.append((len(ws), name)) def get_functions(self): if self.functions is None: self.build_function_map() diff --git a/regex.py b/regex.py index 38ef078..59d89c3 100644 --- a/regex.py +++ b/regex.py @@ -32,4 +32,5 @@ perl_function = re.compile(r"^ *sub ([A-Za-z_][A-Za-z0-9_]*)") python_base = re.compile(r"^[^ ]") python_dict_cleanup = re.compile(r"^( *)((?:[^'\":]|'(?:\.|[^\'])*'|\"(?:\.|[^\'])*)+?)( *)(:)( *)([^ ].*)$") python_assign_cleanup = re.compile(r"^( *)([^ ]+)( *)(=)( *)([^ ].*)$") -python_function = re.compile('^ *def ([A-Za-z_][A-Za-z0-9_]*)') \ No newline at end of file +python_scope = re.compile('^( *)(class|def) ([A-Za-z_][A-Za-z0-9_]*)') +python_indent = re.compile('^( *)')