Anonymous avatar Anonymous committed a5e4506

Added confirm parameter to Rename.get_changes()

Comments (0)

Files changed (3)

rope/refactor/occurrences.py

     """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
         self.name = 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):
         self.tools = tools
         self.offset = offset
+        self.resource = tools.resource
 
     _unsure = False
 

rope/refactor/rename.py

         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,
                     task_handle=taskhandle.NullTaskHandle()):
         """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)

ropetest/refactor/renametest.py

             'def f(arg):\n    arg.new_func()\n',
             mod1.read())
 
+    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', mod1.read())
+
     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' \
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.