Anonymous avatar Anonymous committed edd82a8

extract: handling one-line implicitly continued extractions

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()
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.