X Tutup
Skip to content

Commit 6a38aab

Browse files
make jedi optional dependency
1 parent 57b94b0 commit 6a38aab

File tree

4 files changed

+49
-42
lines changed

4 files changed

+49
-42
lines changed

.travis.install.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ if [[ $RUN == nosetests ]]; then
99
pip install pygments requests 'curtsies >=0.1.17,<0.2.0' greenlet
1010
# filewatch specific dependencies
1111
pip install watchdog
12+
# jedi specific dependencies
13+
pip install jedi
1214
# translation specific dependencies
1315
pip install babel
1416
# Python 2.6 specific dependencies

bpython/autocomplete.py

Lines changed: 45 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@
3131

3232
from glob import glob
3333

34-
import jedi
35-
3634
from bpython import inspection
3735
from bpython import importcompletion
3836
from bpython import line as lineparts
@@ -311,42 +309,52 @@ def matches(self, cursor_offset, line, **kwargs):
311309
def locate(self, current_offset, line):
312310
return lineparts.current_string_literal_attr(current_offset, line)
313311

314-
class JediCompletion(BaseCompletionType):
315-
@classmethod
316-
def matches(self, cursor_offset, line, history, **kwargs):
317-
if not lineparts.current_word(cursor_offset, line):
318-
return None
319-
history = '\n'.join(history) + '\n' + line
320-
script = jedi.Script(history, len(history.splitlines()), cursor_offset, 'fake.py')
321-
completions = script.completions()
322-
if completions:
323-
self._original = completions[0]
324-
else:
325-
self._original = None
326-
327-
matches = [c.name for c in completions]
328-
if all(m.startswith('_') for m in matches):
329-
return matches
330-
elif any(not m.startswith(matches[0][0]) for m in matches):
331-
return matches
332-
else:
333-
return [m for m in matches if not m.startswith('_')]
334-
335-
def locate(self, cursor_offset, line):
336-
start = cursor_offset - (len(self._original.name) - len(self._original.complete))
337-
end = cursor_offset
338-
return start, end, line[start:end]
339-
340-
341-
class MultilineJediCompletion(JediCompletion):
342-
@classmethod
343-
def matches(cls, cursor_offset, line, current_block, history, **kwargs):
344-
if '\n' in current_block:
345-
assert cursor_offset <= len(line), "%r %r" % (cursor_offset, line)
346-
results = JediCompletion.matches(cursor_offset, line, history)
347-
return results
348-
else:
312+
try:
313+
import jedi
314+
except ImportError:
315+
class MultilineJediCompletion(BaseCompletionType):
316+
@classmethod
317+
def matches(cls, cursor_offset, line, **kwargs):
349318
return None
319+
else:
320+
class JediCompletion(BaseCompletionType):
321+
@classmethod
322+
def matches(self, cursor_offset, line, history, **kwargs):
323+
if not lineparts.current_word(cursor_offset, line):
324+
return None
325+
history = '\n'.join(history) + '\n' + line
326+
script = jedi.Script(history, len(history.splitlines()), cursor_offset, 'fake.py')
327+
completions = script.completions()
328+
if completions:
329+
self._orig_start = cursor_offset - (len(completions[0].name) - len(completions[0].complete))
330+
else:
331+
self._orig_start = None
332+
return None
333+
334+
first_letter = line[self._orig_start:self._orig_start+1]
335+
336+
matches = [c.name for c in completions]
337+
if any(not m.lower().startswith(matches[0][0].lower()) for m in matches):
338+
return None # Too general - giving completions starting with multiple letters
339+
else:
340+
# case-sensitive matches only
341+
return [m for m in matches if m.startswith(first_letter)]
342+
343+
def locate(self, cursor_offset, line):
344+
start = self._orig_start
345+
end = cursor_offset
346+
return start, end, line[start:end]
347+
348+
349+
class MultilineJediCompletion(JediCompletion):
350+
@classmethod
351+
def matches(cls, cursor_offset, line, current_block, history, **kwargs):
352+
if '\n' in current_block:
353+
assert cursor_offset <= len(line), "%r %r" % (cursor_offset, line)
354+
results = JediCompletion.matches(cursor_offset, line, history)
355+
return results
356+
else:
357+
return None
350358

351359

352360
def get_completer(completers, cursor_offset, line, **kwargs):

bpython/test/test_autocomplete.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@
66
except ImportError:
77
import unittest
88

9-
#TODO: Parts of autocompletion to test:
10-
# Test that the right matches come back from find_matches (test that priority is correct)
11-
# Test the various complete methods (import, filename) to see if right matches
12-
# Test that MatchesIterator.substitute correctly subs given a match and a completer
139

1410
class TestSafeEval(unittest.TestCase):
1511
def test_catches_syntax_error(self):

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,8 @@ def initialize_options(self):
174174

175175
extras_require = {
176176
'urwid' : ['urwid'],
177-
'watch' : ['watchdog']
177+
'watch' : ['watchdog'],
178+
'jedi' : ['jedi'],
178179
}
179180

180181
packages = [

0 commit comments

Comments
 (0)
X Tutup