|
31 | 31 |
|
32 | 32 | from glob import glob |
33 | 33 |
|
34 | | -import jedi |
35 | | - |
36 | 34 | from bpython import inspection |
37 | 35 | from bpython import importcompletion |
38 | 36 | from bpython import line as lineparts |
@@ -311,42 +309,52 @@ def matches(self, cursor_offset, line, **kwargs): |
311 | 309 | def locate(self, current_offset, line): |
312 | 310 | return lineparts.current_string_literal_attr(current_offset, line) |
313 | 311 |
|
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): |
349 | 318 | 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 |
350 | 358 |
|
351 | 359 |
|
352 | 360 | def get_completer(completers, cursor_offset, line, **kwargs): |
|
0 commit comments