Anonymous avatar Anonymous committed 05a8750

restructure: added unsure argument to default wildcard

Comments (0)

Files changed (4)

 ===========
 
 
+- Handling unsure matches in restructurings : January 25, 2008
+
+
 - Added rope.contrib.autoimport : January 25, 2008
 
 

rope/refactor/occurrences.py

             if same_pyname(pyname, new_pyname):
                 return True
             elif self.unsure is not None and \
-                 self._unsure_match(pyname, new_pyname):
+                 unsure_pyname(new_pyname):
                 occurrence._unsure = self.unsure(occurrence)
                 return occurrence._unsure
         return False
 
-    def _unsure_match(self, expected, pyname):
-        if pyname is None:
-            return True
-        if isinstance(pyname, pynames.UnboundName) and \
-           pyname.get_object() == pyobjects.get_unknown():
-            return True
-
 
 class Occurrence(object):
 
     return expected.get_definition_location() == pyname.get_definition_location() and \
            expected.get_object() == pyname.get_object()
 
+def unsure_pyname(pyname, unbound=True):
+    """Return `True` if we don't know what this name references"""
+    if pyname is None:
+        return True
+    if unbound and not isinstance(pyname, pynames.UnboundName):
+        return False
+    if pyname.get_object() == pyobjects.get_unknown():
+        return True
+
 
 class _TextualFinder(object):
 

rope/refactor/wildcards.py

     def _check_object(self, args, suspect):
         kind = None
         expected = None
+        unsure = args.get('unsure', False)
         for check in ['name', 'object', 'type', 'instance']:
             if check in args:
                 kind = check
                 expected = args[check]
             if expected is not None:
-                checker = _CheckObject(self.project, expected, kind)
+                checker = _CheckObject(self.project, expected,
+                                       kind, unsure=unsure)
                 return checker(suspect.pymodule, suspect.node)
         return True
 
 
 class _CheckObject(object):
 
-    def __init__(self, project, expected, kind='object'):
+    def __init__(self, project, expected, kind='object', unsure=False):
         self.project = project
         self.kind = kind
+        self.unsure = unsure
         self.expected = self._evaluate(expected)
 
     def __call__(self, pymodule, node):
         pyname = self._evaluate_node(pymodule, node)
-        if self.expected is None or pyname is None:
-            return False
+        if pyname is None or self.expected is None:
+            return self.unsure
+        if self._unsure_pyname(pyname, unbound=self.kind=='name'):
+            return True
         if self.kind == 'name':
             return self._same_pyname(self.expected, pyname)
         else:
     def _same_pyname(self, expected, pyname):
         return occurrences.same_pyname(expected, pyname)
 
+    def _unsure_pyname(self, pyname, unbound=True):
+        return self.unsure and occurrences.unsure_pyname(pyname, unbound)
+
     def _split_name(self, name):
         parts = name.split('.')
         expression, kind = parts[0], parts[-1]

ropetest/refactor/restructuretest.py

         self.assertEquals('a = 1\n', self.mod.read())
         self.assertEquals('b = 2 / 1\n', mod2.read())
 
+    def test_unsure_argument_of_default_wildcard(self):
+        self.mod.write('def f(p):\n    return p * 2\nx = "" * 2\ni = 1 * 2\n')
+        refactoring = restructure.Restructure(
+            self.project, '${s} * 2', 'dup(${s})',
+            args={'s': {'type': '__builtins__.str','unsure': True}})
+        self.project.do(refactoring.get_changes())
+        self.assertEquals('def f(p):\n    return dup(p)\nx = dup("")\n'
+                          'i = 1 * 2\n', self.mod.read())
+
 
 if __name__ == '__main__':
     unittest.main()
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.