Adding in a completer.

This commit is contained in:
Paul R. Tagliamonte 2013-03-28 21:42:40 -04:00
parent 4c50424a66
commit de7a450d92
2 changed files with 44 additions and 1 deletions

4
bin/hy
View File

@ -22,6 +22,8 @@ from hy.lex.machine import Machine
from hy.compiler import hy_compile from hy.compiler import hy_compile
from hy.core import process from hy.core import process
import hy.completer
from hy.macros import macro from hy.macros import macro
from hy.models.expression import HyExpression from hy.models.expression import HyExpression
from hy.models.string import HyString from hy.models.string import HyString
@ -120,7 +122,7 @@ try:
except IOError: except IOError:
open(history, 'wa').close() open(history, 'wa').close()
# readline.parse_and_bind("tab: complete") readline.parse_and_bind("tab: complete")
hr = HyREPL() hr = HyREPL()
hr.interact("{appname} {version}".format( hr.interact("{appname} {version}".format(

41
hy/completer.py Normal file
View File

@ -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("()[]{}")