Skip to content

Commit b58db29

Browse files
import completion uses less local functions (more testable!)
1 parent 695ba7c commit b58db29

File tree

1 file changed

+33
-36
lines changed

1 file changed

+33
-36
lines changed

bpython/importcompletion.py

Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,41 @@ def catch_warnings():
5656
fully_loaded = False
5757

5858

59+
def module_matches(cw, prefix=''):
60+
"""Modules names to replace cw with"""
61+
full = '%s.%s' % (prefix, cw) if prefix else cw
62+
matches = [name for name in modules
63+
if (name.startswith(full) and
64+
name.find('.', len(full)) == -1)]
65+
if prefix:
66+
return [match[len(prefix)+1:] for match in matches]
67+
else:
68+
return matches
69+
70+
def attr_matches(cw, prefix='', only_modules=False):
71+
"""Attributes to replace name with"""
72+
full = '%s.%s' % (prefix, cw) if prefix else cw
73+
module_name, _, name_after_dot = full.rpartition('.')
74+
if module_name not in sys.modules:
75+
return []
76+
module = sys.modules[module_name]
77+
if only_modules:
78+
matches = [name for name in dir(module)
79+
if name.startswith(name_after_dot) and
80+
'%s.%s' % (module_name, name) in sys.modules]
81+
else:
82+
matches = [name for name in dir(module) if name.startswith(name_after_dot)]
83+
module_part, _, _ = cw.rpartition('.')
84+
if module_part:
85+
return ['%s.%s' % (module_part, m) for m in matches]
86+
return matches
87+
88+
def module_attr_matches(name):
89+
"""Only attributes which are modules to replace name with"""
90+
return attr_matches(name, prefix='', only_modules=True)
91+
5992
def complete(cursor_offset, line):
6093
"""Construct a full list of possibly completions for imports."""
61-
# TODO if this is done in a thread (as it prob will be in Windows) we'll need this
62-
# if not fully_loaded:
63-
# return []
6494
tokens = line.split()
6595
if 'from' not in tokens and 'import' not in tokens:
6696
return None
@@ -69,39 +99,6 @@ def complete(cursor_offset, line):
6999
if result is None:
70100
return None
71101

72-
def module_matches(cw, prefix=''):
73-
"""Modules names to replace cw with"""
74-
full = '%s.%s' % (prefix, cw) if prefix else cw
75-
matches = [name for name in modules
76-
if (name.startswith(full) and
77-
name.find('.', len(full)) == -1)]
78-
if prefix:
79-
return [match[len(prefix)+1:] for match in matches]
80-
else:
81-
return matches
82-
83-
def attr_matches(cw, prefix='', only_modules=False):
84-
"""Attributes to replace name with"""
85-
full = '%s.%s' % (prefix, cw) if prefix else cw
86-
module_name, _, name_after_dot = full.rpartition('.')
87-
if module_name not in sys.modules:
88-
return []
89-
module = sys.modules[module_name]
90-
if only_modules:
91-
matches = [name for name in dir(module)
92-
if name.startswith(name_after_dot) and
93-
'%s.%s' % (module_name, name) in sys.modules]
94-
else:
95-
matches = [name for name in dir(module) if name.startswith(name_after_dot)]
96-
module_part, _, _ = cw.rpartition('.')
97-
if module_part:
98-
return ['%s.%s' % (module_part, m) for m in matches]
99-
return matches
100-
101-
def module_attr_matches(name):
102-
"""Only attributes which are modules to replace name with"""
103-
return attr_matches(name, prefix='', only_modules=True)
104-
105102
if lineparts.current_from_import_from(cursor_offset, line) is not None:
106103
if lineparts.current_from_import_import(cursor_offset, line) is not None:
107104
# `from a import <b|>` completion

0 commit comments

Comments
 (0)