token completion support. huzzah.

--HG--
branch : pmacs2
This commit is contained in:
moculus 2007-10-21 21:21:47 +00:00
parent 019f2645d6
commit ea3426f235
3 changed files with 28 additions and 11 deletions

View File

@ -1538,7 +1538,7 @@ class TokenComplete(Method):
for t2 in line: for t2 in line:
if t2 is t: if t2 is t:
continue continue
elif t2.name not in ok: elif False and t2.name not in ok:
continue continue
elif t2.string.startswith(t.string): elif t2.string.startswith(t.string):
strings[t2.string] = 1 strings[t2.string] = 1
@ -1549,25 +1549,40 @@ class TokenComplete(Method):
strings = strings.keys() strings = strings.keys()
if not strings: if not strings:
return (0, t.string) return ([], t.string)
i = len(t.string) i = len(t.string)
while i < minlen: while i < minlen:
c = strings[0][i] c = strings[0][i]
for s in strings: for s in strings:
if s[i] != c: if s[i] != c:
return (len(strings), strings[0][:i]) return (strings, strings[0][:i])
i += 1 i += 1
return (len(strings), strings[0][:minlen]) return (strings, strings[0][:minlen])
def _execute(self, w, **vargs): def _execute(self, w, **vargs):
t = w.get_token2() 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 return
(n, s) = self._min_completion(w, t) (candidates, result) = self._min_completion(w, t)
if not n:
return
# XYZ if candidates:
w.set_error("%r (%r) completing to %r (%d)" % (t.string, t.fqname(), s, n)) 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)

View File

@ -179,6 +179,7 @@ class Fundamental(Handler):
self.add_bindings('grep', ('C-c g',)) self.add_bindings('grep', ('C-c g',))
self.add_bindings('pipe', ('C-c p',)) self.add_bindings('pipe', ('C-c p',))
self.add_bindings('view-buffer-parent', ('C-c .',)) self.add_bindings('view-buffer-parent', ('C-c .',))
self.add_bindings('token-complete', ('C-c c',))
# unbound actions # unbound actions
self.add_action(method.GetToken()) self.add_action(method.GetToken())

View File

@ -599,7 +599,8 @@ class Window(object):
return self.get_token_at_point(self.logical_cursor()) return self.get_token_at_point(self.logical_cursor())
def get_token2(self): def get_token2(self):
c = self.logical_cursor() 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): def get_token_at_point(self, p):
for token in self.get_highlighter().tokens[p.y]: for token in self.get_highlighter().tokens[p.y]:
if token.end_x() <= p.x: if token.end_x() <= p.x: