Anonymous avatar Anonymous committed 9374111

The unsure parameter of Rename.get_changes() should be a function

The confirm parameter has been removed. unsure, if not None, will
be called with an Occurrence parameter. If it returns True the
occurrence is renamed.

Comments (0)

Files changed (6)

docs/dev/workingon.txt

 
 * Adding a radio button for deciding what to do about unsure
   occurrences; ignore, ask, rename
-* Removing `unsure` parameter of `Rename.get_changes()`
+* Removing `unsure` parameter of `Rename.get_changes()`
+* Upadating docs

rope/contrib/codeassist.py

     name = rope.base.codeanalyze.get_name_at(resource, offset)
     pyname = rope.base.codeanalyze.get_pyname_at(project.get_pycore(),
                                                  resource, offset)
+    def is_match(occurrence):
+        return unsure
     finder = occurrences.FilteredFinder(
-        project.get_pycore(), name, [pyname], unsure=unsure)
+        project.get_pycore(), name, [pyname], unsure=is_match)
     files = project.get_pycore().get_python_files()
     job_set = task_handle.create_jobset('Finding Occurrences',
                                         count=len(files))

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, confirm=None):
+                 imports=True, unsure=None, docs=False):
         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):
         for pyname in self.pynames:
             if same_pyname(pyname, new_pyname):
                 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
+            elif self.unsure is not None and \
+                 self._unsure_match(pyname, new_pyname):
+                occurrence._unsure = self.unsure(occurrence)
+                return occurrence._unsure
         return False
 
     def _unsure_match(self, expected, pyname):

rope/refactor/rename.py

 from rope.base import exceptions, codeanalyze, pyobjects, pynames, taskhandle
 from rope.base.change import ChangeSet, ChangeContents, MoveResource
 from rope.refactor import occurrences, sourceutils
+import warnings
 
 
 class Rename(object):
         return self.old_name
 
     def get_changes(self, new_name, in_file=False, in_hierarchy=False,
-                    unsure=False, docs=False, confirm=None,
+                    unsure=None, docs=False,
                     task_handle=taskhandle.NullTaskHandle()):
         """Get the changes needed for this refactoring
 
             - `docs`: when `True` rename refactoring will rename
               occurrences in comments and strings where the name is
               visible.  Setting it will make renames faster, too.
-            - `unsure`: rename occurrence even if unsure.  Do not use it
-              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
+            - `unsure`: decides what to do about unsure occurrences.
+              If `None`, they are ignored.  Otherwise `unsure` is
+              called with an instance of `occurrence.Occurrence` as
+              parameter.  If it returns `True`, the occurrence is
+              considered to be a match.
 
         """
+        if unsure in (True, False):
+            warnings.warn(
+                'unsure parameter should be a function that returns'
+                ' True or False', DeprecationWarning, stacklevel=2)
+            def unsure_func(value=unsure):
+                return value
+            unsure = unsure_func
         old_pynames = self._get_old_pynames(in_file, in_hierarchy, task_handle)
         if not in_file and len(old_pynames) == 1 and \
            self._is_renaming_a_function_local_name():
         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, confirm=confirm)
+            self.pycore, self.old_name, old_pynames, unsure=unsure, docs=docs)
         job_set = task_handle.create_jobset('Collecting Changes', len(files))
         for file_ in files:
             job_set.started_job('Working on <%s>' % file_.path)

ropeide/refactor.py

         new_name = self.new_name_entry.get()
         return self.renamer.get_changes(
             new_name, in_file=self.is_local, docs=self.docs.get(), 
-            in_hierarchy=self.in_hierarchy.get(), unsure=True,
-            confirm=self._confirm_occurrence, task_handle=handle)
+            in_hierarchy=self.in_hierarchy.get(),
+            unsure=self._confirm_occurrence, task_handle=handle)
 
     def _confirm_occurrence(self, occurrence):
         unsure = self.unsure_val.get()

ropetest/refactor/renametest.py

                '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)
+        self._rename(mod1, code.index('a_func'),
+                     'new_func', unsure=self._true)
         self.assertEquals(
             'class C(object):\n    def new_func(self):\n        pass\n' \
             'def f(arg):\n    arg.new_func()\n',
             mod1.read())
 
+    def _true(self, *args):
+        return True
+
     def test_renaming_when_unsure_with_confirmation(self):
         def confirm(occurrence):
             return False
                '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._rename(mod1, code.index('a_func'), 'new_func', unsure=confirm)
         self.assertEquals(
             'class C(object):\n    def new_func(self):\n        pass\n' \
             'def f(arg):\n    arg.a_func()\n', mod1.read())
                'c1 = C1()\nc1.a_func()\nc2 = C2()\nc2.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)
+        self._rename(mod1, code.index('a_func'), 'new_func', unsure=self._true)
         self.assertEquals(
             'class C1(object):\n    def new_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.