Anonymous committed a5e4506

Added confirm parameter to Rename.get_changes()

  • Participants
  • Parent commits 612b774

Comments (0)

Files changed (3)

File rope/refactor/

     """For finding occurrences of a name"""
     def __init__(self, pycore, name, pynames, only_calls=False,
-                 imports=True, unsure=False, docs=False):
+                 imports=True, unsure=False, docs=False, confirm=None):
         self.pycore = pycore
         self.pynames = pynames = name
         self.only_calls = only_calls
         self.imports = imports
         self.unsure = unsure
+        self.confirm = confirm
         self.occurrence_finder = _TextualFinder(name, docs=docs)
     def find_occurrences(self, resource=None, pymodule=None):
                 return True
             elif self.unsure and self._unsure_match(pyname, new_pyname):
                 occurrence._unsure = True
+                if self.confirm is not None:
+                    return self.confirm(occurrence)
                 return True
         return False
     def _unsure_match(self, expected, pyname):
     def __init__(self, tools, offset): = tools
         self.offset = offset
+        self.resource = tools.resource
     _unsure = False

File rope/refactor/

         return self.old_name
     def get_changes(self, new_name, in_file=False, in_hierarchy=False,
-                    unsure=False, docs=False,
+                    unsure=False, docs=False, confirm=None,
         """Get the changes needed for this refactoring
               unless you know what you're doing.  If `True`, all name
               occurrences except those that we are sure are not what we
               want are renamed.
+            - `confirm`: when renaming unsure occurrences this function
+              is called with an instance of `occorrences.Occurrence`.
+              If it returns `True` the occurrence will be renamed
         old_pynames = self._get_old_pynames(in_file, in_hierarchy, task_handle)
         changes = ChangeSet('Renaming <%s> to <%s>' %
                             (self.old_name, new_name))
         finder = occurrences.FilteredFinder(
-            self.pycore, self.old_name, old_pynames, unsure=unsure, docs=docs)
+            self.pycore, self.old_name, old_pynames,
+            unsure=unsure, docs=docs, confirm=confirm)
         job_set = task_handle.create_jobset('Collecting Changes', len(files))
         for file_ in files:
             job_set.started_job('Working on <%s>' % file_.path)

File ropetest/refactor/

             'def f(arg):\n    arg.new_func()\n',
+    def test_renaming_when_unsure_with_confirmation(self):
+        def confirm(occurrence):
+            return False
+        code = 'class C(object):\n    def a_func(self):\n        pass\n' \
+               'def f(arg):\n    arg.a_func()\n'
+        mod1 = self.pycore.create_module(self.project.root, 'mod1')
+        mod1.write(code)
+        self._rename(mod1, code.index('a_func'),
+                     'new_func', unsure=True, confirm=confirm)
+        self.assertEquals(
+            'class C(object):\n    def new_func(self):\n        pass\n' \
+            'def f(arg):\n    arg.a_func()\n',
     def test_renaming_when_unsure_not_renaming_knowns(self):
         code = 'class C1(object):\n    def a_func(self):\n        pass\n' \
                'class C2(object):\n    def a_func(self):\n        pass\n' \