Anonymous avatar Anonymous committed 82b37ae

inline: back importing for inline variable

Comments (0)

Files changed (2)

rope/refactor/inline.py

         self._init_imports()
 
     def _init_imports(self):
-        self.import_tools = importutils.ImportTools(self.pycore)
         body = sourceutils.get_body(self.pyfunction)
         body, imports = move.moving_code_with_imports(
             self.pycore, self.resource, body)
                 result = move.ModuleSkipRenamer(
                     self.occurrence_finder, file, handle).get_changed_module()
                 if result is not None:
-                    result = self._add_imports(result, file)
+                    result = _add_imports(self.pycore, result,
+                                          file, self.imports)
                     changes.add_change(ChangeContents(file, result))
             job_set.finished_job()
         return changes
             return True
         return False
 
-    def _add_imports(self, source, file):
-        if not self.imports:
-            return source
-        pymodule = self.pycore.get_string_module(source, file)
-        module_import = importutils.get_module_imports(self.pycore, pymodule)
-        for import_info in self.imports:
-            module_import.add_import(import_info)
-        source = module_import.get_changed_source()
-        pymodule = self.pycore.get_string_module(source, file)
-        source = self.import_tools.organize_imports(
-            pymodule, unused=False, sort=False)
-        return source
-
     def get_kind(self):
         return 'method'
 
             if not only_current and self.resource != resource:
                 result = self._change_module(resource)
                 if result is not None:
+                    result = _add_imports(self.pycore, result,
+                                          resource, self.imports)
                     changes.add_change(ChangeContents(resource, result))
         return changes
 
                                 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 _assigned_lineno(pymodule, pyname):
     definition_line = pyname.assignments[0].ast_node.lineno
     return pymodule.logical_lines.logical_line_in(definition_line)
+
+def _add_imports(pycore, source, resource, imports):
+    if not imports:
+        return source
+    pymodule = pycore.get_string_module(source, resource)
+    module_import = importutils.get_module_imports(pycore, pymodule)
+    for import_info in imports:
+        module_import.add_import(import_info)
+    source = module_import.get_changed_source()
+    pymodule = pycore.get_string_module(source, resource)
+    import_tools = importutils.ImportTools(pycore)
+    return import_tools.organize_imports(pymodule, unused=False, sort=False)

ropetest/refactor/inlinetest.py

         self._inline2(self.mod, 2)
         self.assertEquals('import mod\nprint(1)\n', self.mod2.read())
 
+    def test_inlining_variables_and_back_importing(self):
+        self.mod.write('mainvar = 1\nmyvar = mainvar\n')
+        self.mod2.write('import mod\nprint(mod.myvar)\n')
+        self._inline2(self.mod, self.mod.read().index('myvar'))
+        expected = 'import mod\n' \
+                   'from mod import mainvar\n' \
+                   'print(mainvar)\n'
+        self.assertEquals(expected, 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.