2020-01-03 19:47:51 +01:00
|
|
|
# Copyright 2020 the authors.
|
2017-04-27 23:16:57 +02:00
|
|
|
# This file is part of Hy, which is free software licensed under the Expat
|
|
|
|
# license. See the LICENSE.
|
2013-03-29 03:42:53 +01:00
|
|
|
|
2015-02-17 03:27:28 +01:00
|
|
|
import contextlib
|
2013-07-06 17:43:51 +02:00
|
|
|
import os
|
2015-01-14 20:39:28 +01:00
|
|
|
import re
|
2013-12-03 03:58:59 +01:00
|
|
|
import sys
|
2019-05-20 22:12:00 +02:00
|
|
|
import builtins
|
2015-02-17 03:27:28 +01:00
|
|
|
|
|
|
|
import hy.macros
|
|
|
|
import hy.compiler
|
|
|
|
|
2013-07-06 17:43:51 +02:00
|
|
|
|
|
|
|
docomplete = True
|
|
|
|
|
|
|
|
try:
|
|
|
|
import readline
|
|
|
|
except ImportError:
|
|
|
|
try:
|
|
|
|
import pyreadline.rlmain
|
|
|
|
import pyreadline.unicode_helper # NOQA
|
|
|
|
import readline
|
|
|
|
except ImportError:
|
|
|
|
docomplete = False
|
|
|
|
|
2017-08-07 19:25:58 +02:00
|
|
|
if docomplete:
|
|
|
|
if sys.platform == 'darwin' and 'libedit' in readline.__doc__:
|
|
|
|
readline_bind = "bind ^I rl_complete"
|
|
|
|
else:
|
|
|
|
readline_bind = "tab: complete"
|
2013-12-03 03:58:59 +01:00
|
|
|
|
|
|
|
|
2013-04-02 20:38:21 +02:00
|
|
|
class Completer(object):
|
2013-03-29 03:33:32 +01:00
|
|
|
|
2015-01-14 23:45:23 +01:00
|
|
|
def __init__(self, namespace={}):
|
|
|
|
if not isinstance(namespace, dict):
|
|
|
|
raise TypeError('namespace must be a dictionary')
|
2013-03-29 02:42:40 +01:00
|
|
|
self.namespace = namespace
|
2018-04-22 21:48:37 +02:00
|
|
|
self.path = [hy.compiler._special_form_compilers,
|
2015-01-14 23:45:23 +01:00
|
|
|
builtins.__dict__,
|
|
|
|
namespace]
|
2018-10-23 20:41:20 +02:00
|
|
|
|
|
|
|
self.tag_path = []
|
|
|
|
|
|
|
|
namespace.setdefault('__macros__', {})
|
|
|
|
namespace.setdefault('__tags__', {})
|
|
|
|
|
|
|
|
self.path.append(namespace['__macros__'])
|
|
|
|
self.tag_path.append(namespace['__tags__'])
|
2013-03-29 02:42:40 +01:00
|
|
|
|
2015-01-14 20:39:28 +01:00
|
|
|
def attr_matches(self, text):
|
|
|
|
# Borrowed from IPython's completer
|
2015-01-14 23:45:23 +01:00
|
|
|
m = re.match(r"(\S+(\.[\w-]+)*)\.([\w-]*)$", text)
|
2015-01-14 20:39:28 +01:00
|
|
|
|
|
|
|
if m:
|
|
|
|
expr, attr = m.group(1, 3)
|
|
|
|
attr = attr.replace("-", "_")
|
2015-01-14 23:45:23 +01:00
|
|
|
expr = expr.replace("-", "_")
|
2015-01-14 20:39:28 +01:00
|
|
|
else:
|
|
|
|
return []
|
|
|
|
|
|
|
|
try:
|
|
|
|
obj = eval(expr, self.namespace)
|
|
|
|
words = dir(obj)
|
|
|
|
except Exception:
|
|
|
|
return []
|
|
|
|
|
|
|
|
n = len(attr)
|
|
|
|
matches = []
|
|
|
|
for w in words:
|
|
|
|
if w[:n] == attr:
|
2015-01-14 23:45:23 +01:00
|
|
|
matches.append("{}.{}".format(
|
|
|
|
expr.replace("_", "-"), w.replace("_", "-")))
|
2015-01-14 20:39:28 +01:00
|
|
|
return matches
|
|
|
|
|
|
|
|
def global_matches(self, text):
|
2015-01-14 23:45:23 +01:00
|
|
|
matches = []
|
|
|
|
for p in self.path:
|
|
|
|
for k in p.keys():
|
2019-05-20 21:18:12 +02:00
|
|
|
if isinstance(k, str):
|
2015-01-14 23:45:23 +01:00
|
|
|
k = k.replace("_", "-")
|
|
|
|
if k.startswith(text):
|
|
|
|
matches.append(k)
|
|
|
|
return matches
|
2013-03-29 02:42:40 +01:00
|
|
|
|
2017-06-21 05:48:54 +02:00
|
|
|
def tag_matches(self, text):
|
2015-01-14 23:45:23 +01:00
|
|
|
text = text[1:]
|
2013-03-29 02:42:40 +01:00
|
|
|
matches = []
|
2017-06-21 05:48:54 +02:00
|
|
|
for p in self.tag_path:
|
2015-01-14 23:45:23 +01:00
|
|
|
for k in p.keys():
|
2019-05-20 21:18:12 +02:00
|
|
|
if isinstance(k, str):
|
2015-01-14 23:45:23 +01:00
|
|
|
if k.startswith(text):
|
|
|
|
matches.append("#{}".format(k))
|
2015-01-14 20:39:28 +01:00
|
|
|
return matches
|
2013-03-29 02:42:40 +01:00
|
|
|
|
2015-01-14 20:39:28 +01:00
|
|
|
def complete(self, text, state):
|
2015-01-14 23:45:23 +01:00
|
|
|
if text.startswith("#"):
|
2017-06-21 05:48:54 +02:00
|
|
|
matches = self.tag_matches(text)
|
2015-01-14 23:45:23 +01:00
|
|
|
elif "." in text:
|
2015-01-14 20:39:28 +01:00
|
|
|
matches = self.attr_matches(text)
|
|
|
|
else:
|
|
|
|
matches = self.global_matches(text)
|
2013-03-29 02:42:40 +01:00
|
|
|
try:
|
|
|
|
return matches[state]
|
|
|
|
except IndexError:
|
|
|
|
return None
|
|
|
|
|
2013-07-06 17:43:51 +02:00
|
|
|
|
2015-02-17 03:27:28 +01:00
|
|
|
@contextlib.contextmanager
|
2013-07-06 19:43:05 +02:00
|
|
|
def completion(completer=None):
|
2013-07-06 17:43:51 +02:00
|
|
|
delims = "()[]{} "
|
2013-07-06 19:43:05 +02:00
|
|
|
if not completer:
|
|
|
|
completer = Completer()
|
2013-07-06 17:43:51 +02:00
|
|
|
|
2013-07-06 19:43:05 +02:00
|
|
|
if docomplete:
|
|
|
|
readline.set_completer(completer.complete)
|
|
|
|
readline.set_completer_delims(delims)
|
2013-07-06 17:43:51 +02:00
|
|
|
|
2013-07-06 19:43:05 +02:00
|
|
|
history = os.path.expanduser("~/.hy-history")
|
2013-07-06 17:43:51 +02:00
|
|
|
readline.parse_and_bind("set blink-matching-paren on")
|
2013-07-06 19:43:05 +02:00
|
|
|
|
2013-07-06 17:43:51 +02:00
|
|
|
try:
|
2013-07-06 19:43:05 +02:00
|
|
|
readline.read_history_file(history)
|
2013-07-06 17:43:51 +02:00
|
|
|
except IOError:
|
2019-08-17 01:03:34 +02:00
|
|
|
pass
|
2013-07-06 17:43:51 +02:00
|
|
|
|
2013-12-03 03:58:59 +01:00
|
|
|
readline.parse_and_bind(readline_bind)
|
2013-07-06 17:43:51 +02:00
|
|
|
|
2017-07-26 23:22:50 +02:00
|
|
|
try:
|
|
|
|
yield
|
|
|
|
finally:
|
|
|
|
if docomplete:
|
2019-08-17 01:03:34 +02:00
|
|
|
try:
|
|
|
|
readline.write_history_file(history)
|
|
|
|
except IOError:
|
|
|
|
pass
|