Adding in a completer.
This commit is contained in:
parent
4c50424a66
commit
de7a450d92
4
bin/hy
4
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(
|
||||
|
41
hy/completer.py
Normal file
41
hy/completer.py
Normal 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("()[]{}")
|
Loading…
Reference in New Issue
Block a user