Skip to content

Commit 32f5c8d

Browse files
worked on message timing and to events
1 parent 7a492a1 commit 32f5c8d

File tree

5 files changed

+37
-16
lines changed

5 files changed

+37
-16
lines changed

bpython/autocomplete.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,8 @@ def locate(self, current_offset, line):
251251
def format(self, word):
252252
return after_last_dot(word)
253253

254-
def try_to_complete(self, module):
255-
return importcompletion.try_to_import(module)
254+
def try_to_import(self, module_name):
255+
return importcompletion.try_to_import(module_name)
256256

257257

258258
class FilenameCompletion(BaseCompletionType):

bpython/curtsies.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ def __init__(self, config, locals_, banner, interp=None):
5959
self.request_undo = self.input_generator.event_trigger(
6060
bpythonevents.UndoEvent
6161
)
62+
self.background_import_event = self.input_generator.event_trigger(
63+
bpythonevents.BackgroundImportEvent
64+
)
6265

6366
with self.input_generator:
6467
pass # temp hack to get .original_stty

bpython/curtsiesfrontend/events.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,10 @@ class UndoEvent(curtsies.events.Event):
4747

4848
def __init__(self, n=1):
4949
self.n = n
50+
51+
52+
class BackgroundImportEvent(curtsies.events.Event):
53+
"""Background import for advanced auto complete."""
54+
55+
def __init__(self, module_name):
56+
self.module_name = module_name

bpython/curtsiesfrontend/repl.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,11 @@ def process_control_event(self, e):
701701
_("Reloaded at %s because %s modified.")
702702
% (time.strftime("%X"), " & ".join(e.files_modified))
703703
)
704+
705+
elif isinstance(e, bpythonevents.BackgroundImportEvent):
706+
#self.comleters[1] should match to autocomplete's ImportCompletion class
707+
self.completers[1].try_to_import(e.module_name)
708+
self.status_bar.pop_permanent_message("Imported module %s" % e.module_name)
704709

705710
else:
706711
raise ValueError("Don't know how to handle event type: %r" % e)
@@ -882,6 +887,7 @@ def on_tab(self, back=False):
882887
2) complete the current word with characters common to all completions
883888
3) select the first or last match
884889
4) select the next or previous match if already have a match
890+
5) if line is `from <module> import` and tab then import module and attr
885891
"""
886892

887893
def only_whitespace_left_of_cursor():
@@ -898,14 +904,19 @@ def only_whitespace_left_of_cursor():
898904
self.add_normal_character(" ")
899905
return
900906

901-
# if line is `from a import` and tab then import module and attr
907+
#5. if line is `from a import` and tab then import module and attr
902908
line = self.current_line.split()
903909
module_name = ""
904910
if "from" in line and "import" in line:
911+
#returns <module> name if the line contains: "from <module> import"
905912
module_name = from_import_tab(self.current_line)
906-
if module_name:
907-
self.status_bar.message("Importing module %s" % module_name)
908-
self.completers[1].try_to_complete(module_name)
913+
print(module_name)
914+
if module_name:
915+
if module_name not in sys.modules:
916+
self.status_bar.push_permanent_message("Imported module %s" % module_name)
917+
self.background_import_event(module_name=module_name)
918+
919+
909920

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

bpython/importcompletion.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -101,16 +101,16 @@ def module_attr_matches(name):
101101

102102

103103
def try_to_import(module_name):
104-
"""If this hasn't been imported our goal is to add it to the set of modules and actually import it"""
105-
if module_name not in sys.modules:
106-
try:
107-
module = __import__(module_name)
108-
module_path = os.path.dirname(module.__file__)
109-
for mod in find_modules(module_path):
110-
modules.add("%s.%s" % (module_name, mod))
111-
modules.add(module_name)
112-
except ImportError:
113-
pass
104+
"""Called by curtsiesfrontend repl on tab to import module
105+
in background for better auto completion: attr completion"""
106+
try:
107+
module = __import__(module_name)
108+
module_path = os.path.dirname(module.__file__)
109+
for mod in find_modules(module_path):
110+
modules.add("%s.%s" % (module_name, mod))
111+
modules.add(module_name)
112+
except ImportError:
113+
pass
114114

115115

116116
def complete(cursor_offset, line):

0 commit comments

Comments
 (0)