Anonymous avatar Anonymous committed a87c79a

fixed inlining one-liners

Comments (0)

Files changed (5)

 ===========
 
 
+- Better handling of one-liners : January 10, 2008
+
+
 - Choosing which files to apply a restructuring on : January 9, 2008
 
 

rope/refactor/inline.py

 
     def __init__(self, pycore, resource,
                  definition_generator, aim_offset=None):
+        """Inlines occurrences
+
+        If `aim` is not `None` only the occurrences that intersect
+        `aim` offset will be inlined.
+
+        """
         self.pycore = pycore
         self.generator = definition_generator
         self.resource = resource

rope/refactor/sourceutils.py

         start_line = node.body[0].lineno
     elif len(node.body) > 1:
         start_line = node.body[1].lineno
-
     start = lines.get_line_start(start_line)
+    if scope.start == start_line:
+        # a one-liner!
+        # XXX: one-liners with line breaks
+        start = pymodule.source_code.index(':', start) + 1
+        while pymodule.source_code[start].isspace():
+            start += 1
     end = min(lines.get_line_end(scope.end) + 1, len(pymodule.source_code))
     return start, end
 

ropetest/codeanalyzetest.py

         pyname = name_finder.get_pyname_at(code.rindex('var'))
         self.assertEquals(pymod.get_attribute('var'), pyname)
 
+    # XXX: one-liners with line breaks
+    def xxx_test_one_liners_with_line_breaks(self):
+        code = 'var = 1\ndef f(\n): var = 2\nprint var\n'
+        pymod = self.pycore.get_string_module(code)
+        name_finder = rope.base.evaluate.ScopeNameFinder(pymod)
+        pyname = name_finder.get_pyname_at(code.rindex('var'))
+        self.assertEquals(pymod.get_attribute('var'), pyname)
+
 
 class LogicalLineFinderTest(unittest.TestCase):
 

ropetest/refactor/inlinetest.py

         code = 'one = 1\n\na = one\nb = one\n'
         refactored = self._inline(code, code.rindex('one') + 1,
                                   remove=False, only_current=True)
-        self.assertEquals('one = 1\n\na = one\nb = 1\n',
-                          refactored)
+        self.assertEquals('one = 1\n\na = one\nb = 1\n', refactored)
+
+    def test_inlining_one_line_functions(self):
+        code = 'def f(): return 1\nvar = f()\n'
+        refactored = self._inline(code, code.rindex('f'))
+        self.assertEquals('var = 1\n', refactored)
 
 
 def suite():
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.