Skip to content

Commit 5bece9d

Browse files
committed
make import completion not fall back to regular completion on no matches
1 parent cab1620 commit 5bece9d

File tree

2 files changed

+28
-22
lines changed

2 files changed

+28
-22
lines changed

bpython/cli.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,10 @@ def _complete(self, unused_tab=False):
561561
# Check for import completion
562562
e = False
563563
matches = importcompletion.complete(self.s, cw)
564+
if matches is None:
565+
self.scr.redrawwin()
566+
return False
567+
564568
if not matches:
565569
# Nope, no import, continue with normal completion
566570
try:

bpython/importcompletion.py

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,30 +33,32 @@
3333
def complete(line, cw):
3434
"""Construct a full list of possibly completions for imports."""
3535
tokens = line.split()
36-
if tokens[0] in ['from', 'import']:
37-
completing_from = False
38-
if tokens[0] == 'from':
39-
if len(tokens) > 3:
40-
if '.' in cw:
41-
# This will result in a SyntaxError, so do not return
42-
# any matches
43-
return list()
44-
completing_from = True
45-
cw = '%s.%s' % (tokens[1], cw)
46-
elif len(tokens) == 3:
47-
return ['import']
48-
49-
matches = list()
50-
for name in modules:
51-
if not (name.startswith(cw) and name.find('.', len(cw)) == -1):
52-
continue
53-
if completing_from:
54-
name = name[len(tokens[1]) + 1:]
55-
matches.append(name)
56-
return matches
57-
else:
36+
if tokens[0] not in ['from', 'import']:
5837
return list()
5938

39+
completing_from = False
40+
if tokens[0] == 'from':
41+
if len(tokens) > 3:
42+
if '.' in cw:
43+
# This will result in a SyntaxError, so do not return
44+
# any matches
45+
return list()
46+
completing_from = True
47+
cw = '%s.%s' % (tokens[1], cw)
48+
elif len(tokens) == 3:
49+
return ['import']
50+
51+
matches = list()
52+
for name in modules:
53+
if not (name.startswith(cw) and name.find('.', len(cw)) == -1):
54+
continue
55+
if completing_from:
56+
name = name[len(tokens[1]) + 1:]
57+
matches.append(name)
58+
if not matches:
59+
return None
60+
return matches
61+
6062

6163
def find_modules(path):
6264
"""Find all modules (and packages) for a given directory."""

0 commit comments

Comments
 (0)