From ea3426f235cf09c862e4cce4ad813da962280bd3 Mon Sep 17 00:00:00 2001 From: moculus Date: Sun, 21 Oct 2007 21:21:47 +0000 Subject: [PATCH] token completion support. huzzah. --HG-- branch : pmacs2 --- method.py | 35 +++++++++++++++++++++++++---------- mode2.py | 1 + window2.py | 3 ++- 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/method.py b/method.py index b1e99bc..e494934 100644 --- a/method.py +++ b/method.py @@ -1538,7 +1538,7 @@ class TokenComplete(Method): for t2 in line: if t2 is t: continue - elif t2.name not in ok: + elif False and t2.name not in ok: continue elif t2.string.startswith(t.string): strings[t2.string] = 1 @@ -1549,25 +1549,40 @@ class TokenComplete(Method): strings = strings.keys() if not strings: - return (0, t.string) + return ([], t.string) i = len(t.string) while i < minlen: c = strings[0][i] for s in strings: if s[i] != c: - return (len(strings), strings[0][:i]) + return (strings, strings[0][:i]) i += 1 - return (len(strings), strings[0][:minlen]) + return (strings, strings[0][:minlen]) def _execute(self, w, **vargs): t = w.get_token2() - if regex.reserved_token_names.match(t.name): + + 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 - (n, s) = self._min_completion(w, t) - if not n: - return + (candidates, result) = self._min_completion(w, t) - # XYZ - w.set_error("%r (%r) completing to %r (%d)" % (t.string, t.fqname(), s, n)) + if candidates: + p1 = Point(t.x, t.y) + p2 = Point(t.end_x(), t.y) + w.buffer.delete(p1, p2) + w.insert_string(p1, result) + + if not candidates: + w.set_error("No completion: %r" % result) + elif len(candidates) == 1: + w.set_error("Unique completion: %r" % result) + elif result in candidates: + w.set_error("Ambiguous completion: %r" % candidates) + else: + w.set_error("Partial completion: %r" % candidates) diff --git a/mode2.py b/mode2.py index 0531232..09e0f18 100644 --- a/mode2.py +++ b/mode2.py @@ -179,6 +179,7 @@ class Fundamental(Handler): self.add_bindings('grep', ('C-c g',)) self.add_bindings('pipe', ('C-c p',)) self.add_bindings('view-buffer-parent', ('C-c .',)) + self.add_bindings('token-complete', ('C-c c',)) # unbound actions self.add_action(method.GetToken()) diff --git a/window2.py b/window2.py index 8987f17..48dcf43 100644 --- a/window2.py +++ b/window2.py @@ -599,7 +599,8 @@ class Window(object): return self.get_token_at_point(self.logical_cursor()) def get_token2(self): c = self.logical_cursor() - return self.get_token_at_point(Point(min(0, c.x - 1), c.y)) + p = Point(max(0, c.x - 1), c.y) + return self.get_token_at_point(p) def get_token_at_point(self, p): for token in self.get_highlighter().tokens[p.y]: if token.end_x() <= p.x: