Commits

Anonymous committed edd82a8

extract: handling one-line implicitly continued extractions

  • Participants
  • Parent commits a5902be

Comments (0)

Files changed (2)

rope/refactor/extract.py

         if end_scope != info.scope and end_scope.get_end() != end_line:
             raise RefactoringError('Bad region selected for extract method')
         try:
-            if _UnmatchedBreakOrContinueFinder.has_errors(
-               info.source[info.region[0]:info.region[1]]):
+            extracted = info.source[info.region[0]:info.region[1]]
+            if info.one_line:
+                extracted = '(%s)' % extracted
+            if _UnmatchedBreakOrContinueFinder.has_errors(extracted):
                 raise RefactoringError('A break/continue without having a '
                                        'matching for/while loop.')
         except SyntaxError:
         return similarfinder.make_pattern(self._get_body(), variables)
 
     def _get_body(self):
-        return sourceutils.fix_indentation(self.info.extracted, 0)
+        result = sourceutils.fix_indentation(self.info.extracted, 0)
+        if self.info.one_line:
+            result = '(%s)' % result
+        return result
 
     def _find_temps(self):
         return usefunction.find_temps(self.info.pycore.project,
         return result
 
     def get_body_pattern(self):
-        return self.info.extracted.strip()
+        return '(%s)' % self.info.extracted.strip()
 
     def get_replacement_pattern(self):
         return self.info.new_name

ropetest/refactor/extracttest.py

                    'def g():\n    var = 1\n    return var\n'
         self.assertEquals(expected, refactored)
 
+    def test_extracting_variable_and_implicit_continuations(self):
+        code = 's = ("1"\n  "2")\n'
+        start = code.index('"')
+        end = code.rindex('"') + 1
+        refactored = self.do_extract_variable(code, start, end, 's2')
+        expected = 's2 = "1" "2"\ns = (s2)\n'
+        self.assertEquals(expected, refactored)
+
+    def test_extracting_method_and_implicit_continuations(self):
+        code = 's = ("1"\n  "2")\n'
+        start = code.index('"')
+        end = code.rindex('"') + 1
+        refactored = self.do_extract_method(code, start, end, 'f')
+        expected = '\ndef f():\n    return "1" "2"\n\ns = (f())\n'
+        self.assertEquals(expected, refactored)
+
 
 if __name__ == '__main__':
     unittest.main()