Anonymous avatar Anonymous committed 288985b

inline: handling only_current in other modules

Comments (0)

Files changed (2)

rope/refactor/inline.py

         self.region = range_finder.get_primary_range(offset)
         self.name = range_finder.get_word_at(offset)
         self.offset = offset
+        self.original = resource
 
     def get_changes(self, *args, **kwds):
         pass
         if resources is None:
             resources = self.pycore.get_python_files()
         if only_current:
-            resources = [self.resource]
+            resources = [self.original]
+            if remove:
+                resources.append(self.resource)
         job_set = task_handle.create_jobset('Collecting Changes',
                                             len(resources))
         for file in resources:
                 changes.add_change(self._defining_file_changes(
                         changes, remove=remove, only_current=only_current))
             else:
+                aim = None
+                if only_current and self.original == file:
+                    aim = self.offset
                 handle = _InlineFunctionCallsForModuleHandle(
-                    self.pycore, file, self.others_generator)
+                    self.pycore, file, self.others_generator, aim)
                 result = move.ModuleSkipRenamer(
                     self.occurrence_finder, file, handle).get_changed_module()
                 if result is not None:
 
     def _defining_file_changes(self, changes, remove, only_current):
         start_offset, end_offset = self._get_removed_range()
-        aim = None if not only_current else self.offset
+        aim = None
+        if only_current:
+            if self.resource == self.original:
+                aim = self.offset
+            else:
+                # we don't want to change any of them
+                aim = len(self.resource.read()) + 100
         handle = _InlineFunctionCallsForModuleHandle(
             self.pycore, self.resource,
             self.normal_generator, aim_offset=aim)
     def get_changes(self, remove=True, only_current=False, resources=None,
                     task_handle=taskhandle.NullTaskHandle()):
         if resources is None:
-            resources = self.pycore.get_python_files()
             if only_current:
-                resources = [self.resource]
+                resources = [self.original]
+            else:
+                resources = self.pycore.get_python_files()
         changes = ChangeSet('Inline variable <%s>' % self.name)
         jobset = task_handle.create_jobset('Calculating changes',
                                            len(resources))

ropetest/refactor/inlinetest.py

+from ropetest.testutils import only_for
 import unittest
 
 import rope.base.exceptions
         self._inline2(self.mod2, self.mod2.read().rindex('f'))
         self.assertEquals('print(1)\n', self.mod2.read())
 
+    def test_inlining_functions_in_other_modules_and_only_current(self):
+        code1 = 'def f():\n' \
+                '    return 1\n' \
+                'print(f())\n'
+        code2 = 'import mod\n' \
+                'print(mod.f())\n' \
+                'print(mod.f())\n'
+        self.mod.write(code1)
+        self.mod2.write(code2)
+        self._inline2(self.mod2, self.mod2.read().rindex('f'),
+                      remove=False, only_current=True)
+        expected2 = 'import mod\n' \
+                    'print(mod.f())\n' \
+                    'print(1)\n'
+        self.assertEquals(code1, self.mod.read())
+        self.assertEquals(expected2, self.mod2.read())
+
 
 def suite():
     result = unittest.TestSuite()
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.