Commits

Ali Gholami Rudi  committed 37f735c

extract: matching temps with different names when finding similars

  • Participants
  • Parent commits e281194

Comments (0)

Files changed (3)

File rope/refactor/extract.py

 from rope.base import ast, codeanalyze
 from rope.base.change import ChangeSet, ChangeContents
 from rope.base.exceptions import RefactoringError
-from rope.refactor import sourceutils, similarfinder, patchedast, suites
+from rope.refactor import (sourceutils, similarfinder,
+                           patchedast, suites, usefunction)
 
 
 class _ExtractRefactoring(object):
         variables = []
         variables.extend(self._find_function_arguments())
         variables.extend(self._find_function_returns())
-        body = sourceutils.fix_indentation(self.info.extracted, 0)
-        return similarfinder.make_pattern(body, variables)
+        variables.extend(self._find_temps())
+        return similarfinder.make_pattern(self._get_body(), variables)
+
+    def _get_body(self):
+        return sourceutils.fix_indentation(self.info.extracted, 0)
+
+    def _find_temps(self):
+        return usefunction.find_temps(self.info.pycore.project,
+                                      self._get_body())
 
     def get_checks(self):
         if self.info.method and not self.info.make_global:

File rope/refactor/usefunction.py

                                          task_handle=task_handle)
 
     def _find_temps(self):
-        pymodule = self.project.pycore.get_string_module(self._get_body())
-        result = []
-        for name, pyname in pymodule.get_scope().get_names().items():
-            if isinstance(pyname, pynames.AssignedName):
-                result.append(name)
-        return result
+        return find_temps(self.project, self._get_body())
 
     def _module_name(self):
         return importutils.get_module_name(self.project.pycore,
 
     _rope_result = '_rope__result'
     _rope_returned = '_rope__returned'
+
+
+def find_temps(project, code):
+    pymodule = project.pycore.get_string_module(code)
+    result = []
+    for name, pyname in pymodule.get_scope().get_names().items():
+        if isinstance(pyname, pynames.AssignedName):
+            result.append(name)
+    return result

File ropetest/refactor/extracttest.py

             'def b():\n    return one\nb = one\n'
         self.assertEquals(expected, refactored)
 
+    def test_extracting_pieces_with_distinct_temp_names(self):
+        code = 'a = 1\nprint a\nb = 1\nprint b\n'
+        start = code.index('a')
+        end = code.index('\nb')
+        refactored = self.do_extract_method(code, start, end, 'f',
+                                            similar=True, global_=True)
+        expected = '\ndef f():\n    a = 1\n    print a\n\nf()\nf()\n'
+        self.assertEquals(expected, refactored)
+
 
 if __name__ == '__main__':
     unittest.main()