1010import json
1111import multiprocessing
1212from .utils import (
13- message , PY2 , error , pymode_input , pymode_inputlist , pymode_y_n )
13+ pymode_message , PY2 , pymode_error , pymode_input , pymode_inputlist , pymode_y_n )
1414
1515if PY2 :
1616 sys .path .insert (0 , os .path .join (os .path .dirname (__file__ ), 'libs' ))
1919
2020from rope .base import project , libutils , exceptions , change # noqa
2121from rope .base .fscommands import FileSystemCommands # noqa
22- from rope .contrib import autoimport , codeassist , findit # noqa
22+ from rope .contrib import autoimport as rope_autoimport , codeassist , findit # noqa
2323from rope .refactor import ModuleToPackage , ImportOrganizer , rename , extract , inline , usefunction , move # noqa
2424from rope .base .taskhandle import TaskHandle # noqa
2525
@@ -193,7 +193,7 @@ def show_doc():
193193 raise exceptions .BadIdentifierError
194194 vim .command ('let l:output = %s' % json .dumps (doc .split ('\n ' )))
195195 except exceptions .BadIdentifierError :
196- error ("No documentation found." )
196+ pymode_error ("No documentation found." )
197197
198198
199199def find_it ():
@@ -259,7 +259,7 @@ def new():
259259 root = vim .eval ('input("Enter project root: ", getcwd())' )
260260 prj = project .Project (projectroot = root )
261261 prj .close ()
262- message ("Project is opened: %s" % root )
262+ pymode_message ("Project is opened: %s" % root )
263263
264264
265265def undo ():
@@ -272,7 +272,7 @@ def undo():
272272 with RopeContext () as ctx :
273273 changes = ctx .project .history .tobe_undone
274274 if changes is None :
275- error ('Nothing to undo!' )
275+ pymode_error ('Nothing to undo!' )
276276 return False
277277
278278 if pymode_y_n (yes = False , msg = 'Undo [%s]?' % str (changes )):
@@ -291,7 +291,7 @@ def redo():
291291 with RopeContext () as ctx :
292292 changes = ctx .project .history .tobe_redone
293293 if changes is None :
294- error ('Nothing to redo!' )
294+ pymode_error ('Nothing to redo!' )
295295 return False
296296
297297 if pymode_y_n (yes = False , msg = 'Redo [%s]?' % str (changes )):
@@ -318,6 +318,41 @@ def get_ctx(*args, **kwargs):
318318 return get_ctx
319319
320320
321+ def autoimport ():
322+ """ Autoimport modules.
323+
324+ :return bool:
325+
326+ """
327+ word = vim .eval ('a:word' )
328+ if not word :
329+ pymode_error ("Should be word under cursor." )
330+ return False
331+
332+ def insert_import (name , module , ctx , source ):
333+ linenum = int (ctx .importer .find_insertion_line (source ))
334+ line = 'from %s insert %s' % (module , name )
335+ vim .current .buffer [linenum - 1 :linenum - 1 ] = [line ]
336+
337+ with RopeContext () as ctx :
338+ if not ctx .importer .names :
339+ ctx .generate_autoimport_cache ()
340+ modules = ctx .importer .get_modules (word )
341+ if not modules :
342+ pymode_message ('Global name %s not found.' % word )
343+ return False
344+
345+ source , _ = get_assist_params ()
346+ if len (modules ) == 1 :
347+ insert_import (word , modules [0 ], ctx , source )
348+
349+ else :
350+ module = pymode_inputlist ('Wich module to import:' , modules )
351+ insert_import (word , module , ctx , source )
352+
353+ return True
354+
355+
321356@cache_project
322357class RopeContext (object ):
323358
@@ -331,8 +366,8 @@ def __init__(self, path, project_path):
331366 self .project = project .Project (
332367 project_path , fscommands = FileSystemCommands ())
333368
334- self .importer = autoimport .AutoImport (project = self . project ,
335- observe = False )
369+ self .importer = rope_autoimport .AutoImport (
370+ project = self . project , observe = False )
336371
337372 update_python_path (self .project .prefs .get ('python_path' , []))
338373
@@ -388,7 +423,7 @@ def __init__(self, msg):
388423 def __call__ (self ):
389424 """ Show current progress. """
390425 percent_done = self .handle .current_jobset ().get_percent_done ()
391- message ('%s - done %s%%' % (self .message , percent_done ))
426+ pymode_message ('%s - done %s%%' % (self .message , percent_done ))
392427
393428
394429_scope_weight = {
@@ -413,7 +448,7 @@ def run(self):
413448
414449 with RopeContext () as ctx :
415450 try :
416- message (self .__doc__ )
451+ pymode_message (self .__doc__ )
417452 refactor = self .get_refactor (ctx )
418453 input_str = self .get_input_str (refactor , ctx )
419454 if not input_str :
@@ -439,10 +474,10 @@ def run(self):
439474 ctx .project .do (changes , task_handle = progress .handle )
440475 reload_changes (changes )
441476 except exceptions .RefactoringError as e :
442- error (str (e ))
477+ pymode_error (str (e ))
443478
444479 except Exception as e :
445- error ('Unhandled exception in Pymode: %s' % e )
480+ pymode_error ('Unhandled exception in Pymode: %s' % e )
446481
447482 @staticmethod
448483 def get_refactor (ctx ):
@@ -501,7 +536,7 @@ def get_input_str(self, refactor, ctx):
501536 newname = pymode_input (msg , oldname )
502537
503538 if newname == oldname :
504- message ("Nothing to do." )
539+ pymode_message ("Nothing to do." )
505540 return False
506541
507542 return newname
0 commit comments