Commits

Ali Gholami Rudi  committed 6d58a8a

refactor: moved runtask() to refactor

  • Participants
  • Parent commits 1f166c1

Comments (0)

Files changed (2)

File ropemode/interface.py

             def undo(handle):
                 for changes in self.project.history.undo(task_handle=handle):
                     self._reload_buffers(changes, undo=True)
-            self.env.runtask(undo, 'Undo refactoring', interrupts=False)
+            refactor.runtask(self.env, undo, 'Undo refactoring',
+                             interrupts=False)
 
     @decorators.global_command('r')
     def redo(self):
             def redo(handle):
                 for changes in self.project.history.redo(task_handle=handle):
                     self._reload_buffers(changes)
-            self.env.runtask(redo, 'Redo refactoring', interrupts=False)
+            refactor.runtask(self.env, redo, 'Redo refactoring',
+                             interrupts=False)
 
     @decorators.local_command('a g', shortcut='C-c g')
     def goto_definition(self):
                                                 values.get('resources'))
                 return do_find(self.project, resource, offset,
                                resources=resources, task_handle=handle, **kwds)
-            result = self.env.runtask(calculate, 'Find Occurrences')
+            result = refactor.runtask(self.env, calculate, 'Find Occurrences')
             locations = []
             for occurrence in result:
                 note = ''
         def generate(handle):
             self.autoimport.generate_cache(task_handle=handle)
             self.autoimport.generate_modules_cache(modules, task_handle=handle)
-        self.env.runtask(generate, 'Generate autoimport cache')
+        refactor.runtask(self.env, generate, 'Generate autoimport cache')
 
     @decorators.global_command('f', 'P')
     def find_file(self, prefix):
         self._check_project()
         def _analyze_modules(handle):
             libutils.analyze_modules(self.project, task_handle=handle)
-        self.env.runtask(_analyze_modules, 'Analyze project modules')
+        refactor.runtask(self.env, _analyze_modules, 'Analyze project modules')
 
     def _create(self, name, callback, parentname='source'):
         self._check_project()

File ropemode/refactor.py

 import rope.refactor.rename
 import rope.refactor.restructure
 import rope.refactor.usefunction
+from rope.base import taskhandle
 
 from ropemode import dialog, filter
 
         def calculate(handle):
             return self._calculate_changes(result, handle)
         name = 'Calculating %s changes' % self.name
-        changes = self.env.runtask(calculate, name=name)
+        changes = runtask(self.env, calculate, name=name)
         if action == 'perform':
             self._perform(changes)
         if action == 'preview':
             self.project.do(changes, task_handle=handle)
             self.interface._reload_buffers(changes)
             self._done()
-        self.env.runtask(perform, 'Making %s changes' % self.name,
-                          interrupts=False)
+        runtask(self.env, perform, 'Making %s changes' % self.name,
+                interrupts=False)
         self.env.message(str(changes.description) + ' finished')
 
     def _get_confs(self):
     if text is None or text.strip() == '':
         return None
     return filter.resources(project, text)
+
+
+def runtask(env, command, name, interrupts=True):
+    return RunTask(env, command, name, interrupts)()
+
+class RunTask(object):
+
+    def __init__(self, env, task, name, interrupts=True):
+        self.env = env
+        self.task = task
+        self.name = name
+        self.interrupts = interrupts
+
+    def __call__(self):
+        handle = taskhandle.TaskHandle(name=self.name)
+        progress = self.env.create_progress(self.name)
+        def update_progress():
+            jobset = handle.current_jobset()
+            if jobset:
+                percent = jobset.get_percent_done()
+                if percent is not None:
+                    progress.update(percent)
+        handle.add_observer(update_progress)
+        result = self.task(handle)
+        progress.done()
+        return result