From 7fc0d762d75e6de444a1134ca26264101d0a8668 Mon Sep 17 00:00:00 2001 From: moculus Date: Fri, 30 May 2008 18:13:26 +0000 Subject: [PATCH] --HG-- branch : pmacs2 --- application.py | 8 ++++++-- completer.py | 14 ++++++++++++++ method/__init__.py | 5 ++--- method/introspect.py | 42 +++++++++++++++++++++++++++++++++++++--- mode/perl.py | 46 +++++++++++++++++++++++--------------------- 5 files changed, 85 insertions(+), 30 deletions(-) diff --git a/application.py b/application.py index 53095bc..591a2d1 100755 --- a/application.py +++ b/application.py @@ -194,6 +194,7 @@ class Application(object): method.DATATYPES['method'] = completer.MethodCompleter() method.DATATYPES['register'] = completer.RegisterCompleter() method.DATATYPES['mode'] = completer.ModeCompleter() + method.DATATYPES['token'] = completer.TokenCompleter() # set up curses self.win = curses.newwin(self.y, self.x, 0, 0) @@ -245,7 +246,7 @@ class Application(object): # NOTE: this is not an optimal packing, but it's fast and easy to # understand. i encourage someone else to write something better. - numcols = self.bufferlist.slots[n].width // (maxlen + 2) + numcols = max(self.bufferlist.slots[n].width // (maxlen + 2), 1) numrows = clen - ((clen // numcols) * (numcols - 1)) for i in range(0, numrows): names = [] @@ -387,7 +388,8 @@ class Application(object): return self.mini_buffer def mini_buffer_is_open(self): return self.mini_buffer is not None - def open_mini_buffer(self, prompt, callback, method=None, tabber=None, modename=None): + def open_mini_buffer(self, prompt, callback, method=None, tabber=None, + modename=None, startvalue=None): if self.mini_buffer_is_open(): self.close_mini_buffer() self.mini_prompt = prompt @@ -395,6 +397,8 @@ class Application(object): try: w = self.x - 1 - len(self.mini_prompt) - 1 window.Window(self.mini_buffer, self, height=1, width=w) + if startvalue: + self.mini_buffer.set_data(startvalue) self.mini_active = True except minibuffer.MiniBufferError: self.mini_buffer = None diff --git a/completer.py b/completer.py index 8d5f7bd..8ebf6d7 100644 --- a/completer.py +++ b/completer.py @@ -90,6 +90,20 @@ class ShellCompleter(Completer): else: return self.command_completer.get_candidates(s) +class TokenCompleter(Completer): + def get_candidates(self, s, w=None): + w2 = w.buffer.method.old_window + t = w2.get_token2() + h = w2.get_highlighter() + candidates = {} + for line in h.tokens: + for t2 in line: + if t2 is t: + continue + elif t2.string.startswith(s): + candidates[t2.string] = 1 + return candidates.keys() + class MethodCompleter(Completer): def get_candidates(self, s, w=None): return [n for n in w.application.methods if n.startswith(s)] diff --git a/method/__init__.py b/method/__init__.py index 896e895..38d6334 100644 --- a/method/__init__.py +++ b/method/__init__.py @@ -68,9 +68,8 @@ class Argument(object): p = self.prompt + "(%s) " % (d) else: p = self.prompt - app.open_mini_buffer(p, return_value, method, tabber) - if starting_value: - app.mini_buffer.set_data(starting_value) + app.open_mini_buffer(p, return_value, method=method, tabber=tabber, + startvalue=starting_value) class Method(object): _is_method = True diff --git a/method/introspect.py b/method/introspect.py index 92e44ca..80346ba 100644 --- a/method/introspect.py +++ b/method/introspect.py @@ -1,7 +1,7 @@ import os, commands, re, sets, tempfile from subprocess import Popen, PIPE, STDOUT -import buffer, default, dirutil, regex, util, window +import buffer, completer, default, dirutil, regex, util, window import mode.mini from point import Point @@ -82,7 +82,7 @@ class GetToken(Method): else: w.set_error('Token: %r (%s)' % (token.string, token.fqname())) -class TokenComplete(Method): +class TokenComplete2(Method): '''Complete token names based on other tokens in the buffer''' def _prune_candidates(self, t, minlen, candidates): if not candidates: @@ -131,7 +131,6 @@ class TokenComplete(Method): w.buffer.delete(p1, p2) w.insert_string(p1, result) - #mode.mini.use_completion_window(w.application, result, candidates) if not candidates: w.set_error("No completion: %r" % result) elif len(candidates) == 1: @@ -141,6 +140,43 @@ class TokenComplete(Method): else: w.set_error("Partial completion: %r" % candidates) + +class TokenComplete(Method): + '''Complete token names based on other tokens in the buffer''' + def _execute(self, w, **vargs): + self.old_window = w + tabber = completer.TokenCompleter() + t = w.get_token2() + if t is None: + w.set_error("No token to complete!") + return + elif regex.reserved_token_names.match(t.name): + w.set_error("Will not complete reserved token") + return + + class Dummy(object): pass + dw = Dummy() + dw.buffer = Dummy() + dw.buffer.method = self + (s2, exists, complete) = tabber.tab_string(t.string, dw) + + p1 = Point(t.x, t.y) + p2 = Point(t.end_x(), t.y) + def callback(s): + w.buffer.delete(p1, p2) + w.insert_string(p1, s) + w.application.close_mini_buffer() + + if exists and complete: + w.set_error("Unique completion: %r" % s2) + callback(s2) + return + else: + if exists: + pass + w.application.open_mini_buffer("Foog: ", callback, method=self, + tabber=tabber, startvalue=s2) + class OpenConsole(Method): '''Evaluate python expressions (for advanced use and debugging only)''' def execute(self, w, **vargs): diff --git a/mode/perl.py b/mode/perl.py index 409ceac..b95615a 100644 --- a/mode/perl.py +++ b/mode/perl.py @@ -117,10 +117,11 @@ class PerlGrammar(Grammar): RegionRule(r'quoted', r'qw? *(?P[^ #])', Grammar, r'%(delim)s'), PatternRule(r'perl_function', r"(?:[a-zA-Z_][a-zA-Z_0-9]*::)*[a-zA-Z_][a-zA-Z_0-9]*(?= *\()"), + PatternRule(r'perl_namespace', r"(?:[a-zA-Z_][a-zA-Z_0-9]*\:\:)+(?:[a-zA-Z_][a-zA-Z_0-9]*)?"), PatternRule(r'perl_class', r"(?:[a-zA-Z_][a-zA-Z_0-9]*::)*[a-zA-Z_][a-zA-Z_0-9]*(?=->)"), # some basic stuff - PatternRule(r'delimiter', r"->|=>|(?|=>|(?>=|<<=|\*\*="), PatternRule(r'operator', r"\+\+|\+|<=>|<>|<<|<=|<|-|>>|>=|>|\*\*|\*|&&|&|\|\||\||/|\^|==|//|~|=~|!~|!=|%|!|\.|x(?![a-zA-Z_])"), PatternRule(r'noperator', r"(?:xor|or|not|ne|lt|le|gt|ge|eq|cmp|and)(?![a-zA-Z_])"), @@ -610,27 +611,28 @@ class Perl(mode.Fundamental): 'pod.end': ('red', 'default', 'bold'), # basic stuff - 'escaped': ('magenta', 'default', 'bold'), - 'null': ('default', 'default', 'bold'), - 'sub': ('cyan', 'default', 'bold'), - 'prototype': ('magenta', 'default', 'bold'), - 'operator': ('default', 'default', 'bold'), - 'noperator': ('magenta', 'default', 'bold'), - 'endblock': ('red', 'default', 'bold'), - 'perl_keyword': ('magenta', 'default', 'bold'), - 'cast': ('yellow', 'default', 'bold'), - 'scalar': ('yellow', 'default', 'bold'), - 'array': ('yellow', 'default', 'bold'), - 'deref': ('yellow', 'default', 'bold'), - 'perl_hash': ('yellow', 'default', 'bold'), - 'hash_key': ('green', 'default', 'bold'), - 'perl_function': ('cyan', 'default', 'bold'), - 'perl_builtin': ('magenta', 'default', 'bold'), - 'perl_label': ('cyan', 'default', 'bold'), - 'package': ('cyan', 'default', 'bold'), - 'perl_class': ('cyan', 'default', 'bold'), - 'use': ('cyan', 'default', 'bold'), - 'require': ('cyan', 'default', 'bold'), + 'escaped': ('magenta', 'default', 'bold'), + 'null': ('default', 'default', 'bold'), + 'sub': ('cyan', 'default', 'bold'), + 'prototype': ('magenta', 'default', 'bold'), + 'operator': ('default', 'default', 'bold'), + 'noperator': ('magenta', 'default', 'bold'), + 'endblock': ('red', 'default', 'bold'), + 'perl_keyword': ('magenta', 'default', 'bold'), + 'cast': ('yellow', 'default', 'bold'), + 'scalar': ('yellow', 'default', 'bold'), + 'array': ('yellow', 'default', 'bold'), + 'deref': ('yellow', 'default', 'bold'), + 'perl_hash': ('yellow', 'default', 'bold'), + 'hash_key': ('green', 'default', 'bold'), + 'perl_function': ('cyan', 'default', 'bold'), + 'perl_namespace': ('cyan', 'default', 'bold'), + 'perl_builtin': ('magenta', 'default', 'bold'), + 'perl_label': ('cyan', 'default', 'bold'), + 'package': ('cyan', 'default', 'bold'), + 'perl_class': ('cyan', 'default', 'bold'), + 'use': ('cyan', 'default', 'bold'), + 'require': ('cyan', 'default', 'bold'), # heredoc/evaldoc 'heredoc.start': ('green', 'default', 'bold'),