Skip to content

Commit d3fa4c9

Browse files
More work on attribut completion on tab
1 parent a015a7c commit d3fa4c9

File tree

4 files changed

+40
-1
lines changed

4 files changed

+40
-1
lines changed

bpython/autocomplete.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,9 @@ def locate(self, current_offset, line):
240240
def format(self, word):
241241
return after_last_dot(word)
242242

243+
def try_to_complete(self, module):
244+
return importcompletion.try_to_import(module)
245+
243246

244247
class FilenameCompletion(BaseCompletionType):
245248
def __init__(self, mode=SIMPLE):

bpython/curtsiesfrontend/repl.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
from bpython import autocomplete
3838
from bpython.translations import _
3939
from bpython.pager import get_pager_command
40+
from bpython.line import from_import_tab
4041

4142
from bpython.curtsiesfrontend import replpainter as paint
4243
from bpython.curtsiesfrontend import sitefix
@@ -887,6 +888,15 @@ def only_whitespace_left_of_cursor():
887888
for unused in range(to_add):
888889
self.add_normal_character(" ")
889890
return
891+
892+
# if line is `from a import` and tab then import module and attr
893+
line = self.current_line.split()
894+
module_name = ""
895+
if "from" in line and "import" in line:
896+
module_name = from_import_tab(self.current_line)
897+
if module_name:
898+
self.status_bar.message("Importing module %s" % module_name)
899+
self.completers[1].try_to_complete(module_name)
890900

891901
# run complete() if we don't already have matches
892902
if len(self.matches_iter.matches) == 0:

bpython/importcompletion.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,19 @@ def module_attr_matches(name):
9292
return attr_matches(name, prefix="", only_modules=True)
9393

9494

95+
def try_to_import(module_name):
96+
"""If this hasn't been imported our goal is to add it to the set of modules and actually import it"""
97+
if module_name not in sys.modules:
98+
try:
99+
module = __import__(module_name)
100+
module_path = os.path.dirname(module.__file__)
101+
for mod in find_modules(module_path):
102+
modules.add("%s.%s" % (module_name, mod))
103+
modules.add(module_name)
104+
except ImportError:
105+
pass
106+
107+
95108
def complete(cursor_offset, line):
96109
"""Construct a full list of possibly completions for imports."""
97110
tokens = line.split()
@@ -205,7 +218,7 @@ def find_all_modules(path=None):
205218
p = os.curdir
206219
for module in find_modules(p):
207220
module = try_decode(module, "ascii")
208-
if module is None or "numpy" in module:
221+
if module is None:
209222
continue
210223
modules.add(module)
211224
yield

bpython/line.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,19 @@ def current_object_attribute(cursor_offset, line):
117117
return None
118118

119119

120+
from_import_tab_re = LazyReCompile(r"from ([\w0-9_.]*)\s+import")
121+
122+
123+
def from_import_tab(line):
124+
matches = from_import_tab_re.finditer(line)
125+
try:
126+
module = matches.__next__()
127+
except StopIteration:
128+
pass
129+
if module:
130+
return str(module[1])
131+
132+
120133
current_from_import_from_re = LazyReCompile(
121134
r"from ([\w0-9_.]*)(?:\s+import\s+([\w0-9_]+[,]?\s*)+)*"
122135
)

0 commit comments

Comments
 (0)