Skip to content

Commit 2e8c5e2

Browse files
worked on message timing and to events
1 parent d3fa4c9 commit 2e8c5e2

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
@@ -240,8 +240,8 @@ 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)
243+
def try_to_import(self, module_name):
244+
return importcompletion.try_to_import(module_name)
245245

246246

247247
class FilenameCompletion(BaseCompletionType):

bpython/curtsies.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ def __init__(self, config, locals_, banner, interp=None):
5454
self.request_undo = self.input_generator.event_trigger(
5555
bpythonevents.UndoEvent
5656
)
57+
self.background_import_event = self.input_generator.event_trigger(
58+
bpythonevents.BackgroundImportEvent
59+
)
5760

5861
with self.input_generator:
5962
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
@@ -45,3 +45,10 @@ class UndoEvent(curtsies.events.Event):
4545

4646
def __init__(self, n=1):
4747
self.n = n
48+
49+
50+
class BackgroundImportEvent(curtsies.events.Event):
51+
"""Background import for advanced auto complete."""
52+
53+
def __init__(self, module_name):
54+
self.module_name = module_name

bpython/curtsiesfrontend/repl.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,11 @@ def process_control_event(self, e):
690690
_("Reloaded at %s because %s modified.")
691691
% (time.strftime("%X"), " & ".join(e.files_modified))
692692
)
693+
694+
elif isinstance(e, bpythonevents.BackgroundImportEvent):
695+
#self.comleters[1] should match to autocomplete's ImportCompletion class
696+
self.completers[1].try_to_import(e.module_name)
697+
self.status_bar.pop_permanent_message("Imported module %s" % e.module_name)
693698

694699
else:
695700
raise ValueError("Don't know how to handle event type: %r" % e)
@@ -873,6 +878,7 @@ def on_tab(self, back=False):
873878
2) complete the current word with characters common to all completions
874879
3) select the first or last match
875880
4) select the next or previous match if already have a match
881+
5) if line is `from <module> import` and tab then import module and attr
876882
"""
877883

878884
def only_whitespace_left_of_cursor():
@@ -889,14 +895,19 @@ def only_whitespace_left_of_cursor():
889895
self.add_normal_character(" ")
890896
return
891897

892-
# if line is `from a import` and tab then import module and attr
898+
#5. if line is `from a import` and tab then import module and attr
893899
line = self.current_line.split()
894900
module_name = ""
895901
if "from" in line and "import" in line:
902+
#returns <module> name if the line contains: "from <module> import"
896903
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)
904+
print(module_name)
905+
if module_name:
906+
if module_name not in sys.modules:
907+
self.status_bar.push_permanent_message("Imported module %s" % module_name)
908+
self.background_import_event(module_name=module_name)
909+
910+
900911

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

bpython/importcompletion.py

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

9494

9595
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
96+
"""Called by curtsiesfrontend repl on tab to import module
97+
in background for better auto completion: attr completion"""
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
106106

107107

108108
def complete(cursor_offset, line):

0 commit comments

Comments
 (0)