Anonymous avatar Anonymous committed 10cf79e

autoimport: handling moved resources

Comments (0)

Files changed (2)

rope/contrib/autoimport.py

         project.data_files.add_write_hook(self.write)
         # XXX: handle moved and removed
         observer = resourceobserver.ResourceObserver(
-            changed=self._changed)
+            changed=self._changed, moved=self._moved)
         if observe:
             project.add_observer(observer)
 
     def update_resource(self, resource):
         try:
             pymodule = self.project.pycore.resource_to_pyobject(resource)
-            modname = importutils.get_module_name(self.project.pycore,
-                                                  resource)
+            modname = self._module_name(resource)
             self._add_names(pymodule, modname)
         except exceptions.ModuleSyntaxError:
             pass
 
+    def _module_name(self, resource):
+        return importutils.get_module_name(self.project.pycore, resource)
+
     def _add_names(self, pymodule, modname):
         # XXX: exclude imported names
         globals = []
     def _changed(self, resource):
         if not resource.is_folder():
             self.update_resource(resource)
+
+    def _moved(self, resource, newresource):
+        if not resource.is_folder():
+            modname = self._module_name(resource)
+            if modname in self.names:
+                del self.names[modname]
+            self.update_resource(newresource)

ropetest/contrib/autoimporttest.py

         self.mod1 = testutils.create_module(self.project, 'mod1')
         self.pkg = testutils.create_package(self.project, 'pkg')
         self.mod2 = testutils.create_module(self.project, 'mod2', self.pkg)
-        self.importer = autoimport.AutoImport(self.project)
+        self.importer = autoimport.AutoImport(self.project, observe=False)
 
     def tearDown(self):
         testutils.remove_project(self.project)
                           set(self.importer.get_modules('myvar')))
 
 
+class AutoImportObservingTest(unittest.TestCase):
+
+    def setUp(self):
+        super(AutoImportObservingTest, self).setUp()
+        self.project = testutils.sample_project()
+        self.mod1 = testutils.create_module(self.project, 'mod1')
+        self.pkg = testutils.create_package(self.project, 'pkg')
+        self.mod2 = testutils.create_module(self.project, 'mod2', self.pkg)
+        self.importer = autoimport.AutoImport(self.project, observe=True)
+
+    def tearDown(self):
+        testutils.remove_project(self.project)
+        super(AutoImportObservingTest, self).tearDown()
+
+    def test_writing_files(self):
+        self.mod1.write('myvar = None\n')
+        self.assertEquals(['mod1'], self.importer.get_modules('myvar'))
+
+    def test_moving_files(self):
+        self.mod1.write('myvar = None\n')
+        self.mod1.move('mod3.py')
+        self.assertEquals(['mod3'], self.importer.get_modules('myvar'))
+
+
 if __name__ == '__main__':
     unittest.main()
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.