Commits

Anonymous committed c7bd660

usefunction: ignoring matches in function body

  • Participants
  • Parent commits 84a3605

Comments (0)

Files changed (4)

rope/refactor/similarfinder.py

         self.args = args
         if end is None:
             end = len(self.source)
-        return self.raw_finder.get_matches(code, start=start, end=end)
+        skip_region = None
+        if 'skip' in args.get('', {}):
+            resource, region = args['']['skip']
+            if resource == self.pymodule.get_resource():
+                skip_region = region            
+        return self.raw_finder.get_matches(code, start=start, end=end,
+                                           skip=skip_region)
 
     def get_match_regions(self, *args, **kwds):
         for match in self.get_matches(*args, **kwds):
         if not hasattr(node, 'sorted_children'):
             self.ast = patchedast.patch_ast(node, source)
 
-    def get_matches(self, code, start=0, end=None):
+    def get_matches(self, code, start=0, end=None, skip=None):
         """Search for `code` in source and return a list of `Match`\es
 
         `code` can contain wildcards.  ``${name}`` matches normal
         for match in self._get_matched_asts(code):
             match_start, match_end = match.get_region()
             if start <= match_start and match_end <= end:
+                if skip is not None and (skip[0] < match_end and
+                                         skip[1] > match_start):
+                    continue                    
                 yield match
 
     def _get_matched_asts(self, code):

rope/refactor/usefunction.py

         pattern = similarfinder.make_pattern(body, params)
         goal = '%s(%s)' % (self.pyfunction.get_name(),
                            ', ' .join(('${%s}' % p) for p in params))
-        restructuring = restructure.Restructure(self.project, pattern, goal)
+
+        defining_resource = self.pyfunction.get_module().get_resource()
+        body_region = sourceutils.get_body_region(self.pyfunction)
+        args_value = {'skip': (defining_resource, body_region)}
+        args = {'': args_value}
+
+        restructuring = restructure.Restructure(
+            self.project, pattern, goal, args=args)
         return restructuring.get_changes(task_handle=task_handle)

rope/refactor/wildcards.py

     def matches(self, suspect, arg=''):
         args = parse_arg(arg)
 
+        if not self._check_exact(args, suspect):
+            return False
+        if not self._check_object(args, suspect):
+            return False
+        return True
+
+    def _check_object(self, args, suspect):
+        kind = None
+        expected = None
+        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)
+                return checker(suspect.pymodule, suspect.node)
+        return True
+
+    def _check_exact(self, args, suspect):
         node = suspect.node
         if args.get('exact'):
             if not isinstance(node, ast.Name) or not node.id == suspect.name:
         else:
             if not isinstance(node, ast.expr):
                 return False
-        kind = None
-        expected = None
-        for check in ['name', 'object', 'type', 'instance']:
-            if check in args:
-                kind = check
-                expected = args[check]
-        if expected is not None:
-            return _CheckObject(self.project, expected, kind)(suspect.pymodule,
-                                                              suspect.node)
         return True
 
 
 def parse_arg(arg):
+    if isinstance(arg, dict):
+        return arg
     result = {}
     tokens = arg.split(',')
     for token in tokens:

ropetest/refactor/usefunctiontest.py

         testutils.remove_project(self.project)
         super(UseFunctionTest, self).tearDown()
 
-    # XXX: the function body should be skipped
-    def xxx_test_simple_case(self):
+    def test_simple_case(self):
         code = 'def f():\n    pass\n'
         self.mod1.write(code)
         user = UseFunction(self.project, self.mod1, code.rindex('f'))