Move code from hy.readline_helpers into new hy.completer.completion context
manager so we can "wrap" readline completion around REPL interaction.
This commit is contained in:
parent
5554c50671
commit
748f54b2f9
@ -36,9 +36,7 @@ from hy.lex.states import Idle, LexException
|
|||||||
from hy.lex.machine import Machine
|
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
|
||||||
|
from hy.completer import completion
|
||||||
from hy.readline_helpers import read_history_file, write_history_file
|
|
||||||
import hy.completer
|
|
||||||
|
|
||||||
from hy.models.expression import HyExpression
|
from hy.models.expression import HyExpression
|
||||||
from hy.models.string import HyString
|
from hy.models.string import HyString
|
||||||
@ -183,16 +181,14 @@ def run_repl(hr=None):
|
|||||||
sys.ps1 = "=> "
|
sys.ps1 = "=> "
|
||||||
sys.ps2 = "... "
|
sys.ps2 = "... "
|
||||||
|
|
||||||
history = read_history_file()
|
with completion():
|
||||||
|
if not hr:
|
||||||
|
hr = HyREPL()
|
||||||
|
|
||||||
if not hr:
|
hr.interact("{appname} {version}".format(
|
||||||
hr = HyREPL()
|
appname=hy.__appname__,
|
||||||
hr.interact("{appname} {version}".format(
|
version=hy.__version__
|
||||||
appname=hy.__appname__,
|
))
|
||||||
version=hy.__version__
|
|
||||||
))
|
|
||||||
|
|
||||||
write_history_file(history)
|
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
@ -1,4 +1,11 @@
|
|||||||
# Copyright (c) 2013 Paul Tagliamonte <paultag@debian.org>
|
# Copyright (c) 2013 Paul Tagliamonte <paultag@debian.org>
|
||||||
|
# Copyright (c) 2013 Gergely Nagy <algernon@madhouse-project.org>
|
||||||
|
# Copyright (c) 2013 James King <james@agentultra.com>
|
||||||
|
# Copyright (c) 2013 Julien Danjou <julien@danjou.info>
|
||||||
|
# Copyright (c) 2013 Konrad Hinsen <konrad.hinsen@fastmail.net>
|
||||||
|
# Copyright (c) 2013 Thom Neale <twneale@gmail.com>
|
||||||
|
# Copyright (c) 2013 Will Kahn-Greene <willg@bluesock.org>
|
||||||
|
# Copyright (c) 2013 Ralph Moritz <ralph.moeritz@outlook.com>
|
||||||
#
|
#
|
||||||
# Permission is hereby granted, free of charge, to any person obtaining a
|
# Permission is hereby granted, free of charge, to any person obtaining a
|
||||||
# copy of this software and associated documentation files (the "Software"),
|
# copy of this software and associated documentation files (the "Software"),
|
||||||
@ -18,9 +25,22 @@
|
|||||||
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||||
# DEALINGS IN THE SOFTWARE.
|
# DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
|
import os
|
||||||
|
|
||||||
|
docomplete = True
|
||||||
|
|
||||||
|
try:
|
||||||
|
import readline
|
||||||
|
except ImportError:
|
||||||
|
try:
|
||||||
|
import pyreadline.rlmain
|
||||||
|
import pyreadline.unicode_helper # NOQA
|
||||||
|
import readline
|
||||||
|
except ImportError:
|
||||||
|
docomplete = False
|
||||||
|
|
||||||
import hy.macros
|
import hy.macros
|
||||||
import hy.compiler
|
import hy.compiler
|
||||||
from hy.readline_helpers import set_completer
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import __builtin__
|
import __builtin__
|
||||||
@ -58,4 +78,33 @@ class Completer(object):
|
|||||||
except IndexError:
|
except IndexError:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
set_completer(Completer().complete, "()[]{} ")
|
|
||||||
|
class completion(object):
|
||||||
|
delims = "()[]{} "
|
||||||
|
|
||||||
|
def __init__(self, completer=None):
|
||||||
|
if not completer:
|
||||||
|
completer = Completer()
|
||||||
|
|
||||||
|
self.completer = completer
|
||||||
|
|
||||||
|
def __enter__(self):
|
||||||
|
if not docomplete:
|
||||||
|
return
|
||||||
|
|
||||||
|
readline.set_completer(self.completer.complete)
|
||||||
|
readline.set_completer_delims(self.delims)
|
||||||
|
|
||||||
|
self.history = os.path.expanduser("~/.hy-history")
|
||||||
|
readline.parse_and_bind("set blink-matching-paren on")
|
||||||
|
|
||||||
|
try:
|
||||||
|
readline.read_history_file(self.history)
|
||||||
|
except IOError:
|
||||||
|
open(self.history, 'a').close()
|
||||||
|
|
||||||
|
readline.parse_and_bind("tab: complete")
|
||||||
|
|
||||||
|
def __exit__(self, type, value, tb):
|
||||||
|
if docomplete:
|
||||||
|
readline.write_history_file(self.history)
|
||||||
|
@ -1,68 +0,0 @@
|
|||||||
# -*- coding: utf-8 -*-
|
|
||||||
# Copyright (c) 2013 Paul Tagliamonte <paultag@debian.org>
|
|
||||||
# Copyright (c) 2013 Gergely Nagy <algernon@madhouse-project.org>
|
|
||||||
# Copyright (c) 2013 James King <james@agentultra.com>
|
|
||||||
# Copyright (c) 2013 Julien Danjou <julien@danjou.info>
|
|
||||||
# Copyright (c) 2013 Konrad Hinsen <konrad.hinsen@fastmail.net>
|
|
||||||
# Copyright (c) 2013 Thom Neale <twneale@gmail.com>
|
|
||||||
# Copyright (c) 2013 Will Kahn-Greene <willg@bluesock.org>
|
|
||||||
# Copyright (c) 2013 Ralph Möritz <ralph.moeritz@outlook.com>
|
|
||||||
#
|
|
||||||
# Permission is hereby granted, free of charge, to any person obtaining a
|
|
||||||
# copy of this software and associated documentation files (the "Software"),
|
|
||||||
# to deal in the Software without restriction, including without limitation
|
|
||||||
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
||||||
# and/or sell copies of the Software, and to permit persons to whom the
|
|
||||||
# Software is furnished to do so, subject to the following conditions:
|
|
||||||
#
|
|
||||||
# The above copyright notice and this permission notice shall be included in
|
|
||||||
# all copies or substantial portions of the Software.
|
|
||||||
#
|
|
||||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
||||||
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
||||||
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
||||||
# DEALINGS IN THE SOFTWARE.
|
|
||||||
|
|
||||||
import os
|
|
||||||
|
|
||||||
noop = False
|
|
||||||
|
|
||||||
try:
|
|
||||||
import readline
|
|
||||||
except ImportError:
|
|
||||||
try:
|
|
||||||
import pyreadline.rlmain
|
|
||||||
import pyreadline.unicode_helper # NOQA
|
|
||||||
import readline
|
|
||||||
except ImportError:
|
|
||||||
noop = True
|
|
||||||
|
|
||||||
|
|
||||||
def set_completer(completer, delims):
|
|
||||||
if not noop:
|
|
||||||
readline.set_completer(completer)
|
|
||||||
readline.set_completer_delims(delims)
|
|
||||||
|
|
||||||
|
|
||||||
def read_history_file():
|
|
||||||
if noop:
|
|
||||||
return None
|
|
||||||
|
|
||||||
history = os.path.expanduser("~/.hy-history")
|
|
||||||
readline.parse_and_bind("set blink-matching-paren on")
|
|
||||||
|
|
||||||
try:
|
|
||||||
readline.read_history_file(history)
|
|
||||||
except IOError:
|
|
||||||
open(history, 'a').close()
|
|
||||||
|
|
||||||
readline.parse_and_bind("tab: complete")
|
|
||||||
return history
|
|
||||||
|
|
||||||
|
|
||||||
def write_history_file(history):
|
|
||||||
if not noop:
|
|
||||||
readline.write_history_file(history)
|
|
Loading…
Reference in New Issue
Block a user