Commits

Ali Gholami Rudi  committed e9cb51b

Defining the variable when extracting nested subexpressions

  • Participants
  • Parent commits 50d1e24

Comments (0)

Files changed (2)

File rope/refactor/extract.py

         matched_lines = []
         for match in collector.matches:
             start = self.info.lines.get_line_number(match.get_region()[0])
-            start_line = self.info.logical_lines.\
-                         logical_line_in(start)[0]
+            start_line = self.info.logical_lines.logical_line_in(start)[0]
             matched_lines.append(start_line)
         location_finder = _DefinitionLocationFinder(self.info, matched_lines)
         collector.definition_location = (location_finder.find_lineno(),
     def __init__(self, info, matched_lines):
         self.info = info
         self.matched_lines = matched_lines
+        # This only happens when subexpressions cannot be matched
+        if not matched_lines:
+            self.matched_lines.append(self.info.region_lines[0])
 
     def find_lineno(self):
         if self.info.make_global and not self.info.global_:

File ropetest/refactor/extracttest.py

 
     # TODO: Handle when extracting a subexpression
     def xxx_test_extract_variable_for_a_subexpression(self):
-        code = 'if True:\n    a = 3 + 1 \\\n + 2\n'
+        code = 'a = 3 + 1 + 2\n'
         start = code.index('1')
         end = code.index('2') + 1
         refactored = self.do_extract_variable(code, start, end, 'b')
-        expected = 'if True:\n    b = 1 + 2\n    a = 3 + b\n'
+        expected = 'b = 1 + 2\na = 3 + b\n'
         self.assertEquals(expected, refactored)
 
     def test_extract_variable_starting_from_the_start_of_the_line(self):