Commits

Ali Gholami Rudi  committed 3ab8078

Changed rename refactoring to use new dialogs

  • Participants
  • Parent commits 5e1c7dc

Comments (0)

Files changed (2)

File ropemacs/__init__.py

             offset = None
         renamer = rope.refactor.rename.Rename(self.project, resource, offset)
         oldname = str(renamer.get_old_name())
-        newname = _ask('New name for %s: ' % oldname, starting=oldname)
-        changes = renamer.get_changes(newname, docs=True)
-        self._perform(changes)
+        confs = {'newname': config.Data('New name for %s: ' % oldname, starting=oldname)}
+        optionals = {
+            'docs': config.Data('Rename occurrences in comments and docs: ', values=['yes', 'no']),
+            'in_hierarchy': config.Data('Rename method occurrences in class hierarchy: ', values=['yes', 'no']),
+            'unsure': config.Data('Unsure occurrences: ', values=['ignore', 'match'])}
+        def get_changes(result):
+            newname = result['newname']
+            unsure = result.get('unsure', 'no') == 'yes'
+            kwds = {
+                'docs': result.get('docs', 'yes') == 'yes',
+                'unsure': (lambda occurrence: unsure)}
+            if renamer.is_method():
+                kwds['in_hierarchy'] = result.get('in_hierarchy', 'no') == 'yes'
+            return renamer.get_changes(newname, **kwds)
+        self._dialog(get_changes, confs, optionals)
 
     @interactive
     def rename(self):
     @interactive
     def restructure(self):
         self._check_project()
+        self._save_buffers()
         configs = {'pattern': config.Data('Restructuring pattern: '),
                    'goal': config.Data('Restructuring goal: ')}
         optionals = {'checks': config.Data('Checks: '),
                      'imports': config.Data('Imports: ')}
-        action, result = config.show_dialog(
-            _lisp_askdata, ['perform', 'cancel'], configs, optionals)
-        if action != 'perform':
-            lisp.message('Cancelled!')
-            return
-        restructuring = rope.refactor.restructure.Restructure(
-            self.project, result['pattern'], result['goal'])
-        check_dict = {}
-        for raw_check in result.get('checks', '').split('\n'):
-            if raw_check:
-                key, value = raw_check.split('==')
-                check_dict[key.strip()] = value.strip()
-        checks = restructuring.make_checks(check_dict)
-        imports = [line.strip()
-                   for line in result.get('imports', '').split('\n')]
-        changes = restructuring.get_changes(checks=checks, imports=imports)
-        self._perform(changes)
+        def calculate(result):
+            restructuring = rope.refactor.restructure.Restructure(
+                self.project, result['pattern'], result['goal'])
+            check_dict = {}
+            for raw_check in result.get('checks', '').split('\n'):
+                if raw_check:
+                    key, value = raw_check.split('==')
+                    check_dict[key.strip()] = value.strip()
+            checks = restructuring.make_checks(check_dict)
+            imports = [line.strip()
+                       for line in result.get('imports', '').split('\n')]
+            return restructuring.get_changes(checks=checks, imports=imports)
+        self._dialog(calculate, configs, optionals)
 
     @interactive
     def organize_imports(self):
         organizer = rope.refactor.ImportOrganizer(self.project)
         self._perform(organizer.organize_imports(self._get_resource()))
 
+    def _dialog(self, calculate_changes, configs={}, optionals={}):
+        action, result = config.show_dialog(
+            _lisp_askdata, ['perform', 'cancel'], configs, optionals)
+        if action != 'perform':
+            lisp.message('Cancelled!')
+            return
+        changes = calculate_changes(result)
+        self._perform(changes)
+
     def _perform(self, changes):
         if changes is None:
             return
                                         None, default, None)
     if result == '' and default is not None:
         return default
+    return result
 
 def _ask_values(prompt, values, default=None, starting=None, exact=True):
     if exact and default is not None:
     result = lisp.completing_read(prompt, values, None, exact, starting)
     if result == '' and exact:
         return default
+    return result
 
 def _lisp_askdata(data):
     if data.values:
-        return _ask_values(data.prompt, data.values, default=data.default)
+        return _ask_values(data.prompt, data.values, default=data.default,
+                           starting=data.starting)
     else:
-        return _ask(data.prompt, default=data.default)
+        return _ask(data.prompt, default=data.default, starting=data.starting)
 
 
 DEFVARS = """\

File ropemacs/config.py

 class Data(object):
 
-    def __init__(self, prompt=None, default=None, values=None):
+    def __init__(self, prompt=None, default=None,
+                 values=None, starting=None):
         self.prompt = prompt
         self.default = default
         self.values = values
+        self.starting = starting
 
 
 def show_dialog(askdata, actions, confs={}, optionals={}):