forked from VimPlug/jedi
fix a problem with setup_readline, using __dict__ instead of a simple dir(), #280
This commit is contained in:
@@ -10,7 +10,7 @@ from jedi._compatibility import builtins
|
|||||||
from jedi import Interpreter
|
from jedi import Interpreter
|
||||||
|
|
||||||
|
|
||||||
def setup_readline(namespace=__main__.__dict__):
|
def setup_readline(namespace_module=__main__):
|
||||||
"""
|
"""
|
||||||
Install Jedi completer to :mod:`readline`.
|
Install Jedi completer to :mod:`readline`.
|
||||||
|
|
||||||
@@ -51,11 +51,6 @@ def setup_readline(namespace=__main__.__dict__):
|
|||||||
bash).
|
bash).
|
||||||
|
|
||||||
"""
|
"""
|
||||||
try:
|
|
||||||
import readline
|
|
||||||
except ImportError:
|
|
||||||
print("Module readline not available.")
|
|
||||||
else:
|
|
||||||
class JediRL():
|
class JediRL():
|
||||||
def complete(self, text, state):
|
def complete(self, text, state):
|
||||||
"""
|
"""
|
||||||
@@ -68,7 +63,7 @@ def setup_readline(namespace=__main__.__dict__):
|
|||||||
library module.
|
library module.
|
||||||
"""
|
"""
|
||||||
if state == 0:
|
if state == 0:
|
||||||
interpreter = Interpreter(text, [namespace])
|
interpreter = Interpreter(text, [namespace_module.__dict__])
|
||||||
|
|
||||||
# The following part is a bit hackish, because it tries to
|
# The following part is a bit hackish, because it tries to
|
||||||
# directly access some jedi internals. The goal is to just
|
# directly access some jedi internals. The goal is to just
|
||||||
@@ -80,18 +75,18 @@ def setup_readline(namespace=__main__.__dict__):
|
|||||||
is_import = re.match('^\s*(import|from) ', text)
|
is_import = re.match('^\s*(import|from) ', text)
|
||||||
if not is_import and (not path or re.match('^[\w][\w\d.]*$', path)):
|
if not is_import and (not path or re.match('^[\w][\w\d.]*$', path)):
|
||||||
paths = path.split('.') if path else []
|
paths = path.split('.') if path else []
|
||||||
namespaces = (namespace, builtins.__dict__)
|
namespaces = (namespace_module, builtins)
|
||||||
for p in paths:
|
for p in paths:
|
||||||
old, namespaces = namespaces, []
|
old, namespaces = namespaces, []
|
||||||
for n in old:
|
for n in old:
|
||||||
try:
|
try:
|
||||||
namespaces.append(n[p].__dict__)
|
namespaces.append(getattr(n, p))
|
||||||
except (KeyError, AttributeError):
|
except AttributeError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
self.matches = []
|
self.matches = []
|
||||||
for n in namespaces:
|
for n in namespaces:
|
||||||
for name in n.keys():
|
for name in dir(n):
|
||||||
if name.lower().startswith(like.lower()):
|
if name.lower().startswith(like.lower()):
|
||||||
self.matches.append(path + dot + name)
|
self.matches.append(path + dot + name)
|
||||||
else:
|
else:
|
||||||
@@ -105,8 +100,12 @@ def setup_readline(namespace=__main__.__dict__):
|
|||||||
except IndexError:
|
except IndexError:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
try:
|
||||||
|
import readline
|
||||||
|
except ImportError:
|
||||||
|
print("Module readline not available.")
|
||||||
|
else:
|
||||||
readline.set_completer(JediRL().complete)
|
readline.set_completer(JediRL().complete)
|
||||||
|
|
||||||
readline.parse_and_bind("tab: complete")
|
readline.parse_and_bind("tab: complete")
|
||||||
# No delimiters, Jedi handles that.
|
# No delimiters, Jedi handles that.
|
||||||
readline.set_completer_delims('')
|
readline.set_completer_delims('')
|
||||||
|
|||||||
@@ -5,9 +5,13 @@ from .helpers import TestCase
|
|||||||
|
|
||||||
|
|
||||||
class TestSetupReadline(TestCase):
|
class TestSetupReadline(TestCase):
|
||||||
|
class NameSpace():
|
||||||
|
pass
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
super(type(self), self).__init__(*args, **kwargs)
|
super(type(self), self).__init__(*args, **kwargs)
|
||||||
self.namespace = dict()
|
|
||||||
|
self.namespace = self.NameSpace()
|
||||||
utils.setup_readline(self.namespace)
|
utils.setup_readline(self.namespace)
|
||||||
|
|
||||||
def completions(self, text):
|
def completions(self, text):
|
||||||
@@ -37,8 +41,8 @@ class TestSetupReadline(TestCase):
|
|||||||
def test_modules(self):
|
def test_modules(self):
|
||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
self.namespace['sys'] = sys
|
self.namespace.sys = sys
|
||||||
self.namespace['os'] = os
|
self.namespace.os = os
|
||||||
|
|
||||||
assert self.completions('os.path.join') == ['os.path.join']
|
assert self.completions('os.path.join') == ['os.path.join']
|
||||||
assert self.completions('os.path.join().upper') == ['os.path.join().upper']
|
assert self.completions('os.path.join().upper') == ['os.path.join().upper']
|
||||||
@@ -46,14 +50,19 @@ class TestSetupReadline(TestCase):
|
|||||||
c = set(['os.' + d for d in dir(os) if d.startswith('ch')])
|
c = set(['os.' + d for d in dir(os) if d.startswith('ch')])
|
||||||
assert set(self.completions('os.ch')) == set(c)
|
assert set(self.completions('os.ch')) == set(c)
|
||||||
|
|
||||||
del self.namespace['sys']
|
del self.namespace.sys
|
||||||
del self.namespace['os']
|
del self.namespace.os
|
||||||
|
|
||||||
def test_import(self):
|
def test_import(self):
|
||||||
s = 'from os.path import a'
|
s = 'from os.path import a'
|
||||||
assert set(self.completions(s)) == set([s + 'ltsep', s + 'bspath'])
|
assert set(self.completions(s)) == set([s + 'ltsep', s + 'bspath'])
|
||||||
assert self.completions('import keyword') == ['import keyword']
|
assert self.completions('import keyword') == ['import keyword']
|
||||||
|
|
||||||
|
def test_preexisting_values(self):
|
||||||
|
self.namespace.a = range(10)
|
||||||
|
assert set(self.completions('a.')) == set(['a.' + n for n in dir(range(1))])
|
||||||
|
del self.namespace.a
|
||||||
|
|
||||||
def test_colorama(self):
|
def test_colorama(self):
|
||||||
"""
|
"""
|
||||||
Only test it if colorama library is available.
|
Only test it if colorama library is available.
|
||||||
@@ -67,7 +76,7 @@ class TestSetupReadline(TestCase):
|
|||||||
except ImportError:
|
except ImportError:
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
self.namespace['colorama'] = colorama
|
self.namespace.colorama = colorama
|
||||||
assert self.completions('colorama')
|
assert self.completions('colorama')
|
||||||
assert self.completions('colorama.Fore.BLACK') == ['colorama.Fore.BLACK']
|
assert self.completions('colorama.Fore.BLACK') == ['colorama.Fore.BLACK']
|
||||||
del self.namespace['colorama']
|
del self.namespace.colorama
|
||||||
|
|||||||
Reference in New Issue
Block a user