From de7a450d92eabd4d1229611effaef1aef22d4589 Mon Sep 17 00:00:00 2001 From: "Paul R. Tagliamonte" Date: Thu, 28 Mar 2013 21:42:40 -0400 Subject: [PATCH] Adding in a completer. --- bin/hy | 4 +++- hy/completer.py | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 hy/completer.py diff --git a/bin/hy b/bin/hy index f1fbd70..fbb396d 100755 --- a/bin/hy +++ b/bin/hy @@ -22,6 +22,8 @@ from hy.lex.machine import Machine from hy.compiler import hy_compile from hy.core import process +import hy.completer + from hy.macros import macro from hy.models.expression import HyExpression from hy.models.string import HyString @@ -120,7 +122,7 @@ try: except IOError: open(history, 'wa').close() -# readline.parse_and_bind("tab: complete") +readline.parse_and_bind("tab: complete") hr = HyREPL() hr.interact("{appname} {version}".format( diff --git a/hy/completer.py b/hy/completer.py new file mode 100644 index 0000000..f14ec28 --- /dev/null +++ b/hy/completer.py @@ -0,0 +1,41 @@ +import hy.macros +import hy.compiler + +import __builtin__ + + +PATH = [hy.compiler._compile_table, hy.macros._hy_macros, __builtin__.__dict__] + + +class Completer: + def __init__(self, namespace = None): + if namespace and not isinstance(namespace, dict): + raise TypeError,'namespace must be a dictionary' + self.namespace = namespace + + def complete(self, text, state): + path = PATH + if self.namespace: + path.append(self.namespace) + + matches = [] + + for p in path: + p = filter(lambda x: isinstance(x, str), p.keys()) + p = [x.replace("_", "-") for x in p] + [matches.append(x) for x in + filter(lambda x: x.startswith(text), p)] + + try: + return matches[state] + except IndexError: + return None + + +try: + import readline +except ImportError: + pass +else: + readline.set_completer(Completer().complete) + readline.set_completer_delims("()[]{}")