Commits

Anonymous committed 8b411af

Fixed matching the implicit argument of methods when extracting a method

  • Participants
  • Parent commits 32d89fa

Comments (0)

Files changed (5)

File docs/dev/workingon.txt

-Not Removing The Definition After Inlining
-==========================================
+Small Stories
+=============
 
-- Not removing the definition for inline variable
-- Not removing the definition for inline method
-- Adding to UI
+- Add `_ExtractMethodParts.get_checks()`
+- Fixing extract method failures when the first argument differs!
+
+* Searching all files for extract patterns?
 
 * Callbacks in keybinder hold their parent frame
 * Import actions with more that on phase and filtering problems
 * Renaming ``rope.py`` to ``ropeide.py`` and ``~/.rope`` to
   ``~/.ropeide.py``?
 * Changing the default font on linux to courier?
-* Lots of exceptions for `PyFunction`
 * Commenting based on the line of syntax error in codeassist?
 * Adding restructuring scope; local scope, module, all files?
 * Better search triggering in find type dialog

File docs/done.txt

 ===========
 
 
-- Not removing the definition after inlining : September 1, 2007
+- An option for not removing the definition after inlining : September 1, 2007
 
 
 - Performing import actions on individual imports : September 1, 2007
 - ``C-u`` action prefix : September 1, 2007
 
 
+- Changing inline and move to use froms for back imports : August 27, 2007
+
+
 > Public Release 0.6.1 : August 19, 2007
 
 

File rope/refactor/extract.py

         """Get the changes this refactoring makes
 
         if `similar` is `True` similar expressions/statements are also
-        replaced with the extracted.
+        replaced.
         """
         info = _ExtractInfo(
             self.project, self.resource, self.start_offset, self.end_offset,
     def __init__(self, info):
         self.definition = None
         self.body_pattern = None
+        self.checks = {}
         self.replacement_pattern = None
         self.matches = None
         self.replacements = None
 
     def _find_matches(self, collector):
         regions = self._where_to_search()
-        finder = similarfinder.CheckingFinder(self.info.pymodule, {})
+        finder = similarfinder.CheckingFinder(self.info.pymodule,
+                                              collector.checks,
+                                              check_all=False)
         matches = []
         for start, end in regions:
             matches.extend((finder.get_matches(
         collector.definition = parts.get_definition()
         collector.body_pattern = parts.get_body_pattern()
         collector.replacement_pattern = parts.get_replacement_pattern()
+        collector.checks = parts.get_checks()
 
 
 class _DefinitionLocationFinder(object):
         body = sourceutils.fix_indentation(self.info.extracted, 0)
         return similarfinder.make_pattern(body, variables)
 
+    def get_checks(self):
+        if self.info.method:
+            if _get_function_kind(self.info.scope) == 'method':
+                return {'?%s.type' % self._get_self_name():
+                        self.info.scope.parent.pyobject}
+        return {}
+
     def _create_info_collector(self):
         zero = self.info.scope.get_start() - 1
         start_line = self.info.region_lines[0] - zero
     def get_replacement_pattern(self):
         return self.info.new_name
 
+    def get_checks(self):
+        return {}
+
 
 class _FunctionInformationCollector(object):
 

File rope/refactor/similarfinder.py

 
     """
 
-    def __init__(self, pymodule, checks):
+    def __init__(self, pymodule, checks, check_all=True):
+        """Construct a CheckingFinder
+
+        The `check_all` is `False` missing names are ignored.
+
+        """
         super(CheckingFinder, self).__init__(
             pymodule.source_code, pymodule.get_ast())
         self.pymodule = pymodule
         self.checks = checks
+        self.check_all = check_all
 
     def get_matches(self, code, start=0, end=None):
         if end is None:
                 name, kind = self._split_name(check)
                 node = match.get_ast(name)
                 if node is None:
-                    raise BadNameInCheckError('Unknown name <%s>' % name)
+                    if self.check_all:
+                        raise BadNameInCheckError('Unknown name <%s>' % name)
+                    else:
+                        continue
                 pyname = self._evaluate_node(node)
                 if kind == 'name':
                     if not self._same_pyname(expected, pyname):

File ropetest/refactor/extracttest.py

                    '    print(f(1))\n\na_func()\n'
         self.assertEquals(expected, refactored)
 
+    def test_extract_method_and_varying_first_parameter(self):
+        code = 'class C(object):\n' \
+               '    def f1(self):\n        print(str(self))\n' \
+               '    def f2(self):\n        print(str(1))\n'
+        start = code.index('print(') + 6
+        end = code.index('))\n') + 1
+        refactored = self.do_extract_method(code, start, end,
+                                            'to_str', similar=True)
+        expected = 'class C(object):\n' \
+                   '    def f1(self):\n        print(self.to_str())\n\n' \
+                   '    def to_str(self):\n        return str(self)\n' \
+                   '    def f2(self):\n        print(str(1))\n'
+        self.assertEquals(expected, refactored)
+
 
 if __name__ == '__main__':
     unittest.main()