Anonymous avatar Anonymous committed 61f536e

inline: handling inline variable in other modules

Comments (0)

Files changed (2)

rope/refactor/inline.py

         self.pymodule = self.pyname.get_definition_location()[0]
         self.resource = self.pymodule.get_resource()
         self._check_exceptional_conditions()
+        self._init_imports()
 
     def _check_exceptional_conditions(self):
         if len(self.pyname.assignments) != 1:
 
     def get_changes(self, remove=True, only_current=False, resources=None,
                     task_handle=taskhandle.NullTaskHandle()):
-        source = self._get_changed_module(remove, only_current)
+        if resources is None:
+            resources = self.pycore.get_python_files()
         changes = ChangeSet('Inline variable <%s>' % self.name)
-        if resources is None or self.resource in resources:
+        if self.resource in resources:
+            source = self._change_main_module(remove, only_current)
             changes.add_change(ChangeContents(self.resource, source))
+        for resource in resources:
+            if not only_current and self.resource != resource:
+                result = self._change_module(resource)
+                if result is not None:
+                    changes.add_change(ChangeContents(resource, result))
         return changes
 
-    def _get_changed_module(self, remove, only_current):
+    def _change_main_module(self, remove, only_current):
         region = None
         if only_current:
             region = self.region
         return _inline_variable(self.pycore, self.pymodule, self.pyname,
                                 self.name, remove=remove, region=region)
 
+    def _init_imports(self):
+        self.import_tools = importutils.ImportTools(self.pycore)
+        vardef = _getvardef(self.pymodule, self.pyname)
+        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)
+        changed = rename.rename_in_module(
+            finder, self.imported, resource=resource, replace_primary=True)
+        return changed
+
     def get_kind(self):
         return 'variable'
 
 
 def _inline_variable(pycore, pymodule, pyname, name,
                      remove=True, region=None):
-    assignment = pyname.assignments[0]
-    definition_line = assignment.ast_node.lineno
-    lines = pymodule.lines
-    logicals = pymodule.logical_lines
-    start, end = logicals.logical_line_in(definition_line)
-    definition_with_assignment = _join_lines(
-        [lines.get_line(n) for n in range(start, end + 1)])
-    if assignment.levels:
-        raise rope.base.exceptions.RefactoringError(
-            'Cannot inline tuple assignments.')
-    definition = definition_with_assignment[definition_with_assignment.\
-                                            index('=') + 1:].strip()
+    definition = _getvardef(pymodule, pyname)
+    start, end = _assigned_lineno(pymodule, pyname)
 
     occurrence_finder = occurrences.create_finder(pycore, name, pyname)
     changed_source = rename.rename_in_module(
     else:
         source = changed_source
     return source
+
+def _getvardef(pymodule, pyname):
+    assignment = pyname.assignments[0]
+    lines = pymodule.lines
+    start, end = _assigned_lineno(pymodule, pyname)
+    definition_with_assignment = _join_lines(
+        [lines.get_line(n) for n in range(start, end + 1)])
+    if assignment.levels:
+        raise rope.base.exceptions.RefactoringError(
+            'Cannot inline tuple assignments.')
+    definition = definition_with_assignment[definition_with_assignment.\
+                                            index('=') + 1:].strip()
+    return definition
+
+def _assigned_lineno(pymodule, pyname):
+    definition_line = pyname.assignments[0].ast_node.lineno
+    return pymodule.logical_lines.logical_line_in(definition_line)

ropetest/refactor/inlinetest.py

         refactored = self._inline(code, code.index('start'))
         self.assertEquals('print(1 <= 2)\n', refactored)
 
+    def test_inlining_variables_in_other_modules(self):
+        self.mod.write('myvar = 1\n')
+        self.mod2.write('import mod\nprint(mod.myvar)\n')
+        self._inline2(self.mod, 2)
+        self.assertEquals('import mod\nprint(1)\n', 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.