Anonymous avatar Anonymous committed 5ca05a9

inline: only_current and inlining variables in other modules

Comments (0)

Files changed (2)

rope/refactor/inline.py

     def get_changes(self, remove=True, only_current=False, resources=None,
                     task_handle=taskhandle.NullTaskHandle()):
         if resources is None:
-            if only_current:
-                resources = [self.original]
-            else:
                 resources = self.pycore.get_python_files()
+        if only_current:
+            resources = [self.original]
+            if remove and self.original != self.resource:
+                resources.append(self.resource)
         changes = ChangeSet('Inline variable <%s>' % self.name)
         jobset = task_handle.create_jobset('Calculating changes',
                                            len(resources))
                 source = self._change_main_module(remove, only_current)
                 changes.add_change(ChangeContents(self.resource, source))
             else:
-                result = self._change_module(resource)
+                result = self._change_module(resource, only_current)
                 if result is not None:
                     result = _add_imports(self.pycore, result,
                                           resource, self.imports)
 
     def _change_main_module(self, remove, only_current):
         region = None
-        if only_current:
+        if only_current and self.original == self.resource:
             region = self.region
         return _inline_variable(self.pycore, self.pymodule, self.pyname,
                                 self.name, remove=remove, region=region)
         self.imported, self.imports = move.moving_code_with_imports(
             self.pycore, self.resource, vardef)
 
-    def _change_module(self, resource):
-        finder = occurrences.create_finder(self.pycore, self.name,
-                                           self.pyname, imports=False)
+    def _change_module(self, resource, only_current):
+        filters = [occurrences.NoImportsFilter(),
+                   occurrences.PyNameFilter(self.pyname)]
+        if only_current and resource == self.original:
+            def check_aim(occurrence):
+                start, end = occurrence.get_primary_range()
+                if self.offset < start or end < self.offset:
+                    return False
+            filters.insert(0, check_aim)
+        finder = occurrences.Finder(self.pycore, self.name, filters=filters)
         changed = rename.rename_in_module(
             finder, self.imported, resource=resource, replace_primary=True)
         if changed:

ropetest/refactor/inlinetest.py

         self.assertEquals(code1, self.mod.read())
         self.assertEquals(expected2, self.mod2.read())
 
+    def test_inlining_variables_in_other_modules_and_only_current(self):
+        code1 = 'var = 1\n' \
+                'print(var)\n'
+        code2 = 'import mod\n' \
+                'print(mod.var)\n' \
+                'print(mod.var)\n'
+        self.mod.write(code1)
+        self.mod2.write(code2)
+        self._inline2(self.mod2, self.mod2.read().rindex('var'),
+                      remove=False, only_current=True)
+        expected2 = 'import mod\n' \
+                    'print(mod.var)\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.